我正在制作一个脚本,使用预先提供的前缀和一个含有6位数字的值来重命名文件夹中的文件,这些值与前缀连接在一起时会给出文档标题。powershell脚本文件重命名帮助
我需要脚本遍历文件列表从提供的文件夹,并更改每个文件名的前缀incrimenting incrimental数字每次与初始提供的数字是第一次incriment数。
我很接近,但我一直有一些错误,我不知道哪里会出错。请指教。
Write-host "Please enter prefix:"
[String]$strPrefix = read-host
Write-host "Please enter incrimental value:"
[int]$intInc = read-host
Write-host "Please enter Files folder:"
[String]$strFiles = "C:\scripts\files"
#$items = Get-ChildItem -Path $strFiles
$items = $strFiles
$intInc
#for each file in $strFiles
foreach ($file in $items)
{
$newName = $strPrefix + ('0' * (6 - $intInc.ToSTring().Length)) + ($intInc++).ToString()
if ($extOnly.length -eq 0)
{
Rename-Item New-Name{$file -replace '$newName'}
}
else
{
Write-host "NewName $newName$extOnly"
Rename-Item New-Name{$file -replace '$newName$extOnly'}
} #end else
$file
}#end for
我想我接近,但东西只是使其翻倒
你能后的错误?什么代表$ extOnly变量? $ items = $ strFiles没有意义! –
'#$ items = Get-ChildItem -Path $ strFiles'应该被注释掉吗?你为什么将'$ items'分配给下一行的字符串?字符串上的'foreach'只会循环一次。 – Rynant
请注意,如果'Write-Host“请输入前缀:'',因为您可以将提示传递给'Read-Host',例如'$ strPrefix = Read-Host -Prompt“请输入前缀”' –