Windows batch script to replace spaces with underscores
Windows batch script to replace spaces with underscores
02 March, 2019
We regularly share some of the scripts that our IT technicians and support staff puts together (or finds online), especially if we think it may be useful to someone. Below is an example of a windows script (batch) that replaces file names with spaces to underscores. (For example: "file name.txt" will be changed to "file_name.txt")
The script recursively goes through the directories and makes adjustments.
For our particular IT task the script was used on .txt files (we we automatically manipulating files on a Linux box, coming from Windows, so they had to be free of spacing in files for successful copy), but it can be used on any: just replace *.txt with any other extension (or simply *.* for any files):
SET location=C:\TestDir
for /R %location% %%A in (*.txt) docall :repl "%%A"goto :eof
:repl
set"_fn=%~nx1"
ren %1"%_fn: =_%"