powershell脚本文件重命名帮助

发布于 2024-11-29 01:24:26 字数 928 浏览 0 评论 0原文

我正在制作一个脚本,使用预先提供的前缀与增量 6 位值相结合来重命名文件夹中的文件,该值与前缀连接时给出文档标题。

我需要脚本从提供的文件夹中迭代文件列表,并使用每次递增编号的前缀更改每个文件名,其中初始提供的编号是第一个递增编号。

我很接近,但我遇到了一些错误,我不确定哪里出了问题。请指教。

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

我想我已经很接近了,但有什么东西让它倒塌

Im making a script to rename files in a folder using a preprovided prefix combined with an incrimental 6 digit value which when concatenated with the prefix gives the document title.

I need the script to iterate through the file list from a provided folder and alter each file name with the prefix incrimenting the incrimental number each time with the initial provided number being the first incriment.

I am close but i have been having some errors and im not sure where im going wrong. please advise.

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

I think im close but something is just making it fall over

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分開簡單 2024-12-06 01:24:26
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
}
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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文