Powershell-递归参数;如何排除特定子目录
我有一个powershell脚本,它在将文件从dev复制到根文件夹后更改文件扩展名等。我已经成功地复制了这些文件。但是,我在使用-Recurse(它适用于所有子文件夹)(我想排除dev文件夹)和仅将代码应用于eoot文件夹之间左右为难 代码: 输入:Powershell-递归参数;如何排除特定子目录,powershell,Powershell,我有一个powershell脚本,它在将文件从dev复制到根文件夹后更改文件扩展名等。我已经成功地复制了这些文件。但是,我在使用-Recurse(它适用于所有子文件夹)(我想排除dev文件夹)和仅将代码应用于eoot文件夹之间左右为难 代码: 输入: FOLDER -> dev ->files ->folders 输出: FOLDER -> dev //exclude ->files
FOLDER
-> dev
->files
->folders
输出:
FOLDER
-> dev //exclude
->files //exclude
->folders //exclude
->files //execute code on this
->folders //execute code on this
我还没有测试过这一点,所以我只是脱离了理论,但以下内容可能会对我理解你正在尝试做的事情有所帮助 添加如下内容:
$filesToRecurse = Get-ChildItem -Path {pathToFileToRecurse} -Exclude {pathToFolderToExclude} | where {! $_.PSIsContainer}
然后对要执行重命名的文件列表使用$filesToRecurse变量,因为它不包含要排除的文件或文件夹
注意:如果要排除多个位置等,也可以创建和使用数组变量。尝试类似的操作(PowerShell v5供使用-文件选项)
如何从批处理文件启动此Powershell文件?我看不到如何让它工作,因为它们是道路上的一个空间。此外,如何输入参数?powershell-executionPolicy bypass-noexit-file“c:\temp\path with space\test.ps1”“参数”经过多次尝试和错误后,where语句此处缺少星号“*\dev*”。感谢您的帮助,它现在正在运行。
$filesToRecurse = Get-ChildItem -Path {pathToFileToRecurse} -Exclude {pathToFolderToExclude} | where {! $_.PSIsContainer}
$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
};