null string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
glob5
Posts: 34
Joined: 23 Nov 2010 00:47

null string

#1 Post by glob5 » 24 Aug 2011 01:51

@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?.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: null string

#2 Post by dbenham » 24 Aug 2011 06:02

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

Post Reply