概述
嗨,StackOverflow!
我希望这里有人能帮我。
我已经构建了以下代码来将文本附加到网络驱动器中文件名的末尾。所讨论的文件主要是".zip“文件,通常从父文件夹中的6-8个子目录到每个子目录中的8-10个".zip”文件。
我希望实现的是递归地将文本追加到每个文件名的末尾。
代码:
#Current Directory of creatives
$fileLocation = read-host "Type/Paste location of creatives"
if (Test-Path \\serverPath\name\* -PathType Leaf) {
$serverPathName = "\\serverPath\name\"
$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“文件。我还将其设置为计算每个子dir的文件数:
Get-ChildItem -Directory | ForEach-Object { Write-Host $_.FullName $(Get-ChildItem $_ | Measure-Object).Count}但两个人都不适合我。
摘要:
我希望有人能向我指出我正在犯的错误,以及需要修复的地方。我愿意接受所有的建议。如果有更好的方法,也请告诉我。我不介意改变代码的工作方式,如果它意味着相同的结果。
感谢您的阅读!
拉吉夫·艾哈迈德
发布于 2020-04-23 15:01:53
代码中有一些错误/错误:
$fileLocation,但在代码中不使用它。$currentDirectory,但没有定义。然后,在变量$ZipFiles中收集所有压缩文件,在这个数组中迭代,其中已经有压缩文件,但是使用Get-ChildItem来再次获取它们。;-) 像这样的事情实际上应该有效:
$fileLocation = read-host "Type/Paste location of creatives"
if (Test-Path \\serverPath\name\* -PathType Leaf) {
$serverPathName = "\\serverPath\name\"
$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-Hosthttps://stackoverflow.com/questions/61388388
复制相似问题