views:

855

answers:

2

I have the following Script,

more +1 "C:\Documents and Settings\Administrator\Desktop\Backup Data\import file\1.txt" > "C:\Documents and Settings\Administrator\Desktop\Backup Data\import file\2.txt"
"C:\Program Files\Microsoft Office\Office11\MSACCESS.EXE" "C:\Power Play\Daily\Daily.mdb" /x "Macro1"
DEL  "C:\Documents and Settings\Administrator\Desktop\Backup Data\import file\*.*" /q

The first line I'm doing since my text file comes with a blank line at the top then followed with the titles then the data.

My problem is the following, sometimes the text file contains only blank line and header. So after truncation, i end up with only headers.That is causing me a problem when I am importing the data into access.

Can anyone provide me in a way where I can stop import into access if the file is empty

A: 

I've found a way to check size of the file:

for %R in ("C:\...\import file\1.txt") do if %~zR lss 80 exit 1

This checks file size though (if it's less than 80 bytes). I don't know how to check for number of lines in Windows batch file.

I'd suggest stop using this crippled invention (batch files) and switch to a sane shell.

Eugene Morozov
+1  A: 

You can add the following to your script to exit if the file contains just an empty line and the header:

@echo off
setlocal enableextensions enabledelayedexpansion

set INPUT="C:\Documents and Settings\Administrator\Desktop\Backup Data\import file\1.txt"
set OUPUT="C:\Documents and Settings\Administrator\Desktop\Backup Data\import file\2.txt"

:: Get the number of lines in the file.
:: Skip the first line since it is blank.
set LINES=0
for /f "skip=1 delims=" %%I in (%INPUT%) do (
    set /a LINES=LINES+1
    if !LINES! EQU 2 goto :PROCESS
)
@echo %INPUT% is empty.
endlocal
exit /b 1

:PROCESS
:: Process the file
more +1  %INPUT% > %OUTPUT%
"C:\Program Files\Microsoft Office\Office11\MSACCESS.EXE" ^
"C:\Power Play\Daily\Daily.mdb" /x "Macro1"
DEL "C:\Documents and Settings\Administrator\Desktop\Backup Data\import file\*.*" /q

endlocal
exit /b 0
Patrick Cuff