Friday, July 31, 2009

Batch File Help!!?

I made a batch file to copy a file from one place to another but it keeps saying that the file does not exist!


But, if I go to the file through CMD and type "dir" I see it. I'm thinking that my syntax is wrong. This is my Batch File:


::This file was created to move the .class files


::from their original folders to the jdk folder


::for testing.





@echo off





IF EXIST %1 GOTO :success


IF NOT EXIST %1 GOTO :fail





:file_transfer


copy %1 C:\Program Files\Java\jdk1.6.0\bin\newjava.class


GOTO :end





:success


echo File found...beginning transfer


GOTO :file_transfer





:fail


echo File not found!


GOTO :final








:end


echo File Transfer completed.





:final


REM This is just the end of the file.





Okay os that didn't work so I tried going into CMD and typing:


copy C:\Program Files\Java\jdk1.6.0\bin\workspace\HelloW... C:\Program Files\Java\jdk1.6.0\bin





So that isn't working etheir. HELP!!





oh and I also wanted to know how to take input in a batch file and store it as a variable.

Batch File Help!!?
I think you have to put qoutes around the the statement if it has spaces in the name.





I.E. "copy c:\program files\java...."
Reply:Your peroblem is this line:


copy %1 C:\Program Files\Java\jdk1.6.0\bin\newjav...





The delimiter in the CSI (command string interpreter) is a space not enclosed in quotes, so you're copying the file to C:\Program, which probably doesn't exist. (If %1 is in the same form - a path with a space - you'll have the same problem at the first space.)





Gotos are just fine in batch files - you don't have much choice if what you're doing isn't linear (if it has to make choices).
Reply:You've just shown the world why GOTO's are horrid and a very bad practice. Granted, this is a very simple script, but why don't you try Perl? It's free and far more powerful.





In any case, if you needed to store a variable, which shouldn't be necessary in this case, just use the SET command to store it as a local environment variable. I would make sure that a) you delete it when you're done and b) you don't overwrite an existing environment variable.
Reply:you have to put quotes around the statement


No comments:

Post a Comment