Powershell-递归地将文本附加到子目录中文件名的末尾



概述

嗨,StackOverflow!

我希望这里有人能帮我。

我构建了以下代码,将文本附加到网络驱动器上的文件名末尾。有问题的文件主要是".zip"文件,通常在父文件夹中有6-8个子目录,每个子目录有8-10个".zip"。

我希望实现的是递归地将文本附加到每个文件名的末尾。

代码:

#Current Directory of creatives
$fileLocation = read-host "Type/Paste location of creatives"
if (Test-Path \serverPathname* -PathType Leaf) {
$serverPathName = "\serverPathname"
$driveLetter = "D:"
$fileLocation = ($fileLocation -replace [regex]::Escape($serverPathName),$driveLetter)
}
$fileLocation = Resolve-Path $fileLocation
Write-Output $fileLocation
$newText = read-host "Please enter text to append"
$newText = $newText -replace 's','-'
$addMarket = read-host "Are folders split by market? [Y/N]"
$ZipFiles = Get-ChildItem -Path "$currentDirectory" -Recurse -Filter "*.zip"
if ($addMarket -eq "N") {
foreach  ($Zipfile in $ZipFiles) {
Get-ChildItem | %{$_|rename-item -NewName ((($_.BaseName -replace 's','-') + "-" + $newText + $_.Extension))}
}
}
elseif($addMarket -eq "Y") {
foreach  ($Zipfile in $ZipFiles) {
Get-ChildItem -File -Recurse | Rename-Item -NewName {(($_.BaseName -replace 's','-')  + "-" + (($_.Directory.Name -replace 's','-')) + "-" + $newText + $_.Extension).ToLower()}
}
}
else {
write-host "ERROR! Incorrect Input!"
Write-Host "Exiting Script..."
Exit
}
#Clear Console
clear
#Read-Host -Prompt “Press Enter to exit”

现状:

当我运行上面的(没有循环函数(时,它运行得很好,但我必须进入每个子目录,并直接在那里运行脚本——这显然是耗时和单调的。

当我在循环中运行它时,它可以工作,但会循环很长一段时间,直到达到".zip"文件的总数。例如,8个子目录,9".zip"=循环72次-每次都附加相同的文本。

预期情况:

我希望从子目录的父文件夹运行脚本,并且脚本只会在每个子目录中将文本附加到所有".zip"中一次,然后退出脚本。

问题(?(

我认为问题在于以下变量:

$ZipFiles

但我不知道如何纠正这一点。我已将其设置为在父文件夹中查找所有".zip"文件。我还设置了它来计算每个子目录的文件数:

Get-ChildItem -Directory | ForEach-Object { Write-Host $_.FullName $(Get-ChildItem $_ | Measure-Object).Count}

但两者都不适用于我。

摘要:

我希望有人能向我指出我正在犯的错误以及需要修复的地方。我对所有的建议都持开放态度。如果有更好的方法,也请告诉我。如果这意味着相同的最终结果,我不介意更改代码的工作方式。

感谢阅读!

Rajiv Ahmed

您的代码中有一些错误:
-您要求提供$fileLocation,但您没有在代码中使用它
-相反,您使用了一个变量$currentDirectory,但该变量尚未定义
-然后在变量$ZipFiles中收集所有zip文件,在已经有zip文件但使用Get-ChildItem再次获取的数组上迭代。;-(

像这样的东西实际上应该有效:

$fileLocation = read-host "Type/Paste location of creatives"
if (Test-Path \serverPathname* -PathType Leaf) {
$serverPathName = "\serverPathname"
$driveLetter = "D:"
$fileLocation = ($fileLocation -replace [regex]::Escape($serverPathName), $driveLetter)
}
$fileLocation = Resolve-Path $fileLocation
Write-Output $fileLocation
$newText = read-host "Please enter text to append"
$newText = $newText -replace 's', '-'
$addMarket = read-host "Are folders split by market? [Y/N]"
if ($addMarket -eq "N") {
Get-ChildItem -Path $fileLocation -Recurse -Filter "*.zip" -File |  
ForEach-Object { 
Rename-Item -Path $_.FullName -NewName ((($_.BaseName -replace 's', '-') + "-" + $newText + $_.Extension)) 
}
}
elseif ($addMarket -eq "Y") {
Get-ChildItem -Path $fileLocation -Recurse -Filter "*.zip" -File |  
ForEach-Object { 
Rename-Item -Path $_.FullName -NewName ((($_.BaseName -replace 's', '-') + "-" + (($_.Directory.Name -replace 's', '-')) + "-" + $newText + $_.Extension).ToLower())
}
}
else {
write-host "ERROR! Incorrect Input!"
Write-Host "Exiting Script..."
Exit
}
Clear-Host

最新更新