2011-03-07 11 views
27

मुझे NAME द्वारा विंडोज बैच फ़ाइल में पैरामीटर पास करने में सक्षम होना चाहिए (और ऑर्डर द्वारा नहीं)। मेरा उद्देश्य यहां अंतिम उपयोगकर्ता को किसी भी क्रम में पैरामीटर पास करने के लिए लचीलापन देना है, और बैच फ़ाइल अभी भी उन्हें संसाधित करने में सक्षम होना चाहिए।क्या बैच। बैट फ़ाइल में पैरामीटर "नाम से" (और क्रमशः नहीं) पैरामीटर पास करने का कोई तरीका है?

एक उदाहरण मेरे सवाल का स्पष्ट करने के: somebatchfile.bat originalFile.txt newFile.txt

somebatchfile.bat अंदर (मूल फ़ाइल की सामग्री को कॉपी करने के लिए पहले पैरामीटर% एक सरल बयान है:

कमांड लाइन में

, उपयोगकर्ता निम्न करता है 1%) नई फ़ाइल में (दूसरा पैरामीटर% 2%)। यह निम्न कथन के रूप में सरल हो सकता है: copy %1% %2%

अब, यदि उपयोगकर्ता उपर्युक्त पैरामीटर को विपरीत क्रम में पास करता है, तो परिणाम वांछनीय (वास्तव में बहुत गलत) से दूर होगा।

तो, क्या उपयोगकर्ता नाम के आधार पर पैरामीटर पास करने का कोई तरीका है: उदा। somebatchfile.bat "SOURC=originalFile.txt" "TARGET=newFile.txt" और स्क्रिप्ट के लिए उन्हें पहचानने और सही स्थानों में'em 'का उपयोग करने के लिए। copy %SOURCE% %TARGET%?

धन्यवाद,

+0

यह सवाल लगता है कि: [कमांड लाइन निर्दिष्ट करें शैल स्क्रिप्ट के लिए नाम = मूल्य जोड़े जैसे तर्क] (http://stackoverflow.com/a/43008569/3957754) उम्मीद है कि इससे मदद मिलती है। – JRichardsz

+0

लेकिन ओपी प्रश्न _batch_ (विंडोज़/डॉस) के बारे में है, जो _bash_ से काफी अलग है। –

उत्तर

16

हाँ आप ऐसा ही कुछ कर सकता है, हालांकि मुझे नहीं लगता कि आप उपयोग कर सकते हैं "=" एक टोकन परिसीमक के रूप में करते हैं। आप एक कोलन ":", somebatchfile.bat "SOURC:originalFile.txt" "TARGET:newFile.txt" कह सकते हैं।

@echo off 

set foo=%1 
echo input: %foo% 

for /f "tokens=1,2 delims=:" %%a in ("%foo%") do set name=%%a & set val=%%b 

echo name: %name% 
echo value: %val% 

चल रहा है यह इस का उत्पादन होगा:: के लिए सोते समय के लिए बहुत करीब

C:\>test.bat SOURC:originalFile.txt 
input: SOURC:originalFile.txt 
name: SOURC 
value: originalFile.txt 

[संपादित करें]

ठीक है, शायद यह था यहाँ आप टोकन कैसे विभाजित हैं इसका एक उदाहरण है मुझे कल रात लेकिन फिर से देखकर, आप यह कर सकते हैं:

@echo off 

set %1 
set %2 

echo source: %SOURCE% 
echo target: %TARGET% 

जो इसे उत्पन्न करेगा (ध्यान दें कि मैं कमांड लाइन पर स्रोत और लक्ष्य उलट दिखाने के लिए वे सेट और सही ढंग से प्राप्त किए गए हैं):

C:\>test.bat "TARGET=newFile.txt" "SOURCE=originalFile.txt" 
source: originalFile.txt 
target: newFile.txt 

ध्यान दें कि% 1 और% 2 set इसलिए इन वातावरण चर के रूप में सेट मिलता है इससे पहले कि मूल्यांकन किया जाता है। वे को कमांड लाइन पर उद्धृत किया जाना चाहिए।

+0

धन्यवाद डेव, लेकिन आपके समाधान में बैच फ़ाइल के अंदर "काफी प्रसंस्करण" शामिल है। हालांकि, वांछनीय क्या है "अंतर्निर्मित" कार्यक्षमता प्रकार की चीज जो स्वचालित रूप से आंतरिक चर (उपयोगकर्ता की स्क्रिप्ट में) को पारित पैरामीटर के साथ पॉप्युलेट करती है। उदाहरण के लिए: सम्मेलन द्वारा, वे जानते हैं कि यदि वे इस तरह के पैरामीटर को पास करते हैं: "SOURC: originalFile.txt" तो उनके आंतरिक चर%%% और% val% स्वचालित रूप से सही मान प्राप्त करेंगे। – Pouya

+3

@Pouya: यह ** बैच स्क्रिप्ट ** कोड की केवल एक पंक्ति के साथ 'नाम और मूल्य' की जोड़ी को पार करती है। यह * प्रोसेसिंग का थोड़ा सा * कैसे हो सकता है? लेकिन फिर, मुझे लगता है कि आप अंतर्निहित कार्यक्षमता रखने के बारे में बात कर रहे हैं - ठीक है, नहीं, बैच स्क्रिप्टिंग काफी सीमित उपकरण है, जिसे माइक्रोसॉफ्ट ने (एक बार और) अप्रत्यक्ष रूप से पावरशेल पेश करके स्वीकार किया है। –

+0

एंड्री, समस्या यह है: हमारे मामले में, सभी बैच फ़ाइलों को अंतिम उपयोगकर्ताओं द्वारा प्रदान किया जाता है, और आम तौर पर आप अंतिम उपयोगकर्ताओं को अपने मौजूदा स्क्रिप्ट में कोड की एक भी पंक्ति जोड़ने के लिए नहीं कह सकते हैं ताकि वे संगत बना सकें आपके समाधान के साथ। – Pouya

2

मुझे लगता है कि आप विंडोज बैच स्क्रिप्ट्स में गेटोपट प्रकार का समर्थन ढूंढ रहे हैं जो दुर्भाग्य से पूरी तरह से मौजूद नहीं है। संभवतः आप संभवतः GetOpt.btm script का उपयोग कर सकते हैं। इस तो आप अपने स्क्रिप्ट का उपयोग कर आदेश पर अमल कर सकते हैं:

somebatchfile.bat /SOURC:originalFile.txt /TARGET:newFile.txt 

कोड:

@echo off 
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
:: GetOpt - Process command line options 
:: 
:: Michael Fross 
:: [email][email protected][/email] 
:: [url]http://fross.org[/url] 
:: 
:: This program scans the command line sent to it and sets various 
:: environment variables that coorespond to the settings. 
:: 
:: It sets an OPTION_arg variable for each arg on the command line. 
:: If a switch, the env var is set to 1. If a value is given via the colon sign, 
:: it's set to that value. Note, there can not be any white space around the ':' 
:: 
:: Use "If defined OPTION_arg" or "If %OPTION_arg eq value" to test for options 
:: 
:: It also sets a parameter variable for each paramater entered: PARAM_1 to PARAM_n 
:: PARAM_0 is a special value that contains the number of PARAMs. Useful for looping 
:: through all of them. For example, do i = 1 to %PARAM_0 by 1 ... 
:: 
:: In your batch file call getopt as: 
::  call GetOpt.btm %$ 
:: 
:: I also recommend setting setlocal and endlocal in the host batch file so that 
:: the option/param variable do not stick around after the host batch files exits. 
:: 
:: Example usage: BatchFile.btm /a /b:22 /longopt Parm1 Parm2 /quotedArg:"long quoted arg" 
:: OPTION_a will equal 1. 
:: OPTION_b will equal 22 
:: OPTION_quotedArg will equal "long quoted arg" 
:: OPTION_longopt will eqal 1. 
:: PARAM_1 will equal Parm1 
:: PARAM_2 will equal Parm2 
:: PARAM_0 will be set to the number of parms, so 2 in this case 
:: 
:: To get debug messages, set DEBUG=1. This will give detailed information for each 
:: parameter on the command line as getopt loops through the list. 
:: 
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 

:: Clean up the environment before we get going 
unset getopt* OPTION_* PARAM_* 
set getopt_ParmCounter=1 

:: If in debug mode, kick off the display by showing the number of arguments 
if defined DEBUG echo GetOpt is processing %# arguments: 

:: Loop through all command line arguments one at a time. 
for /L %i in (1,1,%#) do (
    if defined DEBUG (echo. %+ echo Scan #%i:) 

    :: If first character starts with a - or/it must be an option 
    iff %@instr[0,1,%[%i]] == - .or. %@instr[0,1,%[%i]] ==/then 
     set getopt_Parm=%[%i] 
     if defined DEBUG echo - Item "%getopt_Parm" is an option. 

     :: Set the Equal Index to the position of the colon. 0 means none was found 
     set getopt_EqIdx=%@index[%getopt_Parm,:] 

     :: Display the index position of the colon 
     if defined DEBUG .AND. %getopt_EqIdx GE 0 echo - Found colon at index position "%getopt_EqIdx" 

     :: If the index is GE 0 then we must have a colon in the option. 
     :: set the OPTION value to the stuff to the right of the colon 
     iff %getopt_EqIdx ge 0 then 
     set getopt_ParmName=%@instr[2, %@Dec[%getopt_EqIdx] , %getopt_Parm] 
     if defined DEBUG echo - ParmName = "%getopt_ParmName" 
     set getopt_ParmValue=%@right[%@eval[-%getopt_EqIdx-1],%getopt_Parm] 
     if defined DEBUG echo - Parmvalue = "%getopt_ParmValue" 
     set OPTION_%getopt_ParmName=%getopt_ParmValue 
     else 
     :: This is a flag, so simply set the value to 1 
     if defined DEBUG echo - No colon found in "%getopt_Parm" 
     set getopt_ParmName=%@right[%@Dec[%@len[%getopt_Parm]],%getopt_Parm] 
     set getopt_ParmValue=1 
     if defined DEBUG echo - ParmName = "%getopt_ParmName" 
     set OPTION_%getopt_ParmName=%getopt_ParmValue 
     endiff 

     :: Regardless if there was a value or not, display what is going to occur 
     if defined DEBUG echo - Setting Variable OPTION_%getopt_ParmName=%getopt_ParmValue 
    else 
     :: There was no/or - found, therefore this must be a paramater, not an option 
     if defined DEBUG echo - "%[%i]" is a parameter, not an option 
     set PARAM_%getopt_ParmCounter=%[%i] 
     set PARAM_0=%getopt_ParmCounter 
     if defined DEBUG echo - Updating Number of Parms. PARAM_0=%PARAM_0 
     if defined DEBUG echo - Setting Variable PARAM_%getopt_ParmCounter = %[%i] 
     set getopt_ParmCounter=%@Inc[%getopt_ParmCounter] 
    endiff 
) 

:: Display additional information 
iff defined DEBUG then 
    echo. 
    echo There were %PARAM_0 parameters found. Setting PARAM_0=%PARAM_0 
    echo. 
    echo GetOpt has completed processing %# arguments. Ending Execution. 
endiff 

:: Perform cleanup 
unset getopt_* 
4

मैं नाम के एक बैच कार्यक्रम को आपूर्ति पैरामीटर पढ़ने की संभावना देखना चाहती थी।उदाहरण के लिए:

myBatch.bat arg1 arg2 

पढ़ना पैरामीटर इस प्रकार किया जा सकता है:

%~1 would be arg1 
%~2 would be arg2 

बैच कार्यक्रम को आपूर्ति तर्क से ऊपर को पढ़ने के लिए है, लेकिन फिर हर बार जब आप इसे अमल में आसान है, आप व्यवस्था बनाए रखने और याद करने के लिए है क्या तर्क 1 होना चाहिए। इसे दूर करने के लिए, पैरामीटर को कुंजी के रूप में आपूर्ति की जा सकती है: मान प्रारूप। और कमांड नीचे दिखेगा:

mybatch.bar key1:value1 key2:value2 

हालांकि इस तरह की तर्क सूची को पार्स करने का कोई आसान तरीका नहीं है। मैंने बैच स्क्रिप्ट में लूप के लिए निम्नलिखित नेस्टेड लिखा था जो मूल रूप से पार्सल करेगा और key1 के साथ पर्यावरण चर बना देगा, और इसे मान दिया गया मान 1। इस तरह आप पर्यावरण चर पढ़ने के सीधे आगे के तरीके का उपयोग कर आपूर्ति की गई सभी तर्क पढ़ सकते हैं।

@echo off 
FOR %%A IN (%*) DO (
    FOR /f "tokens=1,2 delims=:" %%G IN ("%%A") DO setLocal %%G=%%H 
) 

बाद में, आप %key1% स्वरूप का उपयोग सभी तर्क आपूर्ति की जा रही पढ़ने के लिए कर सकते हैं।

HTH

+0

'setLocal' मेरे लिए काम नहीं किया लेकिन' सेट 'किया। –

+0

सेटलोकल आम तौर पर बैच प्रोग्राम के दायरे में काम करता है, मुझे यकीन नहीं है कि आप इन्हें किस सेट करने का प्रयास कर रहे थे। मैं इन मानकों को बैच स्कोप के भीतर उपयोग किया जाना चाहिए (और होना चाहिए) स्थानीय क्षेत्र पसंद करते हैं। आप इसे पढ़ने की कोशिश कैसे कर रहे थे? क्या आप इन पैरामीटर को पढ़ने का प्रयास करने से पहले एक एंडलोकल जारी किया गया था? –

+0

मैंने अपनी स्क्रिप्ट पर तर्क पारित करने के लिए [IzPack के निष्पादन योग्य] (http://izpack.org/documentation/installation-files.html#executable-mark-file-executable-or-execute-it) टैग का उपयोग किया था। मैंने बस इन तर्कों को फाइल करने के लिए लिखा है: 'INSTALL_PATH गूंजें:% INSTALL_PATH% >> text.txt'। –

4

अन्य तरह से मैं काफी पसंद किया है:

set c=defaultC 
set s=defaultS 
set u=defaultU 

:initial 
if "%1"=="" goto done 
echo    %1 
set aux=%1 
if "%aux:~0,1%"=="-" (
    set nome=%aux:~1,250% 
) else (
    set "%nome%=%1" 
    set nome= 
) 
shift 
goto initial 
:done 

echo %c% 
echo %s% 
echo %u% 

भागो निम्न आदेश:

users 
defaultS 
products 
+0

यह उन पैरामीटरों के साथ काम नहीं करता है जिनमें रिक्त स्थान हैं, इस मामले का समर्थन करने के लिए इसे कैसे सुधारया जा सकता है? – m0dest0

0
:parse 
IF "%~1"=="" GOTO endparse 
ECHO "%~1"| FIND /I "=" && SET "%~1" 
SHIFT /1 
GOTO parse 
:endparse 
:

arguments.bat -c users -u products 

निम्नलिखित उत्पादन तैयार करेगा

यह कोड सभी मानकों की जांच करता है, सभी बाहरी उद्धरणों को स्ट्रिप्स करता है, और यदि बराबर चिह्न मौजूद है, तो चर को वैरिएबल सेट करता है।

इस उत्तर के आधार पर: Using parameters in batch files at DOS command line

0

थोड़ा पार्टी के लिए देर से :) यह प्रबंध विकल्प "की तरह POSIX" के लिए मेरे सुझाव है। उदाहरण mybatchscript.bat -foo=foovalue -bar=barvalue -flag

:parseArgs 
:: asks for the -foo argument and store the value in the variable FOO 
call:getArgWithValue "-foo" "FOO" "%~1" "%~2" && shift && shift && goto :parseArgs 

:: asks for the -bar argument and store the value in the variable BAR 
call:getArgWithValue "-bar" "BAR" "%~1" "%~2" && shift && shift && goto :parseArgs 

:: asks for the -flag argument. If exist set the variable FLAG to "TRUE" 
call:getArgFlag "-flag" "FLAG" "%~1" && shift && goto :parseArgs 


:: your code here ... 
echo FOO: %FOO% 
echo BAR: %BAR% 
echo FLAG: %FLAG% 

goto:eof 

..और यहां कार्यों कि काम करने के लिए। आप mybatchscript.bat रूप

:: ===================================================================== 
:: This function sets a variable from a cli arg with value 
:: 1 cli argument name 
:: 2 variable name 
:: 3 current Argument Name 
:: 4 current Argument Value 
:getArgWithValue 
if "%~3"=="%~1" (
    if "%~4"=="" (
    REM unset the variable if value is not provided 
    set "%~2=" 
    exit /B 1 
) 
    set "%~2=%~4" 
    exit /B 0 
) 
exit /B 1 
goto:eof 



:: ===================================================================== 
:: This function sets a variable to value "TRUE" from a cli "flag" argument 
:: 1 cli argument name 
:: 2 variable name 
:: 3 current Argument Name 
:getArgFlag 
if "%~3"=="%~1" (
    set "%~2=TRUE" 
    exit /B 0 
) 
exit /B 1 
goto:eof 

सहेजें फ़ाइल एक ही बैच फ़ाइल में डाल और आप मिल जाएगा

mybatchscript.bat -bar=barvalue -foo=foovalue -flag 

चलाना चाहिए:

FOO: foovalue 
BAR: barvalue 
FLAG: TRUE 
संबंधित मुद्दे