Board logo

标题: [求助]文件拖动到批处理后的参数问题 [打印本页]

作者: h2thesun     时间: 2008-3-3 13:44    标题: [求助]文件拖动到批处理后的参数问题
if exist autorun.inf goto exist
echo.>autorun.inf
:exist
echo [autorun]>> autorun.inf
echo icon=%1>> autorun.inf
pause
以上是一个制作U盘图标的代码,如果直接运行可以生成autorun.inf的文件,但是如果拖动一个图标文件到该批处理时就不能生成autorun.inf文件,而且发现运行的代码也比较奇怪。高手请教一下。

作者: HAT     时间: 2008-3-3 15:10
重定向出问题了

if exist autorun.inf goto exist
>autorun.inf echo.
:exist
>>autorun.inf echo [autorun]
>>autorun.inf echo icon=%1
pause

作者: h2thesun     时间: 2008-3-3 16:18
Originally posted by HAT at 2008-3-3 03:10 PM:
重定向出问题了

if exist autorun.inf goto exist
>autorun.inf echo.
:exist
>>autorun.inf echo
>>autorun.inf echo icon=%1
pause

还是不行啊。文件拖上去还是不运行。

作者: mkd     时间: 2008-3-3 21:10
在最前面加上一行
cd /d "%~dp1"


如果 %1 沒有空格,不可以有 =

作者: h2thesun     时间: 2008-3-3 23:24
我自己发现如果重定向的文件是有具体路径的话(例子里就是可以把原来的autorun.inf改成l:\autorun.inf等等)就不会出问题。
现在又细看了一下mkd兄的方法,原因找到了,原来在其它文件拖到批处理文件上运行的时候,cmd中的运行目录是在C:\Documents and Settings\Administrator的默认目录下运行的,而不指定绝对路径的autorun.inf也是生成在C:\Documents and Settings\Administrator下的(后来去检查了一下,果然有一个autorun.inf在)。
但是mkd的这句不懂啊:”如果 %1 沒有空格,不可以有 =“ 是什么意思呢?

作者: mkd     时间: 2008-3-4 23:08
aa.bat

@echo off
echo %cmdcmdline%
echo.%1
echo.%2
echo.%3
echo.%4
echo.%5
echo.%6
echo.END
pause >nul


將x,y.txt x;y.txt x=y.txt 拖到aa.bat


cmd /c ""C:\aa.bat" C:\x,y.txt C:\x;y.txt C:\x=y.txt"
C:\x
y.txt
C:\x
y.txt
C:\x
y.txt
END



將x ,y.txt x ;y.txt x =y.txt 拖到aa.bat

cmd /c ""C:\aa.bat" "C:\x ,y.txt" "C:\x ;y.txt" "C:\x =y.txt""
"C:\x ,y.txt"
"C:\x ;y.txt"
"C:\x =y.txt"



END



將C:\A B\x,y.txt C:\A B\x;y.txt C:\A B\x=y.txt 拖到C:\A B\aa.bat

cmd /c ""C:\A B\aa.bat" "C:\A B\x,y.txt" "C:\A B\x;y.txt" "C:\A B\x=y.txt""
"C:\A B\x,y.txt"
"C:\A B\x;y.txt"
"C:\A B\x=y.txt"



END



http://www.cn-dos.net/forum/viewthread.php?tid=31223