【问题标题】:Powershell -Recurse Parameter; How to exclude specific subdirectoryPowershell - 递归参数;如何排除特定子目录
【发布时间】:2017-01-06 02:25:58
【问题描述】:

我有一个 powershell 脚本,它可以在将文件从 dev 复制到根文件夹后更改文件扩展名等。我已经成功复制了文件。但是,我在使用适用于所有子文件夹的 -Recurse(我想排除 dev 文件夹)或仅将代码应用于 eoot 文件夹之间陷入困境。

代码:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "$w = $args[0];foreach ($file in Get-ChildItem -Recurse -Path "$w" -Exclude *.bat, *.txt, *.png, *.jpg, *.jpeg, *.gif, *.mp3, *.mp4, dev\\*) {"$w+$file";(Get-Content $w$file).replace('dev/', '') | Set-Content "$w$file";(Get-Content $w$file).replace('.min.js', '.minjs') | Set-Content "$w$file";(Get-Content $w$file).replace('.json', '.xson') | Set-Content "$w$file";(Get-Content $w$file).replace('https://www.google-analytics.com/analytics.js', '/httpswgacanalyticsjs/') | Set-Content "$w$file";(Get-Content $w$file).replace('.js', '.min.js') | Set-Content "$w$file";(Get-Content $w$file).replace('.minjs', '.min.js') | Set-Content "$w$file";(Get-Content $w$file).replace('.xson', '.json') | Set-Content "$w$file";(Get-Content $w$file).replace('/httpswgacanalyticsjs/', 'https://www.google-analytics.com/analytics.js') | Set-Content "$w$file";(Get-Content $w$file).replace('.min.css', '.mincss') | Set-Content "$w$file";(Get-Content $w$file).replace('.css', '.min.css') | Set-Content "$w$file";(Get-Content $w$file).replace('.mincss', '.min.css') | Set-Content "$w$file";};Write-Host ".""" "%path%"

输入:

FOLDER
    -> dev
        ->files
        ->folders 

输出:

FOLDER
    -> dev        //exclude
        ->files   //exclude
        ->folders //exclude
    ->files //execute code on this
        ->folders //execute code on this

【问题讨论】:

    标签: powershell


    【解决方案1】:

    试试这样的东西(PowerShell v5 使用 -file 选项)

    $w = "c:\temp\";
    $exclude=@("*.bat", "*.txt", "*.png", "*.jpg", "*.jpeg", "*.gif", "*.mp3", "*.mp4")
    
    $hashreplace = @{}
    $hashreplace.'dev/'= ''
    $hashreplace.'.min.js'= '.minjs'
    $hashreplace.'.json'= '.xson'
    $hashreplace.'https://www.google-analytics.com/analytics.js'= '/httpswgacanalyticsjs/'
    $hashreplace.'.js'= '.min.js'
    $hashreplace.'.minjs'= '.min.js'
    $hashreplace.'.xson'= '.json'
    $hashreplace.'/httpswgacanalyticsjs/'= 'https://www.google-analytics.com/analytics.js'
    $hashreplace.'.min.css'= '.mincss'
    $hashreplace.'.css'= '.min.css'
    $hashreplace.'.mincss'= '.min.css'
    
    foreach ($file in Get-ChildItem -Recurse -File -Path "$w" -Exclude $exclude | where {$_.fullname -notlike "*\dev\*"}) 
    {
        $text=(Get-Content $file.FullName)
        $hashreplace.Keys | %{ $text = $text.Replace($_, $hashreplace[$_])}
        $text | set-content $file.FullName 
    };
    

    【讨论】:

    • 如何从批处理文件启动这个 Powershell 文件?我看不到让它工作,因为它们是路径中的一个空间。另外如何输入参数?
    • powershell -executionPolicy bypass -noexit -file "c:\temp\path with space\test.ps1" "argument"
    • 经过反复试验,where 语句在此处“*\dev*”中缺少星号。感谢您的帮助,它现在正在运行。
    【解决方案2】:

    我没有对此进行测试,所以我只是脱离理论,但以下内容可能对我理解您正在尝试做的事情有所帮助。

    添加类似的内容:

    $filesToRecurse = Get-ChildItem -Path {pathToFileToRecurse} -Exclude {pathToFolderToExclude} | where {! $_.PSIsContainer}
    

    然后使用 $filesToRecurse 变量作为要对其执行重命名的文件列表,因为它不会包含您希望排除的文件或文件夹。

    注意:如果要排除多个位置等,也可以创建和使用数组变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-16
      • 1970-01-01
      • 1970-01-01
      • 2022-09-26
      • 2018-09-18
      • 2018-03-18
      • 1970-01-01
      相关资源
      最近更新 更多