开发者

Why is my IF comparison op failing?

开发者 https://www.devze.com 2023-02-19 15:29 出处:网络
update SOLVED - the problem is not with my code.there is a design limitation (some might say \"bug\") in the command processor.the IF comparison operators do not work with values higher than 214748364

update SOLVED - the problem is not with my code. there is a design limitation (some might say "bug") in the command processor. the IF comparison operators do not work with values higher than 2147483648. now I just have to figure out how to strip off some of the least significant digits for the workaround. thanks to all for reading.

=======

hello out there in cyberland,

I manage a small company's network. I'm trying to set up an automated archival process using a batch file running on a server, and need a little help with the logic. I'm not a programmer and don't have the time to learn PowerShell or VBscript. I have read and re-read the MS command reference on IF, FOR, and CALL, and can not figure out what I'm doing wrong.

We have a Windows 2000 Server with two disk drives and a tape drive. One disk has a shared folder, call it Public; the other disk is not shared, call it Staging.

The Public drive will accept incoming backups from the client desktops. I need the process to move files from the Public drive to the Staging drive until the drive is too full to accept any more files, whereupon the tape backup starts. Upon completion of the tape run, the Staging drive will be emptied and the process will resume moving files from Public to Staging.

The goal is to automatically and perpetually archive the desktop backup files to tape, with no user intervention other than changing the tape.

You may ask, why involve two drives? Two reasons:

1) If the desktops back up directly to the Staging drive, at some point the client desktop backups will fail for lack of space on the target drive. I have no way to predict when this will occur; the only definable condition I can think of would be an arbitrary threshold of free disk space, but since the backup files to be archived will be different sizes, I could run into a situation where the target drive free space is above the threshold, but the next backup exceeds that space. Result: client desktop backup fails and server tape backup does not start.

2) If the Staging drive fills up and the tape backup fails for some reason (tape not changed or whatever), having the desktops back up to the Public drive buys me some time to fix the tape issue before the desktop backups begin to fail.

Here is the algorithm I'm trying to code:

1) Begin

2) Obtain free space on Staging drive

3) Obtain size of smallest file on Public drive (if no files present, exit)

4) Compare file size with free space on Staging drive

5) if file will fit, move it to Staging drive; else, exit and start Tape Backup

6) return to Begin

And here is my batch code which is not working. At this stage in testing, I have one file (8 GB) in the Public drive which will fit in the free space (32 GB) on the Staging drive. The set command confirms the variables are being set as expected; the process is failing at the statement [ if %BKFsize% LSS %DiskFree%" ]. Instead of the file being moved, the tape routine specified by 'else' is called instead:

@echo off
setlocal enableextensions enabledelayedexpansion
:Begin
REM   obtain and display free space on Staging drive.
D:
for /f "tokens=3 delims= " %%A in ('dir ^| find "bytes free"') do (
set Z=%%A
set Z=!Z:,=!
)
set DiskFree=!Z!
echo.
echo D: has %DiskFree% bytes free
echo.
REM   obtain sizes of files on Public drive to be moved.
E:
cd \backup.email
if not exist *.bkf exit
dir *.bkf /b /os > BKFlist.txt
for /f "tokens=*" %%G in (BKFlist.txt) do call :CheckBKF "%%G"
goto :eof
:开发者_如何学GoCheckBKF
set BKFfile=%1
set BKFsize=%~z1
echo.
echo File %BKFfile% is %BKFsize% bytes
echo.
set
pause
REM   move file(s) to Staging drive, space permitting; or, 
REM   if not enough space for smallest file, start tape backup.
if %BKFsize% LSS %DiskFree% (
echo Moving file %BKFfile% to drive D
echo.
move E:\backup.email\%BKFfile% D:\backup.email
) else (
C:\WINNT\AutoBackup\TapeBKF.cmd
exit
)
goto Begin
:End

I know this must be fairly simple but like I said, I'm not a programmer, and I bow to your superior skill. Any advice is much appreciated, and thank you.


In batch file, there is no real difference between strings and integers types, but that isn't your problem here.

The problem is that integers only works in the 32 bit signed range.
-2147483648 to 2147483647

But your filesize is greater and also your disk space. To test this you could try to add a one.

Set /a myvar=BKFsize+1
Set /a myvar=DiskFree+1

I suppose you get an error.

You could solve it, if you try to use the string compare, currently you do a string compare but your numbers are not of the same length, so you get "unpredictable" results.
But if you adjust/filling the strings with zeros so they have the same length, it should work.

set "strBKSize=000000000000000000%bkSize%"
set "strBKSize=%strBKSize:~-15%"
set "strDisksize=000000000000000000%Disksize%"
set "strDisksize=%strDisksize:~-15%"

Or you try to calculate not with bytes, but in MB or GB, by removing 6 or 9 numbers/characters.

set BKFsizeGB=%BKFsize:~0,-9%

Then you can use them as numbers.


Maybe the values contained in your variables are not integers but strings

I suppose it's comparing: 8<3 == false

0

精彩评论

暂无评论...
验证码 换一张
取 消