首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell -递归地将文本追加到子目录中文件名的末尾

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

Stack Overflow用户
提问于 2020-04-23 13:26:05
回答 1查看 399关注 0票数 1

概述

嗨,StackOverflow!

我希望这里有人能帮我。

我已经构建了以下代码来将文本附加到网络驱动器中文件名的末尾。所讨论的文件主要是".zip“文件,通常从父文件夹中的6-8个子目录到每个子目录中的8-10个".zip”文件。

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

代码:

代码语言:javascript
复制
#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的文件数:

代码语言:javascript
复制
Get-ChildItem -Directory | ForEach-Object { Write-Host $_.FullName $(Get-ChildItem $_ | Measure-Object).Count}

但两个人都不适合我。

摘要:

我希望有人能向我指出我正在犯的错误,以及需要修复的地方。我愿意接受所有的建议。如果有更好的方法,也请告诉我。我不介意改变代码的工作方式,如果它意味着相同的结果。

感谢您的阅读!

拉吉夫·艾哈迈德

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-23 15:01:53

代码中有一些错误/错误:

  • ,您要求提供一个$fileLocation,但在代码中不使用它。
  • 使用变量$currentDirectory,但没有定义。然后,在变量$ZipFiles中收集所有压缩文件,在这个数组中迭代,其中已经有压缩文件,但是使用Get-ChildItem来再次获取它们。;-)

像这样的事情实际上应该有效:

代码语言:javascript
复制
$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-Host
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61388388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档