Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Greetings I'm looking to delete lines that have only spaces and no other characters.
I had this script to remove blank lines but it has issues with lines that have one or two spaces in it.
SetLocal EnableDelayedExpansion
For %%J in (test.tmp) Do (
For /F "tokens=* delims=" %%A in (%%J) Do (
Set Row=%%A
If not "!Row:~0,1!"=="#" (
:LOOP
If "!Row:~-1,1!"==" " (
(Set Row=!Row:~0,-1!) & GoTo :LOOP)
Set Row=!Row:tiplespawn 0=tiplespawn 1!
Echo !Row! >> %%~nJ_new.tmp)))Is there a way to fix this one or a new one will be fine. Thank you

If you are referring to the following lines in your originally posted code:
:LOOP
If "!Row:~-1,1!"==" " (
(Set Row=!Row:~0,-1!) & GoTo :LOOP)This appears to be an attempt to trim spaces off the end of the line. Unfortunately, the command processor doesn't like labels inside for loops. Try sticking the above in a subroutine and calling it from your for loop.

Hi
This help
@echo off
type nul > NewFile.txt
for /f "tokens=*" %%a in ('type Blanks.txt') do (
if not "%%a"=="" echo %%a >> Newfile.txt
)

To dtech10:
for /f "tokens=*" %%a in ('type Blanks.txt') do (
if not "%%a"=="" echo %%a >> Newfile.txt
)That's unnecessary, because the for command will automatically skip all blank lines. You will never get the condition "%%a"=="" that you're testing above.
I think the original poster wants to remove blanks from existing non-blank lines, as shown by his code.

@echo off > newfile
setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%b in (my.txt) do (
if not "%%b"=="" echo %%b >> newfile
)

Hi klint,
Amazing the way great minds think alike.
::===============================
"That's unnecessary, because the for command will automatically skip all blank lines. You will never get the condition "%%a"=="" that you're testing above."If you leave out that conditional, your newfile will contain"
echo is off
=====================================
If at first you don't succeed, you're about average.M2

I guess it's hard to explain. I'm starting with a file that are seperated by blank lines.
line1
line2
line3
Most of the time the code that I have works nicely to remove the blank lines to make it look like this
line1
line2
line2but I had a situation where in one of the blank lines there was a space character (if that makes sense)
line1
line2
line3Being in between line2 and line3 there is a space. So the code that I posted originally deletes the blank line between line1 and line2 but then errors when it hits the space character between line2 and line3 making the output file look like this
line1
line2and basically strips away the third line or anything below it (if i had 100 lines after it for example).
I hope that's a bit more clean

M2, and dtech10, I see how your code works now. The magic is in the fact that spaces are treated as delimiters. It causes leading spaces to be stripped off each line. So, if a line has nothing but spaces, it becomes an empty string. So you're right, you do need the conditional. I misunderstood the purpose, and thought you were trying to remove completely blank (empty) lines, which are automatically skipped.
So, neilblue, please try dtech10's or M2's suggestion, they both got it right.

dtech10's code works, although it adds a space at the end of each line as well. not sure why it would do this though.
M2's script just removes everything and the new file is blank.
This code will work just fine.
Thank you all for your help.

neilblue, the space is added because of the echo command:
if not "%%a"=="" echo %%a >> Newfile.txt
Just change it to
if not "%%a"=="" echo %%a>> Newfile.txt

Ah.. thank you klint
It actually didn't cause an issue when I tested it out. but it's good reference for future knowledge.
Thank you.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |