Question

for /f %%n IN (new.txt) DO IF NOT EXIST "%current%\%%n" (
  ping -n 1 127.0.0.1 >nul
  setLocal EnableDelayedExpansion
  set n=1n: =%%20!
  curl.exe --globoff -o "%current%\%%n" ""http://someserver.com/%%n ""
  ping -n 3 127.0.0.1 >nul
)

I want to replace all spaces in %%n before handing it to curl to form a url to download the file specified in new.txt. I tried wrapping in double quotes and various other solutions to replace spaces, but none seem to have any effect. curl still doesnt form a proper url and cuts filenames before spaces.

Was it helpful?

Solution

for /f "delims=" %%n IN (new.txt) DO IF NOT EXIST "%current%\%%n" (
    set "n=%%n"
    setLocal EnableDelayedExpansion
    set "n=!n: =%%20!"
    curl.exe --globoff -o "%current%\%%n" --url "http://someserver.com/!n!"
    endlocal
)

You can not do string replacement in a for command replaceable parameter/variable, it is necessary to first copy it into a separate variable to operate with it

Also, by default, for command will split/tokenize the readed lines using spaces and tabs as delimiters, that is the reason for your file names being cut on the first space. Placing the "delims=" option in the for command disables this behaviour.

OTHER TIPS

You can't use the string replace syntax for a (FOR) parameter, this works only for variables.
But as you are in a code block you need to use delayed expansion.

Something like this should work

setlocal EnableDelayedExpansion
for /f "tokens=*" %%n IN (new.txt) DO IF NOT EXIST "%current%\%%n" (
  set "line=%%n"
  set "line=!line: =%%20!"
  curl.exe --globoff -o "%current%\%%n" ""http://someserver.com/!line! ""
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top