Check if file is empty

Sounds easy, how do you identify if a certain file isempty? An ordinary text file? I'm going to execute it using batch file.

e.g

if sample.txt = empty then
echo Empty
else
echo OK
endif

Thanks.
banyokhingAsked:
Who is Participating?
 
ben-gurConnect With a Mentor Commented:
You missed a char after the IF, and the filename should be inside "
(In my code I confused between the EMPTY and NOTEMPTY, so..)

Here is your complete code:

For /F %%A in ("disk_load.csv") Do If %%~zA equ 0 GoTo EMPTY
:NOTEMPTY
echo file is not empty
goto END
:EMPTY
echo file is empty
goto END
:END

Note that it should be called from batch file. it will not work if running it from command line.
Let me know what are the results.

Adam.
0
 
ben-gurCommented:
Use FC to compare your file to a known empty file and then check the return code.

Create an empty file called EMPTY.TXT
Then create the following batch file:

@echo off
fc empty.txt %1 > nul
if errorlevel == 1 goto NOTEMPTY
if errorlevel == 0 goto EMPTY
goto END
:EMPTY
echo file is empty
goto END
:NOTEMPTY
echo file is not empty
goto END
:END

Regards,
Adam.
0
 
banyokhingAuthor Commented:
I got an error... by the way, sorry but the file I was going to check hapens to be a .csv file.

FC: Insufficient number of file specifications
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
ben-gurCommented:
Don't forget to create the file EMPTY.TXT. You can locate it on C:\ and modify the line
fc empty.txt %1 > nul
to
fc C:\empty.txt %1 > nul

If you named the batch file ISEMPTY.BAT, then you should call it like this:
C:\>ISEMPTY.BAT FILE.CSV

If it still doesn't work you can create this batch file:
(Note that this works only on Windows XP)

@echo off
if not exist %1 goto :END
For /F %%A in (%1) Do If %%~zA equ 0 GoTo NOTEMPTY
echo file is empty
goto END
:NOTEMPTY
echo file is not empty
goto END
:END

0
 
banyokhingAuthor Commented:
Hi, it doesn't seem to work...

I have a file called disk_load.csv. Is my syntax correct? thanks.

for /F %%a in (disk_load.csv) DO If %%~A equ 0 goto NOTEMPTY
:EMPTY
echo file is empty
goto END
:NOTEMPTY
echo disk_load.csv has value
goto END
:END
0
 
stuartdehaanCommented:
The file should be in 8.3 format if you are MS-DOS (mode). You should than use disk_l~1.csv.

Stuart
0
 
SteveGTRCommented:
If you consider a file empty you could do the following as the others have said:

@echo off

if "%~z1"=="0" echo File is empty

Or if you consider a file empty that only contains blank lines then you could do this:

@echo off

set Empty=

for /f "delims=" %%a in ('type "%~1" 2^>NUL') do call :CHECKLINE %%a

if "%Empty%"=="N" goto :EOF

echo File is empty

goto :EOF

:CHECKLINE

if not "%1"=="" set Empty=N

Note, that these are both batch files that accept file names on the command line. For example, if you named the first file checkzero.bat, you'd say: checkzero.bat disk_load.csv

Also, I'd recommend copy and pasting these batch files instead of keying them in by hand. This avoids a bunch of potential problems as I've seen above.

Good Luck,
Steve






0
 
InteractiveMindCommented:
You can just do something like this:


@echo off
if /i [%~1]==[] (
   :get_file
   set /p file="Enter the file name: "
   if not exist %file% echo Cannot locate file& goto :get_file
)
set file=%~1
if not exist "%file%" echo File does not exist& exit /b
for %%S in (%file%) do if %%~zS leq 0 (@echo File is empty) else (@echo File is not empty [%%~zS])
exit /b


You can then use it either like this:

At command prompt:

   BatchFileName "c:\fileToCheck.ext"

Or, just call it like so:

   BatchFileName

It will then prompt you for the filename, and check it's size.. If it's size is greater than 0 bytes, then it will output it's actual size.

Regards;
0
 
SteveGTRCommented:
ben-gur offered a number of solutions and has work hardest on the problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.