我从您的其他职位,这有助于理解帽子你正在尝试做以下:
cmd insert text into the middle of the file name
我已经修改我的代码做什么你会喜欢它。它只是在开始时使用“modelname”或“dr network”移动并重命名rpt文件。你可以改变你的喜好。
它还允许您指定SourceDir,或者可以将该值保留为“。”。相对路径。
# Rename using replace to insert text in the middle of the name
# Set directory where the files you want to rename reside
# This will allow you to run the script from outside the source directory
Set-Variable -Name sourcedir -Value "."
# Set folder and rename variables
Set-Variable -Name modelname -Value "dr network_"
Set-Variable -Name id -Value "10yr_"
# Set new filename which is modelname string + id variable
Set-Variable -Name newmodelname -Value $modelname$id
# Check if folder exisits, if not create
if(!(Test-Path -Path $sourcedir\$modelname)){
# rem make directoy with modelname variable
New-Item -ItemType directory -Path $sourcedir\$modelname
}
# Move the rpt files to new dir created
Move-Item -Path $sourcedir\$modelname*.rpt -Destination $sourcedir\$modelname
# Using GetChildItem (i.e. Dir in dos) command with the pipe, will send a list of files to the Rename-Item command
# The Rename-Item command is replacing the modelname string with new modelname string defined at the start
Get-ChildItem -Path $sourcedir\$modelname | Rename-Item -NewName { $_.name -replace $modelname, $newmodelname }
# You can remove the stuff below this line -----
# Pause here so you can check the result.
# List renamed files in their new directory
Get-ChildItem -Path $sourcedir\$modelname
# rem Pause
Write-Host "Press any key"
$pause = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
# rem End ------
希望你得到它的工作。
来源
2015-02-10 05:22:09
dan
谢谢你有帮助的答案。首先是Get-ChildItem * .rpt和Get-Item。\ *。rpt之间的区别。我相信在这种情况下我可以使用这两种选择。其次,我应该在我的问题中说明这一点,我只想移动重命名的项目。 – tfitzhardinge 2015-02-10 02:59:00
因为它会得到所有* .RPT文件,试图重命名它们,并尝试移动重命名的文件。任何未重命名的文件都不会被触碰。为了更好地理解正在发生的事情,尝试将一些示例文件复制到工作目录中,将我发布到ISE中的脚本放入,使用F9添加断点并尝试单步执行脚本。为重命名和移动命令添加-whatif语句,看看它在做什么。 – StephenP 2015-02-10 04:21:26