what is wrong with IF else, please help me

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Kunal
Posts: 6
Joined: 01 Dec 2011 04:37

what is wrong with IF else, please help me

#1 Post by Kunal » 02 Dec 2011 07:54

Hello
I'm trying to check
if file already exist ,
then compare with another file and if any difference found
then move that new file to new destination
else
do nothing.
else
move that file to the new destination

as below
but my bat script doesn't do anything it simply vanish, can anyone assist me where I'm doing wrong?

Code: Select all

set lastCubeDate=20111201

IF EXIST P:\DataTest\CVATest\%lastCubeDate%_all.csv
(
fc P:\DataTest\CVATest\%year%%month%%day%_all.csv P:\DataTest\CVATest\%lastCubeDate%_all.csv | find "FC: no diff" > nul
IF %ERRORLEVEL%  EQU 1 (
cd P:\DataTest\CVATest
move P:\DataTest\CVATest\cvagui.csv P:\DataTest\%year%%month%%day%_all.csv
del P:\DataTest\CVATest\%lastCubeDate%_all.csv
)
 ELSE (
echo P:\DataTest\CVATest\%lastCubeDate%_all.csv not found.
move P:\DataTest\CVATest\cvagui.csv P:\DataTest\%year%%month%%day%_all.csv
))

jeb
Expert
Posts: 964
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: what is wrong with IF else, please help me

#2 Post by jeb » 02 Dec 2011 08:19

Hi Kunal,

the problem is the syntax.

The brackets have to be in the same line, for the IF and also for the ELSE.
And you need to use the DelayedExpansion inside of parenthesis blocks, as the percent expansion is done while parsing, the delayed by executing a line.

Code: Select all

setlocal EnableDelayedExpansion
set lastCubeDate=20111201

IF EXIST P:\DataTest\CVATest\%lastCubeDate%_all.csv (
  fc P:\DataTest\CVATest\%year%%month%%day%_all.csv P:\DataTest\CVATest\%lastCubeDate%_all.csv | find "FC: no diff" > nul
  IF !ERRORLEVEL!  EQU 1 (
    cd P:\DataTest\CVATest
    move P:\DataTest\CVATest\cvagui.csv P:\DataTest\%year%%month%%day%_all.csv
    del P:\DataTest\CVATest\%lastCubeDate%_all.csv
) ELSE (
  echo P:\DataTest\CVATest\%lastCubeDate%_all.csv not found.
  move P:\DataTest\CVATest\cvagui.csv P:\DataTest\%year%%month%%day%_all.csv
))


jeb

Kunal
Posts: 6
Joined: 01 Dec 2011 04:37

Re: what is wrong with IF else, please help me

#3 Post by Kunal » 02 Dec 2011 09:54

still not working , even I put the bracket on the same line, and added EnableDelayedExpansion , still no hope. any suggestions ??? It couldbe I'm doing somewhere wrong, but not able to debug..can I put some debug lines in this piece of code.

OJBakker
Expert
Posts: 29
Joined: 12 Aug 2011 13:57

Re: what is wrong with IF else, please help me

#4 Post by OJBakker » 02 Dec 2011 10:25

A minor syntax problem might be the problem.
change EQU to EQL

Kunal
Posts: 6
Joined: 01 Dec 2011 04:37

Re: what is wrong with IF else, please help me

#5 Post by Kunal » 02 Dec 2011 10:55

not working either.
let me explain what exactly I'm doing

I have to FTP a file as CVAGUI.zip from remote server and uncompress it at local server and then compare with any previous date uncompresed file if , there is any difference in previous date and current date file then copy the new one to the target local server, if not then don't do anything. I have other scripts to call for previous date file. but as I said, last piece of coping or moving isn't working.

Code: Select all

CD /D P:
 
Call \Config\env.cmd

for /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set weekday=%%a& set month=%%b& set day=%%c& set year=%%d)
echo Date is %year%%month%%day%

pause

> P:\batch\Main_Tasks\FTPgetNew.txt ECHO USER uid
>>P:\batch\Main_Tasks\FTPgetNew.txt ECHO password
>>P:\batch\Main_Tasks\FTPgetNew.txt ECHO cd /analytics/extracts/
>>P:\batch\Main_Tasks\FTPgetNew.txt ECHO binary
>>P:\batch\Main_Tasks\FTPgetNew.txt ECHO lcd P:\DataTest\CVATest
>>P:\batch\Main_Tasks\FTPgetNew.txt ECHO get CVA%year%%month%%day%.csv
>>P:\batch\Main_Tasks\FTPgetNew.txt ECHO get cvagui.zip
>>P:\batch\Main_Tasks\FTPgetNew.txt ECHO bye

ftp -d -n -s:P:\batch\Main_Tasks\FTPgetNew.txt xx.xx.xx.xx
pause

TYPE NUL > P:\batch\Main_Tasks\FTPgetNew.txt
DEL P:\batch\Main_Tasks\FTPgetNew.txt

cd P:\DataTest\CVATest
CD /D P:
%batchroot%\tools\bin\PKZip25 -ext -overwrite P:\DataTest\CVATest\cvagui.zip
copy P:\DataTest\CVATest\cvagui.csv P:\DataTest\CVATest\%year%%month%%day%_all.csv
DEL P:\DataTest\CVATest\cvagui.zip

pause

Call %ConfigRoot%\Set_Substract_For_DateMinusX.bat
Echo [%Date%-%Time%] SubstractDay %SubstractDay%

pause


Call %BatchRoot%\Tools\Date-x.bat %day% %month% %year% %SubstractDay% EU
Set lastCubeDate=%DateMinusX% 
echo Comapre Date %lastCubeDate: =%

pause
setlocal EnableDelayedExpansion

IF EXIST P:\DataTest\CVATest\%lastCubeDate: =%_all.csv (
   fc P:\DataTest\CVATest\%year%%month%%day%_all.csv P:\DataTest\CVATest\%lastCubeDate: =%_all.csv | find "FC: no diff" > nul
   IF !ERRORLEVEL!  EQL 1 (
   cd P:\DataTest\CVATest
   move P:\DataTest\CVATest\cvagui.csv P:\DataTest\%year%%month%%day%_all.csv
   del P:\DataTest\CVATest\%lastCubeDate: =%_all.csv
) ELSE (
   echo P:\DataTest\CVATest\%lastCubeDate: =%_all.csv not found.
   move P:\DataTest\CVATest\cvagui.csv P:\DataTest\%year%%month%%day%_all.csv
))

pause


This last piece isn't working.

jeb
Expert
Posts: 964
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: what is wrong with IF else, please help me

#6 Post by jeb » 02 Dec 2011 11:46

Kunal wrote:This last piece isn't working.

Isn't very helpful :?:

Describe your problem, or even better add some ECHO 123, ECHO 456 to see what happens
like this

Code: Select all

setlocal EnableDelayedExpansion
ECHO ON
IF EXIST P:\DataTest\CVATest\%lastCubeDate: =%_all.csv (
  ECHO *****DEBUG: EXIST
   fc P:\DataTest\CVATest\%year%%month%%day%_all.csv P:\DataTest\CVATest\%lastCubeDate: =%_all.csv | find "FC: no diff" > nul
  ECHO *****DEBUG: NOT ERRORLEVEL IS !ERRORLEVEL!
   IF !ERRORLEVEL!  EQL 1 (
   cd P:\DataTest\CVATest
   move P:\DataTest\CVATest\cvagui.csv P:\DataTest\%year%%month%%day%_all.csv
   del P:\DataTest\CVATest\%lastCubeDate: =%_all.csv
) ELSE (
  ECHO *****DEBUG: NOT EXIST
   echo P:\DataTest\CVATest\%lastCubeDate: =%_all.csv not found.
   move P:\DataTest\CVATest\cvagui.csv P:\DataTest\%year%%month%%day%_all.csv
))


Analyze the output and you will find your problem :)

jeb

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: what is wrong with IF else, please help me

#7 Post by orange_batch » 02 Dec 2011 17:41

OJBakker wrote:A minor syntax problem might be the problem.
change EQU to EQL

? EQU is correct, EQL does nothing. Personally I prefer using == for EQU comparisons.

Kunai, I strongly suggest using quotation marks around your paths, but anyway, this should work...

Code: Select all

setlocal EnableDelayedExpansion

IF EXIST P:\DataTest\CVATest\%lastCubeDate: =%_all.csv (
   fc P:\DataTest\CVATest\%year%%month%%day%_all.csv P:\DataTest\CVATest\%lastCubeDate: =%_all.csv | find "FC: no diff" >nul
   IF !ERRORLEVEL! EQU 1 (
   cd P:\DataTest\CVATest
   move P:\DataTest\CVATest\cvagui.csv P:\DataTest\%year%%month%%day%_all.csv
   del P:\DataTest\CVATest\%lastCubeDate: =%_all.csv
) ELSE (
   echo P:\DataTest\CVATest\%lastCubeDate: =%_all.csv not found.
   move P:\DataTest\CVATest\cvagui.csv P:\DataTest\%year%%month%%day%_all.csv
))

What need for the change directory? Also as Ed mentioned in your other thread, I would always use the binary switch with fc. Personally I would write it:

Code: Select all

setlocal enabledelayedexpansion

set file1="P:\DataTest\CVATest\%lastCubeDate: =%_all.csv"
set file2="P:\DataTest\CVATest\%year%%month%%day%_all.csv"
set file3="P:\DataTest\CVATest\cvagui.csv"

if exist %file1% (
fc /b %file2% %file1%|find "FC: no diff">nul
if !ERRORLEVEL!==1 (
move %file3% %file2:CVATest\=%
del %file1%
) else (
echo: ^> %file1% not found.
move %file3% %file2:CVATest\=%
))

Also no need for delayed expansion if you move the errorlevel check out of the main condition (but add verify>nul reset for errorlevel).

Code: Select all

set file1="P:\DataTest\CVATest\%lastCubeDate: =%_all.csv"
set file2="P:\DataTest\CVATest\%year%%month%%day%_all.csv"
set file3="P:\DataTest\CVATest\cvagui.csv"

if exist %file1% (fc /b %file2% %file1%|find "FC: no diff">nul) else verify>nul
if %ERRORLEVEL%==1 (
move %file3% %file2:CVATest\=%
del %file1%
) else (
echo: ^> %file1% not found.
move %file3% %file2:CVATest\=%
)

OJBakker
Expert
Posts: 29
Joined: 12 Aug 2011 13:57

Re: what is wrong with IF else, please help me

#8 Post by OJBakker » 02 Dec 2011 19:12

? EQU is correct, EQL does nothing. Personally I prefer using == for EQU comparisons.


You are right.
I got EQL from if /? (Vista language: dutch)

Code: Select all

    EQL - is gelijk aan
    NEQ - is niet gelijk aan
    LSS - kleiner dan
    LEQ - kleiner dan of gelijk aan
    GTR - groter dan
    GEQ - groter dan of gelijk aan

I always use == so had not noticed this translation error before.

Post Reply