Computing.Net > Forums > Programming > Batch file to rename without spaces

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file to rename without spaces

Reply to Message Icon

Name: WeaselNo7
Date: October 7, 2004 at 07:41:38 Pacific
OS: XP Home SP2
CPU/Ram: P4/512Mb
Comment:

Ello!

Am wondering if you guys can help... I've managed pieces of this problems, but not the whole thing...

I need to make a batch script that renames all files in a given folder (passed at param to script, folders can have spaces in the names!), so that the files have any spaces in the names replaced with underscores.

Problems I came across:
-I don't want to rename the directories the files are in too!
-Files can have multiple spaces in the names.
-Files can have multiple CONSECUTIVE spaces in the names.

Tricksy one... can anyone help?


Thanks!


Sam.




Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: October 7, 2004 at 08:33:55 Pacific
Reply:

No trickies... only a good knowledge of NT scripting

@Echo Off

Set Folder=%*
For /F "tokens=*" %%A in ('Dir "%*" /B /A-D') Do Call :REN %%A
Set File=
Set FileNew=
GoTo :EOF

:REN
Set File=%*
Set FileNew=%File: =_%
If not "%File%"=="%FileNew%" Ren "%Folder%\%*" "%FileNew%"
GoTo :EOF

Note: For /F....Call :REN %%A fits just one line of code.

Save as XRen.bat then type Xren Folder_Name, e.g. XRen My Folder with Files and you are done ("My Folder with Files" is a folder name that holds spaces)



0

Response Number 2
Name: WeaselNo7
Date: October 8, 2004 at 07:24:11 Pacific
Reply:

That's bloody elegant! Nice work IVO!


0

Response Number 3
Name: Terry.Webb
Date: November 11, 2004 at 08:24:27 Pacific
Reply:

Hi, Also wanted something similar, however the %* parameter prefixes the parameters with a "space", which confuses the script (invalid directory and file names) - Using NT4.0 SP6.

Any ideas?

TIA

Terry


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file to rename without spaces

Batch file to rename file(s) in dir www.computing.net/answers/programming/batch-file-to-rename-files-in-dir/13357.html

Batch file to rename .csv field www.computing.net/answers/programming/batch-file-to-rename-csv-field/19279.html

Batch file to rename file www.computing.net/answers/programming/batch-file-to-rename-file/13945.html