简体   繁体   English

使用Powershell重命名文件

[英]Renaming files using powershell

i have files in following format: Starts with 400 or Z400 我有以下格式的文件:以400或Z400开头

  • 40011111.mus 40011111.mus
  • 40011112.mus 40011112.mus
  • Z40011112.mus Z40011112.mus

I need to rename as following: Starts with Z100 我需要重命名如下:以Z100开头

  • Z10011111.mus Z10011111.mus
  • Z10011112.mus Z10011112.mus
  • Z10011112.mus Z10011112.mus

Currently i have the following powershell function. 目前,我具有以下Powershell功能。 but it adds Z as prefix. 但会添加Z作为前缀。 how do i undate the code so that it adds Z when necessary and changes 400 to 100? 我如何取消代码日期,以便在必要时添加Z并将400更改为100?

function renameFiles{
    echo " Start Rename Files" 
    get-childitem -path $mapfolder | 
        Where-Object {$_.extension -eq ".ditamap"} | 
        foreach-object {
           echo "  Renaming: $_ --> Z$_"
           ren "$mapfolder\$_" "$mapfolder\Z$_"
    }
}

不要使用echo而是要使用某种东西,例如ifswitch来处理各种情况:

function Rename-DitmapFile { get-childitem -path $mapfolder -Filter *.ditmap | foreach-object { switch ($_) { {$_.name.startswith('4')} {Rename-Item -Path $_.FullName -NewName ($_.Name -replace '4','Z1')} {$_.name.startswith('Z4')} {Rename-Item -Path $_.FullName -NewName ($_.Name -replace 'Z4','Z1')} } } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM