批处理 - 在文件名以20%空格替换为卷曲 [英] BATCH - Replacing spaces in filename with %20 for curl

查看:424
本文介绍了批处理 - 在文件名以20%空格替换为卷曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  FOR / F %% N的(new.txt)做,如果不存在#%电流%\\ %% N(
  平-N 1 127.0.0.1> NUL
  SETLOCAL EnableDelayedExpansion
  设定N = 1N:= %% 20!
  curl.exe --globoff -o%电流%\\ %% Nhttp://someserver.com/%%n
  平-n 3 127.0.0.1> NUL

我要取代所有空格%% n和递它卷曲,形成一个网址下载new.txt指定的文件之前。我试着包装在双引号和其他各种解决方案来代替空格,但没有似乎有任何效果。卷曲仍犯规形成正确的URL和之前削减空间的文件名。


解决方案

  FOR / Fdelims =%% N的(new.txt)做,如果不存在#%电流% \\ %% N(
    设置N = %% N
    SETLOCAL EnableDelayedExpansion
    设置N = N:!= %% 20
    curl.exe --globoff -o%电流%\\ %% N--urlhttp://someserver.com/!n!
    ENDLOCAL

您不能做字符串替换在命令替换参数/变量,就必须首先将其复制到一个单独的变量用它来操作。

此外,通过默认情况下,命令将分割/标记化使用空格和制表符分隔符的readed线,也就是被削减的第一个空间,为您的文件名的原因。放置在命令delims =选项禁用此行为。

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.

解决方案

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.

这篇关于批处理 - 在文件名以20%空格替换为卷曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆