2011-08-12 127 views
0

我正在制作一个脚本,使用预先提供的前缀和一个含有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 

我想我接近,但东西只是使其翻倒

+0

你能后的错误?什么代表$ extOnly变量? $ items = $ strFiles没有意义! –

+0

'#$ items = Get-ChildItem -Path $ strFiles'应该被注释掉吗?你为什么将'$ items'分配给下一行的字符串?字符串上的'foreach'只会循环一次。 – Rynant

+0

请注意,如果'Write-Host“请输入前缀:'',因为您可以将提示传递给'Read-Host',例如'$ strPrefix = Read-Host -Prompt“请输入前缀”' –

回答

0
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:\temp\files" 

$files = get-childitem $strFiles -recurse 

ForEach ($file in $files) { 
$intIncPadded = "{0:D6}" -f $intInc 
$newName = "$strPrefix$intIncPadded" + $file.Extension 
Rename-Item $file.FullName $newName 
$intInc = $intInc + 1 
} 
+0

rite-host“请输入Files文件夹:” [String] $ strFiles =“C:\ temp \ files” - 不应该有只读 - 主持人在那里? – EBGreen

+0

谢谢,我自己为脚本文件夹添加了填充和读主机,但这种方式看起来有点整齐。谢谢 –

+0

不客气......任何将此标记为答案的机会? :) – nimizen