1.

Solve : problem to catch errorlevel?

Answer»

Dear All, i'm newbie. I tried to make a batch file to backup data(files). But before it copy the files which are needed to backup, it checks first whether there is a data to backup and give information...
here with is part of my coding:

:BACKUP new or changed files
:: Check whether there are files to backup
ECHO. Checking files...
set task=There were no files to backup
xcopy %src% %dest% /d /s /y /q /l|find " 0 "
if not errorlevel 1 goto JOBDONE
set task=Backup new or changed files
echo. %task% in %src%
xcopy %src% %dest% /d /y /s
goto JOBDONE

==============
JOBDONE, only echo the message EV "task".
the idea is using find "0" to get information whether there are files to backup. and i use "errorlevel" for this.

i tested DIRECTLY in command promt, find "0" would give 0 if no file and 1 if found the file(actually charater 0) from xcopy list result using /l switch.
But the problem in my batch file, it always catched errorlevel 1 even it is no file to backup( 0 file).

i'm using ms dos ver:5.1.2 and XP SP2

thank before.....
FOR /F "tokens=1" IN ('DIR /B %src%') DO XCOPY %src% %dest% /d /y /s

If there is nothing in the %src% directory, then nothing is copied, no need for ERRORLEVEL checking.
Quote from: ProgressDBA on June 05, 2008, 10:38:20 AM

FOR /F "tokens=1" IN ('DIR /B %src%') DO XCOPY %src% %dest% /d /y /s

If there is nothing in the %src% directory, then nothing is copied, no need for ERRORLEVEL checking.


Thank u for your respond,

Could u help me to explain ur logic of that code?
Because if i were not mistaken, it won't copy,only if %src% directory is empty.
But the idea of my coding is that there will always be files in %src% dirct. and xcopy /d /s /y /l gives display list whether there is a file changing or update than check with "find" to KNOW whether there is file update and than need to backup...
And also i want to give message to the user...that there is no file need to backup because no file changed.

Or maybe ur code has worked like my idea, just perhaps i didn't understand it well?

once again thank u...The flaw in the original logic is twofold. One there is no leading space in the string you're using in the find instruction. If you fix the first ("0 "), any numeric that is a multiple of ten will produce an errorlevel not zero.

Code: [SELECT]:BACKUP new or changed files
:: Check whether there are files to backup
echo. Checking files...
set task=There were no files to backup
for /f %%i in ('xcopy %src% %dest% /d /s /y /q /l') do (
if %%i==0 goto JOBDONE
)
set task=Backup new or changed files
echo. %task% in %src%
xcopy %src% %dest% /d /y /s
goto JOBDONE




Quote from: Sidewinder on June 06, 2008, 05:49:46 AM
The flaw in the original logic is twofold. One there is no leading space in the string you're using in the find instruction. If you fix the first ("0 "), any numeric that is a multiple of ten will produce an errorlevel not zero.




Wow....u r right, now my code is working...
I didn't realize that.
And i tried ur coding, it's nice, worked very well, and i never thought about that logic which is very cool ...(need more practice)
also this forum...
i think i'll use yours...

Thank u very much....



Discussion

No Comment Found