【问题标题】:Powershell: get all .yaml files, execute commands on those yaml files, then rename themPowershell:获取所有 .yaml 文件,对这些 yaml 文件执行命令,然后重命名它们
【发布时间】:2021-11-16 00:32:48
【问题描述】:

我想制作一个 Powershell 脚本来执行以下操作(用 Python 输入以进行演示):

CommandTemplate = "toPDF main.tex xyz"                  # a command template
ListOfFiles = glob.glob("*.yaml")                       # get all yaml files in cwd
for file in ListOfFiles:                                # iterate thry
    os.system(CommandTemplate.replace("xyz", file))      # call command, will make a main.pdf. Maybe want to wrap this in brackets?
    os.rename("main.pdf", file.replace(".yaml", ".pdf")) # rename main.pdf to match yaml

我不知道从哪里开始。任何帮助表示赞赏。

【问题讨论】:

    标签: python powershell


    【解决方案1】:

    根据您的要求更新要替换的目录路径和文本。

    $files = Get-ChildItem -Path "d:\Test" -Name *.yaml
    
    foreach($file in $files)
    {
     ((Get-Content -path "d:\test\$file") -replace 'abc','def') | Set-Content -Path "D:\test\$file"
     $newName = $file.Split('.')[0]
     Rename-Item -Path d:\test\$file -NewName "$newName.pdf"
    }
    

    【讨论】:

    • 我看不到命令​​实际执行的位置;你能解释一下我将把命令的toPDF main.tex 部分放在哪里吗?
    猜你喜欢
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 2018-11-18
    • 2013-08-21
    • 2021-03-31
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    相关资源
    最近更新 更多