【问题标题】:Renaming and Moving Files Powershell重命名和移动文件 Powershell
【发布时间】:2015-02-09 03:06:36
【问题描述】:

我想将 .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")
}

【问题讨论】:

    标签: powershell


    【解决方案1】:

    由于多种原因,您的示例不起作用。您需要在 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 之间的区别。我相信在这种情况下我可以同时使用这两个选项。其次,我应该在我的问题中说明这一点,我只想移动获得重命名的项目。
    • 这样会获取所有 *.RPT 文件,尝试重命名它们,并尝试移动重命名的文件。任何未重命名的文件都不会被触及。为了更好地了解正在发生的事情,尝试将一些示例文件复制到工作目录,将我发布的脚本放入 ISE,使用 F9 添加断点并尝试单步执行脚本。将 -whatif 语句添加到 rename 和 move 命令中,看看它在做什么。
    【解决方案2】:

    我正在关注您的其他帖子,这有助于了解您正在尝试做的事情:

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

    希望你能成功。

    【讨论】:

    • @StephenP rpt 文件是使用 rpt 文件扩展名重命名的简单 txt 文件。任何文本编辑器都可以轻松打开它。因此,在必须的编程语言中,您可以轻松地打开、编辑、删除和关闭文件。他们没什么特别的。你们(或其他任何人都知道Get-Item和Get-ChildItem之间的区别。我猜这里是用于本地目录中的文件和文件夹的Get-ChildItem,而Get-Item仅与文件有关。这是对吗?
    • @tfitzhardinge 简介:Get-Item 获取指定位置的项目(文件或目录)。它在该位置获取项目的内容。 (除非您使用通配符 (*) 来请求项目的所有内容。) Get-Childitem 获取一个或多个指定位置的项目和子项目。示例:Get-Item c:(仅返回 c: 目录) Get-Item c:*(返回 c: 文件和文件夹) Get-ChildItem(返回 c: 文件和文件夹)linklink
    猜你喜欢
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    相关资源
    最近更新 更多