0

i have files in following format: Starts with 400 or Z400

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

I need to rename as following: Starts with Z100

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

Currently i have the following powershell function. but it adds Z as prefix. how do i undate the code so that it adds Z when necessary and changes 400 to 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$_"
    }
}

1 Answer 1

1

Don't use echo and look at using something, like if or switch to handle the various situations:

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')} } } }

Sign up to request clarification or add additional context in comments.

1 Comment

It is not working if the file name is 40014001.mus. It is changed to Z10011001

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.