Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
lienlee
- Posts: 3
- Joined: 09 Jun 2011 08:35
#1
Post
by lienlee » 27 Jun 2011 06:04
Hi Guys,
I have the following code here but not sure why isnt it working..Please note that is the format. I believe there is an error for having it too long?
Please also note that "&Q192&" etc. are excel cells
If someone can help me fix the following I would appreciate it =)
Code: Select all
set src="&Q192&"
& set dest="&R192&" &
if not exist "%src%"(if not exist "%dest%"
(echo.FAIL Source: %src% and Destination: %dest% not found>>log.txt)
else (echo.FAIL Source: %src% not found>>log.txt))
else (if not exist
(echo.FAIL Destination: %dest% not found>>log.txt)
else
(echo.PASS Source: %src% Destination: %dest%>>log.txt
xcopy /f /s /y %src% %dest% >>log.txt &echo.>>log.txt))
-
dbenham
- Expert
- Posts: 2461
- Joined: 12 Feb 2011 21:02
- Location: United States (east coast)
#2
Post
by dbenham » 27 Jun 2011 08:03
Added after I posted - The following assumes your code is a normal batch file. The more I look at your example it looks like you are doing some weird hybrid batch/Excel thing, in which case all bets are off. I don't understand how you are going to reference Excel cells from batch.problem 1) The & is used to concatenate multiple statements on one line. You didn't need it on your first two lines, plus it was used incorrectly.
problem 2) IF ... ELSE ... must all be on one line unless you use ^ line continuation or (. If you use ( and ) then the parens must be on the same line as the ELSE.
Correct:
Code: Select all
if 1==1 (
rem do something
) else (
rem do something else
)
Incorrect:
Code: Select all
if 1==1 ( rem do something )
else ( rem do something else )
Here is your code corrected (untested)
Code: Select all
set src="&Q192&"
set dest="&R192&"
if not exist "%src%" (
if not exist "%dest%" (
echo.FAIL Source: %src% and Destination: %dest% not found>>log.txt
) else (
echo.FAIL Source: %src% not found>>log.txt
)
) else (
if not exist %dest% (
echo.FAIL Destination: %dest% not found>>log.txt
) else (
echo.PASS Source: %src% Destination: %dest%>>log.txt
xcopy /f /s /y %src% %dest% >>log.txt &echo.>>log.txt
)
)
You might consider the following just because you don't need to worry about all the parens (also untested):
Code: Select all
set src="&Q192&"
set dest="&R192&"
if not exist "%src%" if not exist "%dest%" echo.FAIL Source: %src% and Destination: %dest% not found>>log.txt
if not exist "%src%" if exist "%dest%" echo.FAIL Source: %src% not found>>log.txt
if exist "%src%" if not exist "%dest%" echo.FAIL Destination: %dest% not found>>log.txt
if exist "%src%" if exist "%dest%" (
echo.PASS Source: %src% Destination: %dest%>>log.txt
xcopy /f /s /y %src% %dest% >>log.txt &echo.>>log.txt
)
Dave Benham