cmd:open with的变量

时间:2010-01-01 13:45:54

标签: windows batch-file cmd

例如,当我想要一个批处理文件来“打开”一个文件时。例如,当我将文件拖放到批处理文件中时,它应该对该文件执行一些操作。

现在,我需要知道变量。我知道这种东西有一个变量;我忘了它。

有人可以给我变量吗?

感谢。

2 个答案:

答案 0 :(得分:3)

通过%1编写%9,可以访问批处理文件的前9个参数。

完整的命令行参数存储在%*

有关详细信息,请参阅here

答案 1 :(得分:0)

拖放到批处理文件可能是一项更加困难的工作 因为Windows不知道如何以正确的方式添加文件。

如果您的文件很简单,它会按预期工作。

1.txt
2 3.txt
4 & 5.txt


drag.bat 1.txt "2 3.txt" "4 & 5.txt"

但是有些文件名混淆了窗口......

6,7.txt
8&9.txt

drag.bat 6,7.txt 8&9.txt
-- results in --
%1 = 6
%2 = 7.txt
%3 = 8
%4 =
The command "9.txt" can not be found

在第一时刻,这似乎是一个不可能的问题,
但它存在一个解决方案。

诀窍是使用 cmdcmdline 变量而不是参数%1 ..%9 cmdcmdline包含类似

的内容
cmd /c ""C:\dragTest\test.bat" C:\dragTest\1.txt "C:\dragTest\2 3.txt" 
C:\dragTest\6,7.txt C:\dragTest\8&9.txt"

所以你可以使用它,但毕竟你必须停止你的批次,所以 9.txt 无法执行。

@echo off
setlocal ENABLEDELAYEDEXPANSION
rem Take the cmd-line, remove all until the first parameter
set "params=!cmdcmdline:~0,-1!"
set "params=!params:*" =!"
set count=0

rem Split the parameters on spaces but respect the quotes
for %%G IN (!params!) do (
  set /a count+=1
  set "item_!count!=%%~G"
  rem echo !count! %%~G
  rem Or you can access the parameter with, but this isn't secure with special characters like ampersand
  rem call echo %%item_!count!%%
)

rem list the parameters
for /L %%n in (1,1,!count!) DO (
  echo %%n #!item_%%n!#
)
pause

REM *** EXIT *** is neccessary to prevent execution of "appended" commands
exit