Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I sure could use a batch file to modify the filenames of all the files in a folder. I need to replace the 'underlines' with 'spaces'. I would really appreciate it if someone would help me out?

Sorry about that! I found out that I have a renamer program to do it.
However if someone wants to bother to create a batch file to do it, I could use it and maybe learn something.

@echo off
setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
set old=%%a
set new=!old:_= !
echo ren !old! !new!
)
=====================================
If at first you don't succeed, you're about average.M2

Thanks very much Mechanix2Go!!!
I'll use it instead of the renamer program. I've downloaded a XP Batch tutorial. I'm gonna study that code and see if I can't learn some more about batch programming.

I'd just like to point out a minor problem, when you have spaces in a file it should be enclosed in quotes, so the above should be:
ren !old! "!new!"
(In fact you may even need to enclose !old! in quotes too, just in case there are spaces in the old filename even though it uses underlines.)

Thanks very much Klint. That helps a lot. I'm an old DOS guy with very minimal batch knowledge. I'm amazed at how sophisticated it has become.

Hi klint,
Exactly right. I'm always thinking of 8.3 names. LOL
=====================================
If at first you don't succeed, you're about average.M2

First I want to thank you both for helping me out. I ran into one 'strange' problem. The routine seemed to work perfectly fine. It did output the rename commands correctly, but the system seemed to ignore them. I finally tried changing the:
echo ren "!old!" "!new!"
to
ren "!old!" "!new!"and then it worked.
Anyway, thanks very much! It's a lot easier than using the renamer program.

The echo command was used purely for testing prior to deployment. Commands such as REN can cause major problems if used incorrectly, and therefore it's a good idea to add "echo" in front of them when you first try your batch file so that it tells you what it would do, without actually doing it. When you've checked that it does the right thing, you simply remove the ECHO, just as you've done.

Thanks again Klint. I should have thought of that, but I'm a senile old (78) man and I'm pretty slow. I'll 'try' and remember that excellent tip.
Can I ask you just 'one' more (I promise) question? How do you determine if a directory is empty? I have tried 'if exist file' and it doesn't seem to work right under XP.
I'm trying to search a bunch of sub folders to see if any files have been added. I do a dir on each folder and get the new files displayed, but the problem is that I get a 'File not found' line for each empty folder. I was hoping for a way to determine if the folder is empty before doing the dir.

![]() |
Batch file. Extract singl...
|
get current folder name i...
|

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