Solved Capture 2 file sizes in vars then compare in an IF statement

July 6, 2012 at 09:06:01
Specs: Windows XP

I am Merging 2 CSV files coming from 2 Databases (DB). This happens when one CSV is dropped into a directory I am have listening to. Once I get this file I call a web service that calls to BIP to gather the data from the other DB and reads the CSV file I just got in my directory into a final CSV which then lands back into my directory. What I want to do is get the file size of the first file and save it to a variable. Then once I get the 2nd file I want to get its size and save that to a variable. Then I want to compare both variables to verify my final file size is larger than my first file size variable.

I capture the first file size into a Variable called PREsize using this code:

REM -- Get file size of file coming out of DB
cd Inbox
for /f "tokens=4 " %%a in ('dir /a-d/-c %FILENAME%.txt ^| findstr /e %FILENAME%.txt') do set PREsize=%%a
cd ..

Then I get the 2nd file size into variable called POSTsize using this code:

REm ---Check File size
cd Outbox
for /f "tokens=4 " %%a in ('dir /a-d/-c %WS_FILENAME% ^| findstr /e %WS_FILENAME%') do set POSTsize=%%a

Then I compare the 2 vars, if POSTsize is smaller I send an email to alert support something is wrong with this code:

REM --- Verify that final file size is larger than first file size
IF %POSTsize% LEQ %PREsize% (GOTO FinalfileFailed)

ECHO File %HOME%\Outbox\myFile.txt is corrupted > Log\Error.log
cscript eMail.vbs "My file merge did not complete. Final CSV file is corrupted."

So this code looks right, but for some reason it doesnt look like the compare is working, at least in my tests I am unable to get the email to occur with the final file size is less than the first file size. Can anyone help.


See More: Capture 2 file sizes in vars then compare in an IF statement

Report •

July 6, 2012 at 10:01:01
✔ Best Answer
First thing:
for /f "tokens=4 " %%a in ('dir /a-d/-c %FILENAME%.txt ^| findstr /e %FILENAME%.txt') do set PREsize=%%a
Seems like a long-winded way to say:
for %%a in (%FILENAME%.txt) do set PREsize=%%~Za

Second thing:
Show us the output of the script. If you disabled echo, leave it on.

How To Ask Questions The Smart Way

Report •

July 6, 2012 at 13:07:20
Hi Razor,

I agree about your 1st suggestion. I changed my code and that definitely flows better. My batch scripting is very weak, this script was my 2nd script I ever wrote. As for the comparison not working I think I was wrong. I had 2 problems: 1. Calling my web service and the 2nd one was doing the file size comparisons. Either the code you offer fixed the comparison issue or when I resolved my web service call that fixed my 2nd issue. Either way its now working correctly.

Thanks again for the help. Your solution definitely worked.


Report •

Related Solutions

Ask Question