Reputation: 11
I have a group of text files that contain either one line of data or are blank. I need all of those files to get dumped into a single file. That part's easy enough, but I need the blank file to return a "Nodata" or similar string in the new file.
setlocal enabledelayedexpansion
type nul >C:\somedirectory\DataAll.txt
REM Find all files in a direcotry; for those files
for %%G in (C:\somedirectory\txt??.txt) do (
REM Find all of the text within the found files
for /f "tokens=*" %%T in ('type "C:\somedirectory\%%~nG.txt"') do (
set DataIn=%%T
REM Check to see if the file is empty and write NoData the final variable to it if so
if "C:\somedirectory\%%~nG.txt" LSS 1('set "/a DataIn=Nodata"')
REM Check to see if file is not empty and write the data to the final variable
if "C:\somedirectory\%%~nG.txt" NEQ 0 ('set /a "DataIn=%%T"')
REM Write DataIn to final text file
>> "C:\somedirectory\DataAll.txt" echo !DataIn!
)
)
endlocal
This duplicates the previous text file's data into the blank one. For example, if txt1 has xxx,xxx,xx; txt2 is blank; and txt3 is x,x,x the datall file will read:
xxx,xxx,xx
xxx,xxx,xx
x,x,x
How do I set DataIn to another string?
Upvotes: 1
Views: 57
Reputation: 80033
for %%G in (C:\somedirectory\txt??.txt) do (
set "DataIn=No data"
REM Find all of the text within the found files
for /f "usebackqtokens=*" %%T in ("C:\somedirectory\%%~nG.txt") do (
set DataIn=%%T
)
REM Write DataIn to final text file
>> "C:\somedirectory\DataAll.txt" echo !DataIn!
)
Relying on your "either it's blank or has 1 line" info, then set the output string datain
to the "not found" value and overwrite it with the one data line if it's found. An empty file will not execute the do
part of the for %%T
loop.
Upvotes: 1