我有很多文件夹,并且内部有这些不同的文件。每个文件夹及其子女文件具有相同的名称和不同的扩展名,因此在ABC文件夹中,有ABC.png,abc.prj,abc.pgw文件,在def文件夹中,有def.png,def.prj,def.pgw文件等。使用脚本,我创建了一个带有PNG文件名列表的TXT文件。然后,我在第2行中放了一个名称中的新名称,在第4行中,第3行中的名称为第3行,依此类推。现在,我正在搜索一个powershell脚本: - 扫描第1行中的所有文件夹,然后用第2行中的名称替换为 - 扫描第3行中的所有文件夹,然后用第4行中的名称替换为
替换。我在下面尝试了一下,但它不起作用。
您有一些建议吗?谢谢你
$0=0
$1=1
do {
$find=Get-Content C:1SrvMapsName.txt | Select -Index $0
$repl=Get-Content C:1SrvMapsName.txt | Select -Index $1
Get-ChildItem C:1newmaps -Recurse | Rename-Item -NewName { $_.name -replace $find, $repl} -verbose
$0=$0+2
$1=$1+2
}
until ($0 -eq "")
我相信您的代码有几件事,而Manuel给了您。
- 尽管您有旧文件名和新文件名的列表,但您却没有在
Get-ChildItem
CMDLET中使用该列表,而是尝试替换它找到的所有文件。 - 使用
-replace
使用正则表达式替换,这意味着特殊字符.
在文件名中被视为Any Character
,而不仅仅是一个点。 - 您正在尝试查找
*.png
文件,但是您不使用Get-ChildItem
CMDLET添加-Filter
,因此现在它将返回所有Filetypes。
无论如何,我对您有不同的方法:
如果您的输入文件C:1SrvMapsName.txt
看起来像这样:
picture1.png
ABC_1.png
picture2.png
DEF_1.png
picture3.png
DEF_2.png
以下代码将使用它来构建查找标签,以便它可以对输入文件中提到的文件作用,并使所有其他文件保持不变。
$mapsFile = 'C:1Srv2_MapsName.txt'
$searchPath = 'C:1NewMaps'
# Read the input file as an array of strings.
# Every even index contains the file name to search for.
# Every odd index number has the new name for that file.
$lines = Get-Content $mapsFile
# Create a hashtable to store the filename to find
# as Key, and the replacement name as Value
$lookup = @{}
for ($index = 0; $index -lt $lines.Count -1; $index += 2) {
$lookup[$lines[$index]] = $lines[$index + 1]
}
# Next, get a collection of FileInfo objects of *.png files
# If you need to get multiple extensions, remove the -Filter and add -Include '*.png','*.jpg' etc.
$files = Get-ChildItem -Path $searchPath -Filter '*.png' -File -Recurse
foreach ($file in $files) {
# If the file name can be found as Key in the $lookup Hashtable
$find = $file.Name
if ($lookup.ContainsKey($find)) {
# Rename the file with the replacement name in the Value of the lookup table
Write-Host "Renaming '$($file.FullName)' --> $($lookup[$find])"
$file | Rename-Item -NewName $lookup[$find]
}
}
编辑
如果输入文本文件'c: 1 srv mapsname.txt'不包含文件名,包括其扩展名,请将最终的foreach
循环更改为:
foreach ($file in $files) {
# If the file name can be found as Key in the $lookup Hashtable
# Look for the file name without extension as it is not given in the 'MapsName.txt' file.
$find = [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
if ($lookup.ContainsKey($find)) {
# Rename the file with the replacement name in the Value of the lookup table
# Make sure to add the file's extension if any.
$newName = $lookup[$find] + $file.Extension
Write-Host "Renaming '$($file.FullName)' --> '$newName'"
$file | Rename-Item -NewName $newName
}
}
希望有帮助
您的摘要中的问题是它永远不会结束。我尝试了它,它起作用,但一直在循环。我创建了一个使用文件a.txt,b.txt和c.txt的文件夹。在Map.txt中,我有此内容:
a.txt
a2.md
b.txt
b2.md
c.txt
c2.md
运行以下脚本我设法将每个文件重命名为预期。
$0=0
$1=1
$find=Get-Content D:map.txt | Select -Index $0
while($find) {
$find=Get-Content D:map.txt | Select -Index $0
$repl=Get-Content D:map.txt | Select -Index $1
if(!$find -Or !$repl) {
break;
}
Get-ChildItem D:Files -Recurse | Rename-Item -NewName { $_.name -replace $find, $repl} -verbose
$0=$0+2
$1=$1+2
}