Jump to content

Batch removing spaces deletes everything

Guest
Go to solution Solved by PlutoNZL,
15 minutes ago, OTheB said:

Yep I did realise that. Now I have something like this (the actual code I'm working with):


@echo off
:start
cd %~dp0
for /f "tokens=*" %%G in (file.txt) do (
	set in=%%G
	set in=%in: =%
	echo %in%
)
pause

What is interesting is that if I look at the output with echo on, both the space removing command and the echo command don't behave as they should. The set command just sets %in% to be " =", then the echo does literally nothing. If I move them outside the parentheses, suddenly they both work, but only on the last line as that is what %in% is set to be last. These commands seem to be behaving differently whether they are inside or outside the loop, which is really confusing me.

Check this: http://stackoverflow.com/questions/13805187/how-to-set-a-variable-inside-a-loop-for-f

 

SETLOCAL ENABLEDELAYEDEXPANSION

@echo on
:start
cd %~dp0
for /f "tokens=*" %%G in (file.txt) do (
	set in=%%G
	set in=!in: =!
	echo !in!
)
pause

 

I have the following code:

@echo off
echo line=p o t a t o
set line=%line: =%
echo line=%line%

But when I run it, it displays the changed variable as blank. This also happens if I make the replacement for spaces any string such as:

@echo off
echo line=p o t a t o
set line=%line: =a%
echo line=%line%

Why does this happen? And how do I fix it? All I need to do is remove all the spaces.

Removing the "line=" bit makes it even worse. Normally when echo is off and I type "echo xyz", what will appear is "xyz", but now it just says echo is off (I am confused as hell right now).

Link to comment
Share on other sites

Link to post
Share on other sites

You're never setting line equal to "p o t a t o". You're just printing "line=p o t a t o".

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, PlutoNZL said:

You're never setting line equal to "p o t a t o". You're just printing "line=p o t a t o".

Yep I did realise that. Now I have something like this (the actual code I'm working with):

@echo off
:start
cd %~dp0
for /f "tokens=*" %%G in (file.txt) do (
	set in=%%G
	set in=%in: =%
	echo %in%
)
pause

What is interesting is that if I look at the output with echo on, both the space removing command and the echo command don't behave as they should. The set command just sets %in% to be " =", then the echo does literally nothing. If I move them outside the parentheses, suddenly they both work, but only on the last line as that is what %in% is set to be last. These commands seem to be behaving differently whether they are inside or outside the loop, which is really confusing me.

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, OTheB said:

Yep I did realise that. Now I have something like this (the actual code I'm working with):


@echo off
:start
cd %~dp0
for /f "tokens=*" %%G in (file.txt) do (
	set in=%%G
	set in=%in: =%
	echo %in%
)
pause

What is interesting is that if I look at the output with echo on, both the space removing command and the echo command don't behave as they should. The set command just sets %in% to be " =", then the echo does literally nothing. If I move them outside the parentheses, suddenly they both work, but only on the last line as that is what %in% is set to be last. These commands seem to be behaving differently whether they are inside or outside the loop, which is really confusing me.

Check this: http://stackoverflow.com/questions/13805187/how-to-set-a-variable-inside-a-loop-for-f

 

SETLOCAL ENABLEDELAYEDEXPANSION

@echo on
:start
cd %~dp0
for /f "tokens=*" %%G in (file.txt) do (
	set in=%%G
	set in=!in: =!
	echo !in!
)
pause

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×