2010-01-01 50 views
0

例如,我想要一個批處理文件來'打開'一個文件。當我例如拖放文件到批處理文件,它應該做一些與該文件的東西。cmd:開放的變量

現在,我需要知道變量。我知道這種東西有一個變量,我只是忘了它。

有人可以給我的變量嗎?

謝謝。

+1

superuser.com可能更適合這裏 – 2010-01-01 13:48:51

+0

沒有。我特意在這裏詢問代碼,而不是與編程無關的東西。 – 2010-01-01 14:30:56

回答

3

通過編寫%1%9可以訪問批處理文件的前9個參數。

完整的命令行參數存儲在%*中。請參閱here

+0

謝謝!工作正常。 – 2010-01-01 13:52:16

+1

您也可以使用'shift'來移除'%1'並將每個剩餘的參數移動一個地方。通過這種方式,您可以訪問之前在12位左右的參數。 – Joey 2010-01-01 14:42:00

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