Board logo

标题: 如何使用两个变量? [打印本页]

作者: threesecond     时间: 2008-1-9 12:38    标题: 如何使用两个变量?
我有一个文字文文件 file_list.dat 范例如下
http://download.microsoft.com/download/5/b/e/5beaf800-bbbc-4078-a56b-d2dba58dbf0c/WindowsXP-KBxxxxxxxx.exe
http://download.microsoft.com/download/b/8/0/b80e4fc4-eafb-4aa7-b18b-e77b33fc3074/WindowsXP-KB xxxxxxxx.exe
http://download.microsoft.com/download/2/e/0/2e0f0bf9-6eed-41e1-8279-c3ba6c1d68a1/WindowsXP-KB xxxxxxxx.exe

我想做的批处理如下:
::撷取网址中的最后一个文件名
FOR /F "tokens=8 delims=/ " %%a in (file_list.dat) do set file_name=%%a
::撷取整列网址
FOR /F %%b in (file_list.dat) do set file_url=%%b
::然后我想判断文件是否存在,若不存在则下载,且要 for ….do 循环处理多列,
if exit % file_name % (
xcopy %file_name% Folder\ /y
) else (
wget %file_url%
)
pause
exit

但始终不成功,请各位帮我看看该如何写?

Last edited by threesecond on 2008-1-9 at 12:41 PM ]

作者: abcd     时间: 2008-1-9 12:41
是提取这个??WindowsXP-KB xxxxxxxx-exe

还是这个xxxxxxxx-exe

作者: threesecond     时间: 2008-1-9 12:44
%file_name% 需提取WindowsXP-KBxxxxxxxx.exe
%file_url% 则提取整个 URL (含WindowsXP-KBxxxxxxxx.exe)
以上两个提取已经以 echo 测试 ok,但合并使用在下面的 if exit 就出问题了。

作者: chenall     时间: 2008-1-9 12:47
没有说清楚.
::撷取网址中的最后一个文件名
是要每个网址后面的文件名还是要列表中的最后一个文件名.
按你的
FOR /F "tokens=8 delims=/ " %%a in (file_list.dat) set file_name=%%a
应该是要每个网的最后一个文件名,但是这个语句的结果是取列表的最后一个文件名.

我按照我的理解给你改一个,不知是否可用.

刚发完,返回就发现已经有好几个回贴了,可以结合5楼的修改一下.
是个不错的方法.



  1. FOR /F %%b in (file_list.dat) (
  2. set file_url=%%b
  3. call :check
  4. )
  5. goto :eof

  6. :check
  7. if not defined file_url goto :eof
  8. FOR /F "tokens=8 delims=/ " %%a in (%file_url%) set file_name=%%a
  9. if exit %file_name% (
  10. xcopy %file_name% Folder\ /y
  11. ) else (
  12. wget %file_url%
  13. )
  14. goto :eof
chenall 发表于: 2008-01-09 09:25


Last edited by chenall on 2008-1-9 at 12:51 PM ]

作者: abcd     时间: 2008-1-9 12:48
@echo off
for /f "delims=" %%i in (file_list.dat) do (
if not exist "%%~nxi" wget "%%i"
)

作者: threesecond     时间: 2008-1-9 15:13
Originally posted by chenall at 2008-1-9 12:47 PM:
没有说清楚.
::撷取网址中的最后一个文件名
是要每个网址后面的文件名还是要列表中的最后一个文件名.
按你的
FOR /F "tokens=8 delims=/ " %%a in ...

FOR /F %%b in (file_list.dat) (
set file_url=%%b
call :check
)
goto :eof


:check
if not defined file_url goto :eof
FOR /F "tokens=8 delims=/ " %%a in (%file_url%) set file_name=%%a
if exit %file_name% (
xcopy %file_name% Folder\ /y
) else (
wget %file_url%
)
goto :eof

我要提取每一个网址最后的文件名
我想您理解我的意思了,但仍有些小问题,
第1和第10列应该加上 do 才能运行 set 是吧?
我修改了后,运行到第10列,%file_name% 却变成了 URL 网址了,而非文件名,

FOR /F "tokens=8 delims=/ " %%a in (%file_url%) set file_name=%%a
这行的in (%file_url%) 会变成找不到文件,
(将网址当作文件名了,当然找不到了)
该怎么办?

Last edited by threesecond on 2008-1-9 at 03:22 PM ]

作者: lxmxn     时间: 2008-1-9 15:45    标题: 试试这个,没有测试:
@echo off & SetLocal EnableDelayedExpansion
for /f "delims=" %%a in (file_list.dat) do (
set filename=%%~nxa
if not exist "!filename!" (wget -q "%%~a") else (copy /y "!filename!" folder)
)

作者: threesecond     时间: 2008-1-9 17:12
试用七楼的办法,稍修改一下就成功了,
果然还是需要SetLocal EnableDelayedExpansion 才能成功哪~~

作者: zh159     时间: 2008-1-9 17:17
7 楼应该可以改为:

for /f "delims=" %%a in (file_list.dat) do if not exist "%%~nxa" (wget -q "%%~a") else (copy /y "%%~nxa" folder)

自己测试