22

I am writing a file to remove spaces from filenames in a folder and then put the result in a .txt file. I just get a result of "Echo is on." over and over.

This is what I have so far:

@echo ON
SET LOCAL EnableDelayedExpansion
For %%# in (*.*) do (
    SET var=%%~n#
    Set MyVar=%var%
    set MyVar=%MyVar: =%
    echo %MyVar%>>text.txt
)

Can someone tell me whats wrong?

1
  • Did you call the batch file echo.bat/cmd ? Commented Apr 29, 2013 at 11:05

5 Answers 5

89

Removing all spaces (not just leading and trailing) can be done without using setlocal enabledelayedexpansionwith the following line:

set var=%var: =%

This works by replacing all spaces in the string with the empty string.

Source: DOS - String Manipulation

Sign up to request clarification or add additional context in comments.

2 Comments

Worked perfectly when I wanted to remove the space between a letter and a variable (e.g., "_p 011820181033" needed to be "_p011820181033"). Thanks!
This trick is valid to replace other characters as well: just put the desired character to remove instead of space. For example, set var=%var:.=% will remove all the dots in the variable, which may be useful if you have a variable containing a version number with dots and you need just the numbers.
14

The reason why you are getting ECHO is on. is because delayed expansion was not used, which caused the value of %var% and %MyVar% to be inserted before the for command is run, and since they were not defined at the start, empty variables were inserted in. When the echo %MyVar%>>text.txt was run, it was interpreted as echo >>text.txt. When echo is run without any arguments, it outputs whether echo is on or off, which is what you get in text.txt.

To fix the problem, you have to do two things:

First, there is something wrong with your second line. There is no space between set and local in setlocal. The second line should be SETLOCAL EnableDelayedExpansion.

Second, to use delayed expansion, you have to replace all %s in each variable with !, like !var! instead of %var%.

End result:

@echo ON
SETLOCAL EnableDelayedExpansion
For %%# in (*.*) do (
    SET var=%%~n#
    Set MyVar=!var!
    set MyVar=!MyVar: =!
    echo !MyVar!>>text.txt
)

You actually do not need to use a temporary variable in this case, you can just do SET MyVar=%%~n# and skip to set MyVar=!MyVar: =!.

Comments

2

The wrong thing is you've enabled the variable expansion (you wroted it bad...) and also you are not using it, when you use enabledelayedexpansion you need to write the variable names as this: !Variable! instead of this else: %Variable%

But you don't need to use it with this code:

@echo ON
For %%# in (*) do (
    SET "var=%%~n#"
    Call Set "MyVar=%%var: =%%"
    Call echo %%MyVar%%>>text.txt
)

1 Comment

@foxidrive, It works, sorry but... you've tested it before comment?.
0

Run the following batch in the folder holding the files to be renamed

    @echo off
    setlocal enabledelayedexpansion
    for %%j in (*.*) do (
    set filename=%%~nj
    set filename=!filename=.=_!
    set filename=!filename= =_!
    if not "!filename!"=="%%~nj" ren "%%j" "!filename!%%~xj"
    )

you just need to add the print to txt

1 Comment

that just removed all the file names and left the extensions
0

The set var=%var: =% did not work for me. So I tried with success for a number the following code: set /a var-=1 & set /a var+=1

1 Comment

surely doesn't work for an alphanumeric string (which file names usually are). To remove (leading and/or trailing) spaces from a supposed number, just set /a var=var should do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.