2015-02-09 132 views
1

我想重命名.rpt文件从dr_network到dr_network_10yr。然后创建文件夹输出(如果它不存在)并将文件移动到文件夹。重命名和移动文件Powershell

文件的重命名工作但无法移动文件。请注意文件应该是相对路径。

感谢您的协助。

New-Item .\Output -force 
Get-ChildItem *.rpt | 
    ForEach-Object{ 
     Rename-Item $_ ($_.Name -replace 'dr_network_','dr_network_10yr') 
     Move-Item $_($_.Fullname -destination ".\Output") 
} 

回答

3

你的例子不起作用的原因有几个。你需要指定一个类型的新建项目

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" 
} 
+0

谢谢你有帮助的答案。首先是Get-ChildItem * .rpt和Get-Item。\ *。rpt之间的区别。我相信在这种情况下我可以使用这两种选择。其次,我应该在我的问题中说明这一点,我只想移动重命名的项目。 – tfitzhardinge 2015-02-10 02:59:00

+0

因为它会得到所有* .RPT文件,试图重命名它们,并尝试移动重命名的文件。任何未重命名的文件都不会被触碰。为了更好地理解正在发生的事情,尝试将一些示例文件复制到工作目录中,将我发布到ISE中的脚本放入,使用F9添加断点并尝试单步执行脚本。为重命名和移动命令添加-whatif语句,看看它在做什么。 – StephenP 2015-02-10 04:21:26

0

我从您的其他职位,这有助于理解帽子你正在尝试做以下:

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 ------ 

希望你得到它的工作。

+0

@StephenP rpt文件是用rpt文件扩展名重命名的简单txt文件。任何文本编辑器都可以轻松打开它。所以在必须的编程语言中,您可以轻松打开,编辑,删除和关闭这些文件。他们没有什么特别的。你们(或者其他人知道Get-Item和Get-ChildItem是不同的,不过我在这里得到的是Get-ChildItem用于本地目录中的文件和文件夹,Get-Item只与文件相关。对吗? – tfitzhardinge 2015-02-10 23:56:39

+0

@tfitzhardinge简介: Get-Item获取指定位置的项目(文件或目录),它不会**获取该位置的项目内容(除非使用通配符(*),请求该项目的所有内容) GET-Childitem获取项和子项中的一个或多个指定位置 例如:。 获取-C项:(只返回C:目录) 获取-Item c:\ *(返回c:文件和文件夹) Get-ChildItem(返回c:文件和d文件夹) [link](https://technet.microsoft.com/en-us/library/hh849800.aspx) [link](https://technet.microsoft.com/en-us/library/ hh849788。ASPX) – dan 2015-02-11 01:37:22