Page 1 of 1
tokens: how to remove all the spaces between words?
Posted: 24 Oct 2011 05:01
by tinfanide
Code: Select all
@ECHO OFF
FOR /F "tokens=1*" %%G IN (removeSpace-txt.txt) DO @echo %%G%%H%%I
pause
How can I remove all the spaces?
I know I can do things like
Code: Select all
FOR /F "tokens=1,2,3" %%G IN (txt.txt) DO @echo %%G%%H%%I
But it doesn't work for irregular numbers of spaces per each line.
My txt file:
Good Morning Boys
Good Afternoon Girls
Good Night Boys Girls Dogs
Re: tokens: how to remove all the spaces between words?
Posted: 24 Oct 2011 05:18
by dbenham
You should use string substitution which is documented under SET (HELP SET)
Code: Select all
@echo off
setlocal disableDelayedExpansion
for /f "delims=" %%L in (test.txt) do (
set "ln=%%L"
setlocal enableDelayedExpansion
echo(!ln: =!
endlocal
)
If you know that the file does not contain ! then you can enable delayed expansion at the top and remove the SETLOCAL and ENDLOCAL within the loop.
Dave Benham
Re: tokens: how to remove all the spaces between words?
Posted: 24 Oct 2011 19:47
by Rileyh
You could also try using "delims= " to remove space lists:
Code: Select all
for /f "tokens=1,2,3 delims= " %%g IN (txt.txt) DO (@echo %%G %%H %%I)
pause
Could try that, else try:
Code: Select all
for /f "tokens=* delims= " %%g IN (txt.txt) DO (@echo %%g %%h %%i)
pause
Glad to help,
Rileyh
Re: tokens: how to remove all the spaces between words?
Posted: 25 Oct 2011 08:17
by tinfanide
Rileyh wrote:You could also try using "delims= " to remove space lists:
Code: Select all
for /f "tokens=1,2,3 delims= " %%g IN (txt.txt) DO (@echo %%G %%H %%I)
pause
Could try that, else try:
Code: Select all
for /f "tokens=* delims= " %%g IN (txt.txt) DO (@echo %%g %%h %%i)
pause
Glad to help,
Rileyh
Sorry, those two are not working on my computer.
It returns something like
%%g %%h %%i
Re: tokens: how to remove all the spaces between words?
Posted: 25 Oct 2011 09:14
by tinfanide
dbenham wrote:You should use string substitution which is documented under SET (HELP SET)
Code: Select all
@echo off
setlocal disableDelayedExpansion
for /f "delims=" %%L in (test.txt) do (
set "ln=%%L"
setlocal enableDelayedExpansion
echo(!ln: =!
endlocal
)
If you know that the file does not contain ! then you can enable delayed expansion at the top and remove the SETLOCAL and ENDLOCAL within the loop.
Dave Benham
Just one little question that I cannot search the Internet for explanation:
What is "(" and ":"?
What are their uses?
Re: tokens: how to remove all the spaces between words?
Posted: 25 Oct 2011 13:39
by dbenham
tinfanide wrote:Just one little question that I cannot search the Internet for explanation:
What is "(" and ":"?
Well that really is 2 questions dealing with 2 unrelated subjects
ECHO( is a way of echoing a blank line. If the line we are attempting to echo is empty and we use simply
ECHO then you will get
ECHO is on. as the output. This is a possibility with your code because you could run accross a line that consists solely of spaces, which then get stripped to a blank line.
Most people use
ECHO. instead of
ECHO(, but
this thread demonstrates shortcomings of
ECHO. and there is a general consensus that
ECHO( is the safest variant.
The colon (:) is part of the syntax used for string substitution. The code is using !ln! instead of %ln% to get delayed expansion because it is inside a FOR loop. %ln% would give the value of ln before the LOOP was parsed - which would be empty: not what we want
-
!VAR! gives the value of VAR
-
!VAR:SEARCH=REPLACE! gives the value of VAR after any occurrance of SEARCH is replaced by REPLACE.
So
!ln: =! simply gives the value of ln after all instanses of <space> have been stripped (replaced with nothing).
Use HELP SET to get more info on delayed expansion and string substitution.
Dave Benham
Re: tokens: how to remove all the spaces between words?
Posted: 26 Oct 2011 07:44
by tinfanide
ECHO( is a way of echoing a blank line. If the line we are attempting to echo is empty and we use simply
ECHO then you will get
ECHO is on. as the output. This is a possibility with your code because you could run accross a line that consists solely of spaces, which then get stripped to a blank line.
Most people use
ECHO. instead of
ECHO(, but
this thread demonstrates shortcomings of
ECHO. and there is a general consensus that
ECHO( is the safest variant.
[/quote]
Yeah, just very new to BAT and just have enough knowledge to work on the first part.
I've tested like this
Code: Select all
@echo off
@echo off
echo Good Morning
echo.
echo Good Afternoon
echo(
echo Good Evening
echo:
echo Good Night
pause
The three are the same to making a line break.
And CMD tells me it is only the "echo)" not the internal or external command
It seems that echo)
Re: tokens: how to remove all the spaces between words?
Posted: 26 Oct 2011 08:28
by tinfanide
dbenham wrote:The colon (:) is part of the syntax used for string substitution. The code is using !ln! instead of %ln% to get delayed expansion because it is inside a FOR loop. %ln% would give the value of ln before the LOOP was parsed - which would be empty: not what we want
-
!VAR! gives the value of VAR
-
!VAR:SEARCH=REPLACE! gives the value of VAR after any occurrance of SEARCH is replaced by REPLACE.
So
!ln: =! simply gives the value of ln after all instanses of <space> have been stripped (replaced with nothing).
Use HELP SET to get more info on delayed expansion and string substitution.
Dave Benham
For this part, I've played with your sample codes to try getting something out of it.
Code: Select all
@echo off
setlocal disableDelayedExpansion
for /f "delims=_" %%L in (test1.txt) do (
set "ln=%%L"
setlocal enableDelayedExpansion
echo(!ln:_=~!
endlocal
)
pause
My question is why in this case I need
to get all the "_" across all the items on each line
but in your example searching " "
you don't need to include that
Can you tell me why?
Re: tokens: how to remove all the spaces between words?
Posted: 26 Oct 2011 14:49
by dbenham
Because in my example I disabled token parsing entirely by using "DELIMS=".
With your code you you set DELIMS=_, so by design it will split your line into tokens at every _ (consecutive delimiters are treated as one).
If you use "delims=" it will preserve the entire line.
If you use "tokens=* delims=_" it will strip any leading underscores until it reaches a non-underscore character, and then everything from then on will be preserved.
Dave Benham
Re: tokens: how to remove all the spaces between words?
Posted: 27 Oct 2011 09:14
by tinfanide
Code: Select all
@echo off
dir /b /a-d *.* > temp.txt
setlocal disableDelayedExpansion
for /f "delims=" %%L in (temp.txt) do (
set "ln=%%L"
setlocal enableDelayedExpansion
echo(!ln: =_!
endlocal
)
pause
I've tried to turn filenames with spaces within into a text file that contains the chanages:
Original filename:
test 1.txt
Changed to
test_1.txt
But this is only printed in the temp.txt file
how can I actually apply the change to the real filenames?