0

I'm trying to strip spaces and commas from %%~ni. I've managed to do it by putting it into my new variable !url!. It echos fine, but does anyone know why I can't use it here: set "line=!line:REPLACE=!url!!" ?

@echo off
setlocal enableextensions enabledelayedexpansion
for /R %%i in (*.csv) do (
ren "%%~i" temp.tmp
(for /F "usebackq tokens=*" %%f in ("%%~dpitemp.tmp") do (
        set "line=%%f"
        echo %%~ni

        set url=%%~ni
        set url=!url: =-!
        set url=!url:,=!
        echo !url!
        set "line=!line:REPLACE=!url!!"


        echo(!line!
)) > "%%~i"
del "%%~dpitemp.tmp"
)
Siwap
  • 63
  • 8
  • 1
    `set "line=!line:REPLACE=!url!!"` cannot work, but you could do `for /F "delims=" %%e in ("!url!") do set "line=!line:REPLACE=%%e!"`... – aschipfl Aug 10 '18 at 13:09
  • `set url=!url:(=!` `set url=!url:)=!` Quick question, do you know why brackets wont work in here? – Siwap Aug 10 '18 at 13:27
  • 1
    Use the quoted syntax like `set "url=!url:(=!"` and `set "url=!url:)=!"` to protect such special characters from being recognised by the parser... – aschipfl Aug 10 '18 at 13:30
  • Perfect, thanks a lot for all your help (: – Siwap Aug 10 '18 at 14:19
  • You may also use `call set "line=%%line:REPLACE=!url!%%"`. I suggest you to read [this answer](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990). Altough the topic is different, the problem and the way to solve it is the same... – Aacini Aug 10 '18 at 14:50

1 Answers1

2

Answer for anyone else having this problem:

@echo off
setlocal enableextensions enabledelayedexpansion
for /R %%i in (*.csv) do (
ren "%%~i" temp.tmp
(for /F "usebackq tokens=*" %%f in ("%%~dpitemp.tmp") do (
        set "line=%%f"
        set url=%%~ni
        set "url=!url: =-!"
        set "url=!url:,=!"
        set "url=!url:(=!"
        set "url=!url:)=!"
        for /F "delims=" %%e in ("!url!") do set "line=!line:REPLACE=%%e!"
        echo(!line!
)) > "%%~i"
del "%%~dpitemp.tmp"
)
Siwap
  • 63
  • 8