Hello,
I often have some ascii text files that need edited. Not wanting to do the same task over and over, I want to make a batch file.
Here is and idea given to me by another poster. So I have 100 or 1000 files. Could be txt, log, cfg etc.
Need to delete the blank lines, for all the files in the current directory tree. And as an option remove extra whitespace. (what is extra whitespace? I will consider it to be: one or more tabs, two or more spaces between tokens, trailing spaces after the last token on a line.)
The following will single space the files in the current tree. Option to remove extra whitespace.
:: single_space.bat
:: single space all txt files in the current tree...
:: Usage single_space [nowhitespace]
@echo off
SetLocal EnableDelayedExpansion
If exist *.new del /s *.new >nul
If not %%1==() Set Option=%1
:: backup original files. cp *txt *old
For /F %%E in ('dir/b/s sample*.txt') Do (
copy /y %%E %%~dpnE.old >nul)
:: parse txt files, create new temp files.
For /F %%J in ('dir/s/b sample*.txt') Do (
For /F "tokens=*" %%A in (%%J) Do (
Set Row=%%A
If !Option!==nowhitespace Call :Sub1
Echo !Row!>>%%~dpnJ.new))
:: rename temp files. cp *new *txt, rm *new
For /F %%M in ('dir/b/s sample*.new') Do (
copy /y %%M %%~dpnM.txt >nul && del %%M)
:: tab to space, two spaces to one, zero trailing spaces.
:Sub1
Set Row=!Row: = !
Echo !Row!|find/I " " >nul
If errorlevel 1 (break) else (Set Row=!Row: = !) & GoTo :Sub1
If "!Row:~-1,1!"==" " (Set Row=!Row:~0,-1!) & GoTo :Sub1
GoTo :EOF
Some of this I understand. There are four parts and a subroutine.
part1:
1 ::single space all txt files in the current tree...
2 @echo off
3 SetLocal EnableDelayedExpansion
4
5 If not %%1==() Set Flag=%1
6
Not sure why it must be %%1.
part2:
7 :: backup original files. cp *txt *old
8 For /F %%E in ('dir/b/s sample*.txt') Do (
9 copy /y %%E %%~dpnE.old >nul)
10
part3:
11 :: parse txt files, create new temp files.
12 For /F %%J in ('dir/s/b sample*.txt') Do (
13 For /F "tokens=*" %%A in (%%J) Do (
14 Set Row=%%A
15 If !Option!==nowhitespace Call :Sub1
16 Echo !Row!>>%%~dpnJ.new))
17
part4:
18 :: rename temp files. cp *new *txt, rm *new
19 For /F %%M in ('dir/b/s sample*.new') Do (
20 copy /y %%M %%~dpnM.txt >nul && del %%M)
21
subroutine:
22 :: tab to space, two spaces to one, zero trailing.
23 :Sub1
24 Set Row=!Row: = !
25 Echo !Row!|find/I " " >nul
26 If errorlevel 1 (break) else (Set Row=!Row: = !) & GoTo :Sub1
27 If "!Row:~-1,1!"==" " (Set Row=!Row:~0,-1!) & GoTo :Sub1
28 GoTo :EOF
Ok,
line 24 change a tab to a space.
line 25 find two spaces.
line 26 not found, quit.
found, change two spaces to one space.
line 27 not sure, maybe check for trailing space.
Can anyone explain this?
Also, had to have the subroutine. Or the goto on line 26, 27 will mess up the for loop.
If I was real smart, I'd figure out how to change the font for this site.
Or support code blocks like begin [code] end [/code]
These post are so hard to read. Can't tell one space from two spaces or one space from a tab.
Maybe even expand the margins? To make post more readable.
enjoy,