[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

批处理传参中带"="问题, 传参和保留字, 求助

本帖最后由 daols 于 2012-12-16 14:06 编辑
  1. @echo off
  2. ::setlocal enableextensions
  3. ::setlocal enabledelayedexpansion
  4. set wgetex=%~dp0wget.exe
  5. set autodir="%tmp%"
  6. set logfile="%tmp%\wgetx.log"
  7. set wget="%ComSpec%" /k %wgetex% --no-check-certificate -c -S -t 0 -U "Opera/9"
  8. if not exist "%wgetex%" (echo Error: "%wgetex%" not found.&&pause&&goto end)
  9. if /I [%1]==[] (goto usage) else if /I [%1]==[/?] (goto usage) else if /I [%1]==[--help] (goto usage)
  10. if exist "%windir%\tray.exe" (set wget="%windir%\tray.exe" %wget%)
  11. if /I [%2] NEQ [] (set wget=%wget% %~3 %~4 %~5 %~6 %~7 %~8 %~9 -P %2) else (set wget=%wget% %~3 %~4 %~5 %~6 %~7 %~8 %~9 -P %autodir%)
  12. if exist %1 (set wget=%wget% -i %1) else (set wget=%wget% %1)
  13. if defined logfile (echo %date:~0,10% # %time%>>%logfile%&&echo line: %0 %*>>%logfile%&&echo exec: start "%0" %wget%>>%logfile%&&echo.>>%logfile%)
  14. start "%0" %wget%
  15. ::pause
  16. goto end
  17. :usage
  18. echo Usage :
  19. echo %0 "$URL" [Save to %autodir% by default]
  20. echo %0 "$URL" "$SavePath"
  21. echo %0 "$UrlListFile.txt" "$SavePath"
  22. echo %0 "$URL" "$SavePath" "--referer=$URL -r -p -np -k"
  23. echo note:
  24. echo 1.前两个参数默认为 "url[或url列表文件]路径" 和 "下载位置",
  25. echo   如果有其他[最多9个]参数, 会自动追加传递给wget
  26. echo   [比如: [wgetx . . --help] 会自动调用wget帮助],
  27. echo 2.内部有[=]或[^"]等保留字的参数外部需加双引号,
  28. echo   %%3及其以后的参数会被自动脱掉最外层双引号,
  29. echo   这样可以绕过9参数上限并不受参数中 [=] 号的影响.
  30. echo 3.参数内部有多个 [=] 号时不能同时存在双引号,
  31. echo   且参数中不能有空格, 同时要在整个参数外部加双引号.
  32. echo 4.旧版本 wget-1.10.2 支持--no-check-certificate, 新版不支持,
  33. echo 5.在使用 [start "%ComSpec%" ...] 命令的脚本中,
  34. echo   各版本wget.exe路径外都不可带引号, 会报找不到文件错误,
  35. echo   所以程序路径中不能有空格和中文.
  36. echo demo:
  37. echo %wgetex% --no-check-certificate -c -S -t 0 -U "Opera/9" -P "%autodir%" -i "$UrlList.txt"
  38. echo %wgetex% --no-check-certificate -c -S -t 0 -U "Opera/9" -P "%autodir%" "http://users.ugent.be/~bpuype/wget/wget.exe"
  39. echo %wgetex% --no-check-certificate -c -S -t 7 -U "Opera/9" -P "%autodir%" -r -p -np -k "http://docs.gimp.org/2.2/zh_CN/"
  40. echo.
  41. pause
  42. :end
  43. ::# 1. IF [/I] string1 compare-op[EQU/NEQ...] string2 中的 string1 里有 [=] 号的时候 string1 外不能有双引号, 否则会忽略 /I 以及 compare-op 运算, 并优先被当作 IF string1==string2 命令处理而报错. 可以用 [ ] 括号来代替引号, 如: if /I [%1] EQU [] ...
  44. ::# 2. 用 %n 传参时如果参数中有一个 [=] 号并带双引号, 须在整个参数外用双引号, 处理时用 %~n 来脱引号, 否则参数会被 = 分割成两个, 如 [--referer="$URL" -r -p -np -k] 五个参数可用 ["--referer="$URL" -r -p -np -k"] 合并成一个来传入. 这样可以绕过9参数上限并不受参数中 [=] 号的影响.
  45. ::# 目前的问题是: 像 [--referer="$URL/?a=b"] 这样带更多 [=] 号和引号的参数仍没有方法正确传递.
  46. ::# 测试: wgetx "$URL/?1=a" "2=b" "--referer="$URL/?3=c"" "-r -p -np -k"
  47. ::# 显示: start "wgetx" "C:\WIN2003\tray.exe" "C:\WIN2003\system32\cmd.exe" /k C:\WIN2003\wget.exe --no-check-certificate -c -S -t 0 -U "Opera/9" --referer="$URL/?3 c"" -r -p -np -k     -P "2=b") else (set wget="C:\WIN2003\tray.exe" "C:\WIN2003\system32\cmd.exe" /k C:\WIN2003\wget.exe --no-check-certificate -c -S -t 0 -U "Opera/9" --referer="$URL/?3 c"" -r -p -np -k     -P "B:\TEMP" "$URL/?1=a"
  48. ::# 也就是说参数内部有多个 [=] 时不能同时存在双引号, 且参数中不能有空格, 同时要在整个参数外部加双引号.
复制代码

起..........

TOP

  1. start "%0" %wget% %~3 %~4 %~5 %~6 %~7 %~8 %~9
复制代码
wgetx "a=b" 2 3 4 5 6 7 8

TOP

回复 3# BAT-VBS
不管事...问题不在 %3456789上...而在于%1那个"="号...
看后面参数...a=b被分割成了 a b 两个参数...
  1. B:\>wgetx "a=b" 2 3 4 5 6 7 8 9
  2. 此时不应有 =b""。
  3. B:\>wgetx a=b 2 3 4 5 6 7 8 9
  4. start "wgetx" "C:\WIN2003\tray.exe" "C:\WIN2003\system32\cmd.exe" /k C:\WIN2003\wget.exe --no-check-certificate -c -S -t 0 -U "Opera/12.00" "a" -P b 2 3 4 5 6 7 8
  5. 请按任意键继续. . .
复制代码

TOP

我重新修改了一下主贴...让它看起来更明确点...

TOP

回复 4# daols


    把@echo off删掉,看看是哪一行在报错。

TOP

多谢楼上BAT-VBS,,现在解决了一个问题
  1. 此时不应有 =b""。
  2. B:\>if /I ""a=b"" EQU "" (goto usage)
复制代码
改成: if /I [%1] EQU [] (goto usage), 所有if后的EQU和NEQ比较运算都用[]来标示...不能用双引号.
问题出在 if 上, if 将他后面紧接着的 "a=b" 当作 IF string1==string2 command 这种字符串比较的表达式来处理了, 后面的EQU运算被忽略了 , 我将引号改成中括号之后[a=b]就被当作字符串处理了, 字符串里有等号时外边不能用双引号, 只要双引号里带等号就会被当做字符串比较表达式而报错...

现在出现了第二个等号的问题, 这是个不会报错的 :
  1. B:\>wgetx "$url/?1=1" "$SavePath" --referer="$URL"
  2. start "wgetx" "C:\WIN2003\tray.exe" "C:\WIN2003\system32\cmd.exe" /k C:\WIN2003\wget.exe --no-check-certificate -c -S -t 0 -U "Opera/12.00" "$url/?1=1" -P "$SavePath" --referer "$URL"
  3. B:\>wgetx "$url/?1=1" "$SavePath" --referer^="$URL"
  4. start "wgetx" "C:\WIN2003\tray.exe" "C:\WIN2003\system32\cmd.exe" /k C:\WIN2003\wget.exe --no-check-certificate -c -S -t 0 -U "Opera/12.00" "$url/?1=1" -P "$SavePath" --referer "$URL"
复制代码
可见传入参数中的 [--referer="$URL"] 是一个整体字符串, 但是因为存在"="号, 被处理后分割成了两个, 这样命令就不对了...这个应该怎么解决呢...= =|"

TOP

本帖最后由 daols 于 2012-12-15 06:39 编辑

现在解决了两个问题(主贴已更新):
1. IF [/I] string1 compare-op[EQU/NEQ...] string2 中的 string1 里有 [=] 号的时候 string1 外不能有双引号, 否则会忽略 /I 以及 compare-op 运算, 并优先被当作 IF string1==string2 命令处理而报错.
2. %n传参时如果参数中有一个 [=] 号并带双引号, 须在整个参数外用双引号, 处理时用 %~n 来脱引号, 否则参数会被 = 分割成两个, 如 [--referer="$URL" -r -p -np -k] 五个参数可用 ["--referer="$URL" -r -p -np -k"] 合并成一个来传入.

::# 目前的问题是: 像 [--referer="$URL/?a=b"] 这样带更多 [=] 号和引号的参数仍没有方法正确传递.
::# 测试: wgetx "$URL/?1=a" "2=b" "--referer="$URL/?3=c"" "-r -p -np -k"
::# 显示: start "wgetx" "C:\WIN2003\tray.exe" "C:\WIN2003\system32\cmd.exe" /k C:\WIN2003\wget.exe --no-check-certificate -c -S -t 0 -U "Opera/9" --referer="$URL/?3 c"" -r -p -np -k     -P "2=b") else (set wget="C:\WIN2003\tray.exe" "C:\WIN2003\system32\cmd.exe" /k C:\WIN2003\wget.exe --no-check-certificate -c -S -t 0 -U "Opera/9" --referer="$URL/?3 c"" -r -p -np -k     -P "B:\TEMP" "$URL/?1=a"

TOP

返回列表