【问题标题】:powershell copy and change the name of each copied file by an increasing counterpowershell 复制并通过递增计数器更改每个复制文件的名称
【发布时间】:2018-04-16 08:51:28
【问题描述】:

是否可以在 Powershell 中通过保持相同的文件扩展名将每个复制文件的名称更改为递增计数器,例如: 第一个选择的源文件:fileA.txt 将变为:file_001.txt, 第二个文件:fileB.doc 将变为:file_002.doc, 第三个会变成:file_003.txt 等等...

【问题讨论】:

  • 是的,有可能,您可以使用Rename-Item 重命名每个文件,然后创建一个for 循环来计算名称后面的数字
  • 你太不耐烦了。查看我对您最初问题的原始答案
  • @Olaf 他们的老板更有可能对“他们”正在编写的脚本不耐烦;-)

标签: powershell


【解决方案1】:

检查这个功能,看看它是否有效。

function Copy-EnumerateFiles {
    param(
        [ValidateScript({Test-Path $_})]
        $Source,
        [ValidateScript({Test-Path $_})]
        $Destination        
    )
    [int]$i = 1
    $Files = Gci -Path $Source    
        Foreach($File in $Files) {            
            Copy-item -Path "$($file.fullname)" -Destination $Destination
            Rename-Item -Path $Destination\$($file.Name) -NewName "$($file.BaseName)_$($i)$($file.Extension)"
            $i++           
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-16
    • 2021-12-20
    • 2013-03-24
    • 1970-01-01
    • 2022-10-17
    • 2017-05-18
    • 2021-06-12
    • 1970-01-01
    相关资源
    最近更新 更多