windowsbatch-filebatch-rename

Write a batch file to rename files within multiple folders


I am trying to write a batch file to rename many files with a # sign in the filename to be replaced with No..

i.e. *#*.* to *No.*.* Examples are GT #3.doc to GT No. 3.doc all files have differing file extensions. i.e. .txt, .doc, .docx, .xls, .dat, .mdb, etc...

Also, these files are not in one folder but are in many folders and sub-folders.

OS is Win 7. I look forward to the help as I am a novice with batch files and there are hundreds of files to be renamed for moving to a cloud solution that does not accept symbols. Thank you.


Solution

  • I forgot how to use batch few years ago ever since powershell came out. Powershell has an easy way of doing this.

    $target = "Your Path"
    Get-ChildItem -path $target -Recurse -Include *#* | rename-item -NewName { $_.name -replace '#','NO'}
    

    -recurse will go through all folders and -include will filter files with # in it and pipe it to rename-item. Rename-item has a replace function that only replaces specified part with whatever you'd like.