Solved Delimiter validation for number of columns using batch scrip

April 12, 2019 at 06:04:20
Specs: DOS
My valid records are only that contains 3 columns. Else everything should be captured into an error file. In this case any records having less or more than 2 commas.

1,2,3
1,2,3,4
1,2

This works for only more than 2 commas.
FINDSTR /R "^.*,.*,.*$" "%infile%"

Thanks for your help in advance.


See More: Delimiter validation for number of columns using batch scrip

Reply ↓  Report •

#1
April 15, 2019 at 02:50:31
✔ Best Answer
I can add, saving the 3 column long strings to a file if you want.

console output:

1,2,3
DONE!

using input.txt containing:

1,2,3
1,2,3,4
1,2

%uniquenumber%-errorfile.txt (outputfile) contains:

1,2,3,4
1,2

test.bat:

@echo off&setlocal enabledelayedexpansion
for /f "tokens=1-3 delims=:." %%a in ('echo %time%') do set "uniquenumber=%%a-%%b-%%c"
for /f "tokens=1-3,* delims=," %%a in (
	input.txt
) do (
	if "%%c"=="" (
		call :subroutine "%%a,%%b"
	)
	if not "%%c"=="" (
		if "%%d"=="" (
			echo %%a,%%b,%%c
		) else (
			call :subroutine "%%a,%%b,%%c,%%d"
		)
	)
)
echo DONE^^!&pause
exit/b
:subroutine
set "var=%~1"
:loop
if "!var:~-1!"=="," (
	set "var=!var:~0,-1!"
	goto loop
) else (
	if not "!var!"=="" (
		echo !var!>>%uniquenumber%-errorfile.txt
	)
)
exit/b

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


Reply ↓  Report •

#2
April 20, 2019 at 23:44:53
@echo off&setlocal
for /f "tokens=*" %%a in (q) do (
set x=%%a
set c=0
call :xx %%a
)
goto :eof


:xx
set cc=%1
if not defined cc goto :z
set /a c+=1
shift
goto :xx
:z
if %c% equ 3 (echo ok: %c% %1 %x%) else ( echo not ok: %x%)

message edited by nbrane


Reply ↓  Report •
Related Solutions


Ask Question