Saturday, 15 August 2015

Remove all blank lines from *.txt files in the directory and subfolders in Windows -



Remove all blank lines from *.txt files in the directory and subfolders in Windows -

searching, trying , crying developed code:

for /r %%g in (*.txt) /f "delims=" %%a in ('more +1 %%g ^| find /v ""') (set line=%%~a echo !line!) > new\%%g

do know why loop forever?

what expected do, remove blank lines in every *.txt file founds in subdirectories , set new file same name in "new" folder. can provide "new" folders needed manually.

i'm not sure cause loop forever, writing new folder in same hierarchy reading from. process files wrote. need filter prevent that.

also, think output file name needs %%~nxg file name , extension, without path.

there much simpler way strip out blank lines. findstr . file strip out empty lines. findstr /rc:"[^ ] strip out empty lines , lines contain spaces.

@echo off setlocal set "outfolder=new" %%f in ("%outfolder%\.") /r %%g in (*.txt) if "%%~dpg" neq "%%~ff\" findstr /rc:"[^ ]" "%%g" >"%%~ff\%%~nxg"

but simplest thing create sure output folder distinct source hierarchy

for /r %%g in (*.txt) findstr /rc:"[^ ]" "%%g" >"\distinctpath\%%~nxg"

update

one thing cause script hang piped more hang if file has more 64k lines. don't understand why using more in first place.

windows batch-file cmd

No comments:

Post a Comment