|
Answer» Baruch to replace space in file name with underscore. OK,I already did it. But I was wondering how you would do it. Here is the problem.Some utilities s used for batch programming don't do well with spaces in file names. Unless you put quotes around everything. For me,I would rather have underscores and forget about the nice way spaces look in file names. Just a PERSONAL quirk. So I renamed any file-name with a space to have a underscore character instead. The method took me about an hour while working with another problem. But I only had 25 file names. It should not have taken that long. What would I doif EVER do if I had 200 names with spaces? So, my question is : How would you rename all files with space in the name to have only an underscore a dash instead? Some sample names in a single directory::
Code: [Select]My Dog and Cat.txt New Phone .txt NoSpaceHere.txt west sunset.txt Lost and found from BACK yard.txtAnybody? You could do it with something like a Python script but are you asking for a batch file to do it? What's a Baruch? Might be a way in batch but it would be complicated.
Code: [Select]import os import string
path = r'.'
for name in os.listdir(path): if os.path.isfile(os.path.JOIN(path, name)): if string.find(name, ' ') > 0 : fnm = name.split(' ') fname = '_'.join(fnm) os.rename(name, fname) Baruch? Not sure what that was. I have lost my eye teeth, do I can not see what I say.
Thanks for the Python script. I will add that to my collection. Very simple in batch.
Code: [Select]echo off setlocal enabledelayedexpansion for /f "delims=" %%A in ('dir /b ^| find " "') do ( set oldname=%%A set newname=!oldname: =_! echo Renaming "!oldname!" to "!newname!" ren "!oldname!" "!newname!" ) echo Done Pause
|