Solved finding empty lines in a file using batch script

April 25, 2019 at 01:11:04
Specs: DOS
I have a file with following data (4 rows)

a,b,c

1,2,3
,,

How to find out 2nd and 4th lines. I need to do two changes to this
1.write the two error records to new file(new file1 contains 2 and 4 records or at least 4th record because 2nd line is basically empty)
2.create new file removing 2 error records(new file2 contain 1 and 3 records)

Thanks for your help


See More: finding empty lines in a file using batch script

Report •

✔ Best Answer
April 27, 2019 at 21:03:27
@echo off & setlocal
for /f %%a in (test) do call :xx %%a
goto :eof

:xx
:: just debugging dislay FYI
echo 1: %1
echo all: %*
if "%1" neq "" (
>>mainfile: echo %*
) else (
>>errfile echo %*
)

goto :eof



#1
April 25, 2019 at 03:28:43
so write line 2+4 to file1
and line 1+3 to file2, got it.

i'll try something

i5-6600K[delid]@4.9GHz/4.4GHz cache/@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded


Report •

#2
April 25, 2019 at 03:46:15
test.bat:

@echo off
set inputfile=input.txt
set file1=file1.txt
set file2=file2.txt
::removing the :: for the 2 lines below will make file1 & file2 before writing new data to them.
::echo off>%file1%
::echo off>%file2%
for /f "tokens=1* delims=[]" %%a in ('find /N /V "" "%inputfile%"') do (
	if "%%a"=="1" if not "%%b"=="" echo %%b >>%file2%
	if "%%a"=="2" if not "%%b"=="" echo %%b >>%file1%
	if "%%a"=="2" if "%%b"=="" echo.>>%file1%
	if "%%a"=="3" if not "%%b"=="" echo %%b >>%file2%
	if "%%a"=="4" if not "%%b"=="" echo %%b >>%file1%
)

i5-6600K[delid]@4.9GHz/4.4GHz cache/@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded

message edited by hidde663


Report •

#3
April 25, 2019 at 06:53:14
Thanks for the help hidde663

I want it to be generic. Sorry if I didn't make it clear. Basically I need all the empty lines (with or without delimiters to be removed from my file)

the above script is only working the specific sample data I gave.


Report •

Related Solutions

#4
April 25, 2019 at 08:25:05
this should be fool proof as long as the max numbers of variables/tokens is 3

so:
a,b,c
and
1,2,3
are fine, but:
1,2,3,4
won't work as it will cut of the ,4 part, as it is expecting 2 comma's(delims) with 3 "tokens"/variables.

anyways, test.bat:

@echo off&setlocal enabledelayedexpansion
set "inputfile=input.txt"
set "outputfile=output.txt"
::removing the :: for the line below will empty outputfile before writing new data to it.
::echo off>%outputfile%
for /f "tokens=1-4 delims=[]," %%a in ('find /N /V "" "%inputfile%"') do (
	if not "%%b%%c%%d"=="" if not "%%b,%%c,%%d"==",," echo %%b,%%c,%%d >>%outputfile%
)

i5-6600K[delid]@4.9GHz/4.4GHz cache@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded

message edited by hidde663


Report •

#5
April 25, 2019 at 08:28:37
is this a follow up on this post of yours?:
https://www.computing.net/answers/p...

i5-6600K[delid]@4.9GHz/4.4GHz cache@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded


Report •

#6
April 26, 2019 at 01:05:56
that post is to find out the rows having empty columns.. I want to capture these kind of rows in a new file.. this is to find out the empty rows.. I want to create a new file excluding these empty rows.

Report •

#7
April 26, 2019 at 08:44:38
did post #4 work for you?

i5-6600K[delid]@4.9GHz/4.4GHz cache@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded


Report •

#8
April 27, 2019 at 21:03:27
✔ Best Answer
@echo off & setlocal
for /f %%a in (test) do call :xx %%a
goto :eof

:xx
:: just debugging dislay FYI
echo 1: %1
echo all: %*
if "%1" neq "" (
>>mainfile: echo %*
) else (
>>errfile echo %*
)

goto :eof


Report •

Ask Question