Page 1 of 1

null string

Posted: 24 Aug 2011 01:51
by glob5
@echo off
setlocal enabledelayedexpansion

Set sz=
for /f "tokens=5 delims= " %%a in
('type mytext.txt') Do (Set sz=!sz!+%%a)
echo !sz!

if mytext.txt is empty , do statement does not get executed and the statement
,echo !sz!, results in ,!sz! itself. How can i check the sz to see if mytext.txt is empty or not?.

Re: null string

Posted: 24 Aug 2011 06:02
by dbenham

Code: Select all

for %%a in ("mytext.txt") do set sz=%%~za
sz will be undefined if mytext.txt does not exist. Otherwise it will contain the file size.

The same thing works for batch file and function call arguments. Assume %1 contains your file name:

Code: Select all

set sz=%~z1

Try HELP FOR and HELP CALL for a full list of expansion modifiers.

Dave Benham