Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重命名和移动文件Powershell_Powershell - Fatal编程技术网

重命名和移动文件Powershell

重命名和移动文件Powershell,powershell,Powershell,我想将.rpt文件从dr_network重命名为dr_network_10yr。然后创建文件夹输出(如果不存在),并将文件移动到该文件夹 重命名文件可以工作,但无法移动文件。注意,文件应该是相对路径 # 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 ru

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

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

# 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 ------
谢谢你的帮助

新项目。\n输出-强制 获取ChildItem*.rpt| ForEach对象{ 重命名项目$\u$\ uu.Name-替换“dr\u网络”、“dr\u网络\u 10yr” 移动项目$\u$\ Fullname-目标。\Output }
由于几个原因,您的示例不起作用。您需要在新项目上指定类型

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

由于几个原因,您的示例不起作用。您需要在新项目上指定类型

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

以下是您的另一篇帖子,这有助于理解您正在尝试做的事情:

我已经修改了我的代码,以满足您的要求。它仅在开始时移动和重命名带有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 ------

希望你能让它工作。

我从你的另一篇文章中看到了以下内容,这有助于理解你想做的事情:

我已经修改了我的代码,以满足您的要求。它仅在开始时移动和重命名带有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 ------

希望你能让它工作。

谢谢你的回答。首先是Get-ChildItem*.rpt和Get-Item.*.rpt之间的区别。我相信我可以在这种情况下使用这两种选择。第二,我应该在我的问题中说明这一点,我只想移动获得重命名的项目。因为这将获得所有*.RPT文件,尝试重命名它们,并尝试移动重命名的文件。任何未重命名的文件都不会被触摸。为了更好地理解正在发生的事情,请尝试将一些示例文件复制到工作目录中,将我发布的脚本放入ISE中,使用F9添加断点,然后尝试单步执行脚本。将-whatif语句添加到rename和move命令中,并查看它到底在做什么。感谢您提供了有帮助的答案。首先是Get-ChildItem*.rpt和Get-Item.*.rpt之间的区别。我相信我可以在这种情况下使用这两种选择。第二,我应该在我的问题中说明这一点,我只想移动获得重命名的项目。因为这将获得所有*.RPT文件,尝试重命名它们,并尝试移动重命名的文件。任何未重命名的文件都不会被触摸。为了更好地理解正在发生的事情,请尝试将一些示例文件复制到工作目录中,将我发布的脚本放入ISE中,使用F9添加断点,然后尝试单步执行脚本。将-whatif语句添加到rename和move命令中,并查看其具体操作。@StephenP rpt文件是简单的txt文件,以rpt文件扩展名重命名。任何文本编辑器都可以轻松打开它。因此,在必须的编程语言中,您可以轻松地打开、编辑、删除和关闭文件。他们没什么特别的。你们知道Get Item和Get ChildItem的区别吗。我在这里留言,但是Get-ChildItem用于本地目录中的文件和文件夹,Get-Item仅与文件相关。是否正确?@tfitzhardinge简言之:Get Item在指定位置获取项文件或目录。它不会在该位置获取项目的内容。除非使用通配符*请求项目的所有内容。Get Childitem获取一个或多个指定位置中的项和子项。示例:Get Item c:仅返回c:目录Get Item c:*返回c:文件和文件夹Get ChildItem返回c:文件和文件夹@StephenP rpt文件是使用rpt文件扩展名重命名的简单txt文件。任何文本编辑器都可以轻松打开它。因此,在必须的编程语言中,您可以轻松地打开、编辑、删除和关闭文件。他们没什么特别的。你们知道Get Item和Get ChildItem的区别吗。我在这里留言,但是Get-ChildItem用于本地目录中的文件和文件夹,Get-Item仅与文件相关。是这样吗?@Tfizzhardinge i n简介:Get Item在指定位置获取项文件或目录。它不会在该位置获取项目的内容。除非使用通配符*请求项目的所有内容。Get Childitem获取一个或多个指定位置中的项和子项。示例:Get Item c:仅返回c:目录Get Item c:*返回c:文件和文件夹Get ChildItem返回c:文件和文件夹