重命名和移动文件 Powershell

3
我希望将.rpt文件从dr_network重命名为dr_network_10yr。然后创建Output文件夹(如果不存在),并将文件移动到该文件夹中。 文件重命名可以正常工作,但无法移动文件。请注意,文件应该采用相对路径。 感谢您的协助。
New-Item .\Output -force
Get-ChildItem *.rpt | 
    ForEach-Object{
        Rename-Item $_ ($_.Name -replace 'dr_network_','dr_network_10yr')
        Move-Item $_($_.Fullname -destination ".\Output")
}
2个回答

6

您的示例由于几个原因而无法工作。您需要在New-Item上指定一种类型。

New-Item .\Output -force -ItemType Directory

接下来您需要获取所有的*.rpt文件并对它们进行迭代。重命名语法正确,但是移动语法存在问题。Powershell不知道您试图做什么。您还将一个文件重命名后尝试移动一个不存在的文件。以下操作可能会有所帮助:

#Tell powershell its a directory
New-Item .\Output -force -ItemType Directory
Get-ChildItem *.rpt | 
    ForEach-Object{
        #store the new name as a variable
        $newName = $_.FullName -replace 'dr_network_','dr_network_10yr'
        #rename the file
        Rename-Item $_ $newName
        #move the newly renamed file to the Output folder
        Move-Item $newName -destination ".\Output"
}

谢谢你的帮助。首先,Get-ChildItem *.rpt 和 Get-Item .*.rpt 之间有什么区别?我相信在这种情况下我可以使用两个选项。其次,我应该在我的问题中明确说明,我只想移动那些被重命名的项目。 - undefined
这样做将获取所有的*.RPT文件,尝试重命名它们,并尝试移动已重命名的文件。任何未被重命名的文件都不会被触及。为了更好地理解正在发生的情况,请尝试将一些示例文件复制到工作目录中,将我发布的脚本添加到ISE中,并使用F9添加断点,然后尝试逐步执行脚本。在重命名和移动命令中添加-whatif语句,以查看它具体在做什么。 - undefined

1

我在你的另一篇帖子中关注了你,这有助于理解你想做什么:

将文本插入文件名中间的命令

我已经修改了我的代码,使其能够按照您的要求进行操作。它只会移动和重命名以“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 ------

希望你能使它工作。

@StephenP rpt文件是简单的文本文件,只是将其重命名为rpt文件扩展名。任何文本编辑器都可以轻松打开它。因此,在大多数编程语言中,您可以轻松地打开、编辑、删除和关闭这些文件。没有什么特别的。你们(或其他人)知道Get-Item和Get-ChildItem之间的区别吗?我猜测Get-ChildItem用于本地目录中的文件和文件夹,而Get-Item仅与文件相关。这样对吗? - undefined
在简述中:Get-Item 获取指定位置的项目(文件或目录)。它不会获取指定位置项目的内容。(除非您使用通配符字符(*)请求所有项目的内容。)Get-ChildItem 获取一个或多个指定位置的项目和子项。示例: Get-Item c: (只返回c:目录) Get-Item c:* (返回c:文件和文件夹) Get-ChildItem (返回c:文件和文件夹)链接 链接 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接