Batch file required to rename files in folders

Asked By 0 points N/A Posted on -
qa-featured

I have many files with spaces and underscores in the file names which is acceptable by windows. But for an application i am using it cannot tolerate any spaces or special character in the file names. Hence, I am currently manually updating all the file names. But it looks like it is going to take a long time and hence wanted to check for help here. Can anybody share a script or batch file to read all the files in a folder and if the file name has space or special character those have to be removed. Thank you for helping

SHARE
Answered By 0 points N/A #193965

Batch file required to rename files in folders

qa-featured

Use PowerShell to do anything smarter for a DOS prompt. Here, I've shown how to batch rename all the files and directories in the current directory that contain spaces by replacing them with _underscores.

Dir |

Rename-Item -NewName { $_.Name -replace " ","_" }

Optionally, the Where-Object command can be used to filter out ineligible objects for the successivecmdlet (command-let). The following are some examples to illustrate the flexibility it can afford you:

• To skip any document files

Dir |

Where-Object { $_.Name -notmatch ".(doc|xls|ppt)x?$" } | Rename-Item -NewName { $_.Name -replace " ","_" }

• To process only directories (pre-3.0 version)

Dir |

Where-Object { $_.Mode -match "^d" } | Rename-Item -NewName { $_.Name -replace " ","_" } PowerShell v3.0 introduced new Dir flags. You can also use Dir -Directory there.

• To skip any files already containing an underscore (or some other character)

Dir |

Where-Object { -not $_.Name.Contains("_") } | Rename-Item -NewName { $_.Name -replace " ","_" }

Related Questions