[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 36277
  • Last Modified:

Error Handling in a Batch File

Big points because of the urgency. In the following batch file I generate a CHM file, but the CHM compiler is not reliable. As a result I want to check if my batch file is smaller than roughly 2 megs and abort copying it to the clean file directory if it is smaller than roughly two megs. This script is not working as the file gets copied no matter what. I'm sure it is a screwup in the goto statements.

@echo off
echo\

echo Changing Directory
cd /d %2

del %TEMP\chmbuild\*

CALL prepare_xml.bat %1 %2
CALL strip_remarks.bat %1 %2

echo Building Intermediate Files
xsltproc --xinclude --stringparam l10n.gentext.default.language ja --stringparam htmlhelp.use.hhk 1 --output %TEMP%\chmbuild\ "%DOCBOOK_BASE%\xsl\htmlhelp\mysql-html-help.xsl" "%TEMP%\chmbuild\%1.xml"
if not %errorlevel%==0 goto :error

echo Compiling HTML Help File
hhc.exe %TEMP%\chmbuild\htmlhelp.hhp

echo Checking FIle Size
if not %TEMP%\chmbuild\htmlhelp.chm~z1 GTR 2000000 goto :error

echo Moving from Temp Directory
move %TEMP%\chmbuild\htmlhelp.chm C:\built-docs\%1-%3.chm
if not %errorlevel%==0 goto :error

:error
echo ERROR ENCOUNTERED
exit /B -1
0
Squeebee
Asked:
Squeebee
  • 3
  • 2
1 Solution
 
SteveGTRCommented:
A quick glance and I'd say you should do the following:

echo Checking File Size
pushd.

cd /D "%TEMP%\chmbuild"

set error=

for /f "delims=" %%a in ('dir /b htmlhelp.chm 2^>NUL') do if /I %%~za LSS 2000000 set error=Y

popd

if "%error%"=="Y" goto :ERROR

I'll run some more tests.

Good Luck,
Steve
0
 
SteveGTRCommented:
Yes, that appears to do the trick. Here's a little test program that accepts a number of bytes of the command line for testing. I hardcoded the file name and directory. You can change them if you like for testing:

@echo off

setlocal

echo Checking File Size

set fileSize=200

if not "%~1"=="" set fileSize=%~1

set dirName=\temp\temp\temp
set fileName=temp.dat
REM %TEMP%\chmbuild
REM htmlhelp.chm

pushd.

cd /D "%dirName%"

set error=

for /f "delims=" %%a in ('dir /b "%fileName%" 2^>NUL') do if /I %%~za LSS %fileSize% set error=Y

popd

if "%error%"=="Y" goto :ERROR

echo No error...

goto :EOF

:ERROR

echo Error
0
 
SqueebeeAuthor Commented:
Hi Steve;

Can you either show this incorporated into the batc I provided or show how to modify my batch file to use this? I'm afraid batch files are far from my strong point.

Thanks.
0
 
SqueebeeAuthor Commented:
Nevermind Steve, figured it out.
0
 
SqueebeeAuthor Commented:
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now