Batch: Checking for null value and writing a string in its place -
i have group of text files contain either 1 line of data or blank. need of files dumped single file. part's easy enough, need blank file return "nodata" or similar string in new file.
setlocal enabledelayedexpansion type nul >c:\somedirectory\dataall.txt rem find files in direcotry; files %%g in (c:\somedirectory\txt??.txt) ( rem find of text within found files /f "tokens=*" %%t in ('type "c:\somedirectory\%%~ng.txt"') ( set datain=%%t rem check see if file empty , write nodata final variable if if "c:\somedirectory\%%~ng.txt" lss 1('set "/a datain=nodata"') rem check see if file not empty , write data final variable if "c:\somedirectory\%%~ng.txt" neq 0 ('set /a "datain=%%t"') rem write datain final text file >> "c:\somedirectory\dataall.txt" echo !datain! ) ) endlocal
this duplicates previous text file's data blank one. example, if txt1 has xxx,xxx,xx; txt2 blank; , txt3 x,x,x datall file read:
xxx,xxx,xx
xxx,xxx,xx
x,x,x
how set datain string?
for %%g in (c:\somedirectory\txt??.txt) ( set "datain=no data" rem find of text within found files /f "usebackqtokens=*" %%t in ("c:\somedirectory\%%~ng.txt") ( set datain=%%t ) rem write datain final text file >> "c:\somedirectory\dataall.txt" echo !datain! )
relying on "either it's blank or has 1 line" info, set output string datain
"not found" value , overwrite 1 data line if it's found. empty file not execute do
part of for %%t
loop.
Comments
Post a Comment