Computing.Net > Forums > Programming > batch IF function

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 IF function

Reply to Message Icon

Name: batchlover
Date: January 13, 2008 at 14:04:35 Pacific
OS: XP 5.1.2600 SP2
CPU/Ram: pentium xeon 3.06ghz 2048
Product: dell pws450
Comment:

I am simply making an application now which shows you a list of files in a directory and then you can type in certain details and it will do the job for you. My problem is, whenever the code reaches the first "if" line, the damn thing just closes up. What's the problem? Here is my code so far :
@echo off
dir
echo file or folder ?
set /p source=
echo is it a folder or a file ?
set /p sourcetype=
echo delete or copy ?
set /p function=
if %function%=copy echo copy to where ?
set /p dest=
if %function%=delete set %function%=del
if %sourcetype%=file %function% %source%
if %sourcetype%=folder set %function%=rmdir /q /s
%function% "%source%"
if %function%=copy
if %sourcetype%=file
if not exist %dest% mkdir "%dest%"
else %function% "%source%" "%dest%"
if exist %dest% %function% "%source%" "%dest%"
else
if %sourcetype%=folder %function%=xcopy /e /h /r /k /x /y
%function% "%source%" "%dest%"
pause



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: January 13, 2008 at 15:28:07 Pacific
Reply:

You say "the damn thing just closes up." Do you mean you are running this batch file by double-clicking its name in Explorer? My tip is not to do that while you're developing the batch file. The window will close before you have time to read any error message that may appear.

If you run it from a Command Prompt, you will see the error message:

=copy was unexpected at this time.

This gives you a clue where the error is. Looks like a syntax error, so type IF /? to check how you are supposed to use the IF command.


0

Response Number 2
Name: Mechanix2Go
Date: January 13, 2008 at 16:55:59 Pacific
Reply:

To set a var, use SINGLE = like this:

set var=a

To test for equality, use DOUBLE = like this:

if %var%==a echo yes


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 3
Name: batchlover
Date: January 14, 2008 at 04:15:30 Pacific
Reply:

ah i have found out why - you were both right lol - the syntax is incorrect :P
i was doing this :
if something=something function
while i was supposed to be doing this :
if something==something function
i forgot the 2 ='s lol
thanks for ur help :)
batchlover


Batch is swift.... like a ninja! Be very afraid......


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 IF function

Batch: Copy most recent directory.. www.computing.net/answers/programming/batch-copy-most-recent-directory/13198.html

Batch - if [not] exist www.computing.net/answers/programming/batch-if-not-exist/11906.html

Batch IF EXIST ... ELSE www.computing.net/answers/programming/batch-if-exist-else/15828.html