Wikiposts
Search
Computer/Internet Issues & Troubleshooting Anyone with questions about the terribly complex world of computers or the internet should try here. NOT FOR REPORTING ISSUES WITH PPRuNe FORUMS! Please use the subforum "PPRuNe Problems or Queries."

Running a batch file under XP

Thread Tools
 
Search this Thread
 
Old 15th Oct 2005, 12:18
  #1 (permalink)  
Thread Starter
 
Join Date: Oct 1999
Location: Melbourne - Australia
Posts: 356
Likes: 0
Received 0 Likes on 0 Posts
Running a batch file under XP

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...
Lurk R is offline  
Old 15th Oct 2005, 14:48
  #2 (permalink)  
 
Join Date: Mar 2004
Location: Bournemouth, UK
Age: 53
Posts: 133
Likes: 0
Received 0 Likes on 0 Posts
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
Stoney X is offline  
Old 15th Oct 2005, 17:07
  #3 (permalink)  
Spoon PPRuNerist & Mad Inistrator
 
Join Date: Sep 2003
Location: Twickenham, home of rugby
Posts: 7,394
Received 251 Likes on 168 Posts
Lurk R

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

SD
Saab Dastard is offline  
Old 15th Oct 2005, 17:47
  #4 (permalink)  
 
Join Date: May 2001
Location: England
Posts: 1,904
Likes: 0
Received 0 Likes on 0 Posts
uh! uh! me! me! me! I know! I know................

You're batched file is called rename.bat

(batch files cannot have the same name as a command line program)
Superpilot is offline  
Old 15th Oct 2005, 20:14
  #5 (permalink)  
 
Join Date: Jul 2002
Location: Northampton UK
Posts: 537
Likes: 0
Received 0 Likes on 0 Posts
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
rotorcraig is offline  
Old 15th Oct 2005, 21:16
  #6 (permalink)  
 
Join Date: Mar 2004
Location: Bournemouth, UK
Age: 53
Posts: 133
Likes: 0
Received 0 Likes on 0 Posts
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?

Regards
Stoney
Stoney X is offline  
Old 16th Oct 2005, 06:32
  #7 (permalink)  
Thread Starter
 
Join Date: Oct 1999
Location: Melbourne - Australia
Posts: 356
Likes: 0
Received 0 Likes on 0 Posts
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...
Lurk R is offline  
Old 16th Oct 2005, 07:18
  #8 (permalink)  

Plastic PPRuNer
 
Join Date: Sep 2000
Location: Cape Town
Posts: 1,898
Received 0 Likes on 0 Posts
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/
Mac the Knife is offline  
Old 16th Oct 2005, 09:33
  #9 (permalink)  
 
Join Date: May 2001
Location: England
Posts: 1,904
Likes: 0
Received 0 Likes on 0 Posts
Drat!

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.
Superpilot is offline  
Old 16th Oct 2005, 14:50
  #10 (permalink)  
 
Join Date: Sep 2002
Location: UK
Posts: 129
Likes: 0
Received 0 Likes on 0 Posts
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!

Last edited by shuttlebus; 16th Oct 2005 at 19:04.
shuttlebus is offline  
Old 16th Oct 2005, 18:37
  #11 (permalink)  
 
Join Date: Nov 2002
Location: 39N 77W
Posts: 1,630
Likes: 0
Received 0 Likes on 0 Posts
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...
seacue is offline  
Old 16th Oct 2005, 19:07
  #12 (permalink)  

Plastic PPRuNer
 
Join Date: Sep 2000
Location: Cape Town
Posts: 1,898
Received 0 Likes on 0 Posts
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

Last edited by Mac the Knife; 16th Oct 2005 at 19:22.
Mac the Knife is offline  
Old 16th Oct 2005, 19:38
  #13 (permalink)  
 
Join Date: Jul 2002
Location: Northampton UK
Posts: 537
Likes: 0
Received 0 Likes on 0 Posts
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.
rotorcraig is offline  
Old 17th Oct 2005, 17:23
  #14 (permalink)  
Spoon PPRuNerist & Mad Inistrator
 
Join Date: Sep 2003
Location: Twickenham, home of rugby
Posts: 7,394
Received 251 Likes on 168 Posts
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
Saab Dastard is offline  
Old 18th Oct 2005, 10:42
  #15 (permalink)  
Thread Starter
 
Join Date: Oct 1999
Location: Melbourne - Australia
Posts: 356
Likes: 0
Received 0 Likes on 0 Posts
Well... In a nutshell - mission accomplished!

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

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!
Lurk R is offline  
Old 18th Oct 2005, 12:48
  #16 (permalink)  
 
Join Date: Apr 2003
Location: Lincolnshire
Age: 64
Posts: 2,278
Received 36 Likes on 14 Posts
You could always cheat and use something like this
ZH875 is offline  
Old 18th Oct 2005, 19:31
  #17 (permalink)  
 
Join Date: Jul 2002
Location: Northampton UK
Posts: 537
Likes: 0
Received 0 Likes on 0 Posts
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

RC
rotorcraig is offline  

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Contact Us - Archive - Advertising - Cookie Policy - Privacy Statement - Terms of Service

Copyright © 2024 MH Sub I, LLC dba Internet Brands. All rights reserved. Use of this site indicates your consent to the Terms of Use.