PDA

View Full Version : Running a batch file under XP


Lurk R
15th Oct 2005, 12:18
Background:

I have about 20 folders - each has about 30 files that I need to change the file type fairly regularly. Rather than do it via Windows Explorer, I just open a DOS command window and type:

rename c: \folder1\*.xls *.csv

It changes all the Excel files into .csv files but keeps the same file name. I then re-type the command for the other 19 folders and the job is done.

It occurred to me the other day that writing and running a batch file would be a lot faster. So I wrote the following:

rename c: \folder1\*.xls *.csv
rename c: \folder2\*.xls *.csv
rename c: \folder3\*.xls *.csv

etc.

rename c: \folder20\*.xls *.csv
exit


But when I doubleclick the batch file, a DOS window pops up, text scrolls at a zillion miles per hour and the window closes. When I check the folders, none of the files have changed name.


Does anyone have any clues what I might be doing wrong? Or if there is a better way of achieving the same thing? And no - buying a Mac is not an option!!!

Thanks in advance...

Stoney X
15th Oct 2005, 14:48
If you run the batch file from within an MS-DOS window, you should see what is going on, i.e. any error messages will remain displayed. I've got similar batch files that run fine when double-clicked in Explorer in Windows XP.

Regards
Stoney

Saab Dastard
15th Oct 2005, 17:07
Lurk R

Ummm - don't leave a space between C: and \folderN ?

SD

Superpilot
15th Oct 2005, 17:47
uh! uh! me! me! me! I know! I know................

You're batched file is called rename.bat :E

(batch files cannot have the same name as a command line program)

rotorcraig
15th Oct 2005, 20:14
I think that one of the above suggestions (no space or rename.bat) will prove to be the cause.

But failing that, add the following line to your batch file before the "exit"

Pause

This will leave the output on the screen until you press a key, so you can see what's going on

RC

Stoney X
15th Oct 2005, 21:16
I think you will find that Lurk R deliberatley put a space in c: \ otherwise you get c:\ smilie. Superpilot, if your sugestion is correct can I pass on the wooden spoon I've had since last I did that? :O

Regards
Stoney

Lurk R
16th Oct 2005, 06:32
Yes, I deliberately added a space between the C: and \ on the forum here to prevent getting that absurd emoticon!

The batch files were rather unimaginatively called do.bat and undo.bat - not rename.bat

I'll try that pause thing - if errors are being generated I'd love to know what it's saying!

Thanks all...

Mac the Knife
16th Oct 2005, 07:18
Batch files work very well under XP, there are several useful extra commands over DOS.

The rules are occasionally a bit different and the parsing mechanism isn't completely consistent with DOS, but they're still pretty similar.

Beware of the difference between "command" and "cmd" - "CMD.exe is the NT/XP equivalent of Command.com in previous operating systems. The older 16 bit command processor command.com is supplied to provide backward compatibility for 16 bit DOS applications. e.g. command.com will fail to set %errorlevel% after certain commands.

To ensure that a batch file will not run if accidentally copied to a Windows 95/98 machine you should use the extension .CMD rather than .BAT

The COMSPEC environment variable will show if you are running CMD.EXE or command.com"

Remove the finalk EXIT from your file and run it in a command window - you should then be able to see any errors generated.

Batch files are fun!

PS: For real power consider the REXX scripting language - http://regina-rexx.sourceforge.net/

Superpilot
16th Oct 2005, 09:33
Drat! :ugh:

Stoney, hold on to the spoon for the mo mate! ;)

Lurk R, confirm that the lines:

rename c: \folder1\*.xls *.csv
rename c: \folder2\*.xls *.csv
rename c: \folder3\*.xls *.csv
<repeat for all folders>

....are all that exists in the batch file and nothing else. I just tried it and it worked for me without problems.

shuttlebus
16th Oct 2005, 14:50
Lurk R,

Are you running your batch file in the same folder as the files you are trying to modify?

From memory, XP runs batch files in the default user directory (usually C:\documents and settings\username

Two ways round this....

1) Run the batch file in the directory with the files you want to modify
2) Add a path= statement to the top of the batch file

i.e. path=c:\myfiles

I'll have a play with DOS at home later to see if this cures it.

Regards,

Shuttlebus

_____________________
Edited for spelling and emoticons... gurr!

seacue
16th Oct 2005, 18:37
If I were to do your task, I would have added some steps for clarity and conservatism. You might find your renamed files in whatever folder you were using when you called this bat file.

I think you are trying to same too much typing.

I would have said:

cd c: \folder1 (space only for anti emoticon purposes)
ren *.xls *.csv
cd: \folder2
ren *.xls *.csv
cd: \folder3
ren *.xls *.csv

The above approach makes it absolutely clear where the files you are tampering with are located.

See if this helps.

seacue

Oops...shuttlebus seems to have got there first...

Mac the Knife
16th Oct 2005, 19:07
Oh alright then...

cd myfolder
for /D %%a in (folder1 folder2 folder3) do ren *.xls *.csv

and

cd myfolder
for /r %%a in (myfolder) do ren *xls *.csv

might work too

rotorcraig
16th Oct 2005, 19:38
cd c: \folder1 (space only for anti emoticon purposes)
ren *.xls *.csv
Take care - that only works if the default user directory is on drive c:

If the default user directory is d: \somewhere then cd c: \folder1 will tell the OS to change it's logical pointer on c: to \folder1 but your ren *.xls *.csv will try to take effect in d: \somewhere

So you would actually need to include

c:
cd \folder1
ren *.xls *.csv

But given that the original ren c: \folder1\*.xls *.csv was explicit it's better IMHO. With an explicit path in the command the default user directory is irrelevant.

Saab Dastard
17th Oct 2005, 17:23
LurkR,

The problem is almost certainly the "Exit" command.

Remove it and all should be well - Mac mentioned it already.

Shuttlebus & Seacue - you both need to brush up on elementary directory structures!

(Mounts soap box)

The command (batch file) can be located anywhere if the location of the target file is EXPLICIT, i.e. C: \myfiles\file1.xls (absolute path). In this situation there is no possibility of any file(s) other than the desired target being modified IRRESPECTIVE of wherever the batch file is located.

With an implicit or relative file location, e.g. file1.xls or \myfiles\file1.xls, the location of the file is assumed by the command interpreter to be relative to the current directory. So if the command states a relative path, The relationship of the command and target would need to be in the same relation, if not the same directory.

It is perfectly possible to have all batch files in a single directory (it used to be good practice to do so), totally separate from the data upon which they operate.

Calling the batch file could be done from anywhere in the directory by invoking the absolute path to it, with the commands in the batch file referencing absolute or relative paths as appropriate to their function.

BTW, it is incorrect to say that commands are executed in a particular directory (commands are not executed in ANY directory) - it is true that the default current directory for the CMD interpreter is C: \documents and settings\username.

(Dismounts soap box).

SD

Lurk R
18th Oct 2005, 10:42
Well... In a nutshell - mission accomplished!

Pretty much solved with a combination of tips from above so thanks all. :D

Firstly, I added the "PAUSE" line to the file. This helped because it kept the DOS box on the screen which allowed me to see "The Command Syntax Is Incorrect" (or similar) after each step. I fiddled around a little by adding the full path to the renamed file but it didn't seem to help at all.

I then tried what ultimately seems to be a combination of what Shuttlebus and Seacue proposed. By pointing the cursor to the appropriate directory with a cd c: \folder 1 command line followed by a rename *.xls *.csv, this seemed to work.

Saab - I know what you mean about where the batch file is actually located. My 2 files were in separate directories to where all the Excel files were. However I kept the EXIT line in the file and it all worked still - didn't really seem to make any difference. Maybe because I put PAUSE ahead of it, it never really got to the EXIT step???


Anyway - thanks to the communal brainpower here. Combining a number of tips achieved the desired result so once again, big thanks to all!

ZH875
18th Oct 2005, 12:48
You could always cheat and use something like this (http://www.1-4a.com/rename/)

rotorcraig
18th Oct 2005, 19:31
Good news that you've sorted it.

Doesn't quite stack up in my mind however ... think that there was indeed something syntactically incorrect with your rename command which is no longer the case with the simplified version.

Just experimented and found that if you put rename c:/whatever... in a batch file rather than rename c:\ whatever... then you do indeed get The syntax of the command is incorrect. And that would be eliminated in your simplier version.

Could that have been it :confused:

RC