Sie sind auf Seite 1von 4

19/10/2016

(SS64)

Parameters/Arguments|WindowsCMD|SS64.com

CMD

Syntax

Search

CommandLinearguments(Parameters)
Acommandlineargument(orparameter)isanyvaluepassedintoabatchscript:
C:>MyScript.cmdJanuary1234"Somevalue"
ArgumentscanalsobepassedtoasubroutinewithCALL:
CALL:my_sub2468
Youcangetthevalueofanyargumentusinga%followedbyit'snumericalpositiononthecommandline.The
firstitempassedisalways%1theseconditemisalways%2andsoon
%*inabatchscriptreferstoallthearguments(e.g.%1%2%3%4%5...%255)
onlyarguments%1to%9canbereferencedbynumber.

ParameterExtensions
Whenanargumentisusedtosupplyafilenamethenthefollowingextendedsyntaxcanbeapplied:
weareusingthevariable%1(butthisworksforanyparameter)
%~f1Expand%1toaFullyqualifiedpathnameC:\utils\MyFile.txt
%~d1Expand%1toaDriveletteronlyC:
%~p1Expand%1toaPathonlye.g.\utils\thisincludesatrailing\whichwillbeinterpretedas
anescapecharacterbysomecommands.
%~n1Expand%1toafileNamewithoutfileextensionC:\utils\MyFileorifonlyapathis
present(withnotrailingbackslash\)thelastfolderinthatpath.
%~x1Expand%1toafileeXtensiononly.txt
%~s1Changethemeaningoff,n,sandxtoreferencetheShort8.3name(ifitexists.)
%~1Expand%1removinganysurroundingquotes(")
%~a1Displaythefileattributesof%1
%~t1Displaythedate/timeof%1
%~z1Displaythefilesizeof%1
%~$PATH:1SearchthePATHenvironmentvariableandexpand%1tothefullyqualifiednameofthe
firstmatchfound.
Themodifiersabovecanbecombined:
%~dp1Expand%1toadriveletterandpathonly
%~sp1Expand%1toapathshortenedto8.3characters
%~nx2Expand%2toafilenameandextensiononly
Theseparameter/argumentvariablesarealwaysdenotedwithasingleleading%
Thisisunlikeregularvariableswhichhavebothleadingandtrailing%'ssuchas%variable%,orFOR
commandvariableswhichuseasingleleading%onthecommandlineoradoubleleading%%whenusedina
batchfile.

http://ss64.com/nt/syntaxargs.html

1/4

19/10/2016

Parameters/Arguments|WindowsCMD|SS64.com

ifafilenamewithnodriveletter/pathisexpandedtodisplayadriveletter/paththecommandshellwillassume
oftenincorrectlythatthefileresidesinthecurrentdirectory.
Whenwritingbatchscriptsit'sagoodideatostorethevaluesinavariableSET_LogFile=%~dp1,therestof
thescriptcanthenrefertotheeasytoreadvariablename%_LogFile%Thiswillalsomakelifeeasierifyou
laterneedtochangearoundtheorderoftheparameters.

Tokenization
Tokenizationofthecommandlineargumentsisnotalwaysdonebythecmd.exeshell.Mostoften
thetokenizationisdonebythenewlyformedprocesses'runtime,attheOSlevel,Windowspassesa
commandlineuntokenizedasasinglestringtothenewprocess.Youcanreadmoreaboutthelow
leveldetailsofthebatchlanguage/cmdescapingfromtheexcellentanswersbyjebanddbenhamon
thisStackOverflowquestion.

PassingbyReference
Inadditiontopassingnumericorstringvaluesonthecommandline,itisalsopossibletopassa
variablenameandthenusethevariabletotransferdatabetweenscriptsorsubroutines.Passingby
referenceisaslightlymoreadvancedtechniquebutcanbeparticularlyusefulwhenthestring
containscharactersthatareCMDdelimitersorquotes.

LinksrelativetotheBatchScript
Youcangetthepathnameofthebatchscriptitselfwith%0,parameterextensionscanbeappliedto
thisso%~dp0willreturntheDriveandPathtothebatchscripte.g.W:\scripts\and%~f0will
returnthefullpathnameW:\scripts\mybatch.cmd
Youcanrefertootherfilesinthesamefolderasthebatchscriptbyusingthissyntax:
CALL%0\..\SecondBatch.cmd
Thiscanevenbeusedinasubroutine,Echo%0willgivethecalllabelbut,echo"%~nx0"willgive
youthefilenameofthebatchscript.
Whenthe%0variableisexpanded,theresultisenclosedinquotationmarks.

Use%~a1todisplaytheExtendedAributesofale.
FOR's%%~aIrecognizes9NTFSfileattributes.Theexpansionofafileattributeproducesaseries
of9dashes,witheachrecognizedattributereplacingadashwithaletter.Afilewithnorecognized
attributesorwithnonesetwillexpandto9dasheslikethis:
AttributeExpansion
FILE_ATTRIBUTE_DIRECTORYd
FILE_ATTRIBUTE_READONLYr
FILE_ATTRIBUTE_ARCHIVEa
FILE_ATTRIBUTE_HIDDENh
FILE_ATTRIBUTE_SYSTEMs
FILE_ATTRIBUTE_COMPRESSEDc
FILE_ATTRIBUTE_OFFLINEo
FILE_ATTRIBUTE_TEMPORARYt
FILE_ATTRIBUTE_REPARSE_POINTl
FILE_ATTRIBUTE_NORMAL
OtherNTFSattributesnotrecognisedby%%~aIcanbereadusingFSUTILusncommand:
FILE_ATTRIBUTE_ENCRYPTED
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
FILE_ATTRIBUTE_SPARSE_FILE
Example:ExpansionofafilewiththeHiddenandSystemattributes:
hs

Maximumlinelength

http://ss64.com/nt/syntaxargs.html

2/4

19/10/2016

Parameters/Arguments|WindowsCMD|SS64.com

Maximumlinelength
Themaximumlengthofanycommandline(orvariable)withinCMDis8191characters.

Terminology:CommandlineArgumentorParameter?
Inmathematicaltermsaparameteris"anumericalorothermeasurablefactor"sotypicallya
number.
Inprogrammingterminology:
Aparameterrepresentsavaluethattheprocedureexpectsyoutopasswhenyoucallit.
Anargumentrepresentstheactualvaluethatyoupasstoaprocedure.
Inpracticethephrasesargumentandparametertendtobeusedinterchangeably,CMDbatchfiles
donotperformanytypechecking.

FORparameters
TheFORcommandcreatesparametervariableswhichareidentifiedwithaletterratherthana
number(e.g.%%G).
TheParameterExpansionsdescribedabovecanalsobeappliedtothese.
Toavoidconfusionbetweenthetwosetsofletters,avoidusingtheletters(a,d,f,n,p,s,t,x,z)as
FORparametersorjustchooseaFORparameterletterthatisUPPERcase.
Soforexampleinareferencelike%%~fGthe%%GistheFORparameter,andthe~fisthe
ParameterExpansion.
Examples:
Passparametersfromonebatchtoanother:
MyBatch.cmdSMITH100
OraspartofaCALL:
CALLMyBatch.cmdSMITH100
GetthefilesizeofC:\demo\sample.xlsxusingthe%~zparameterextension:
@Echooff
For%%Gin("C:\demo\sample.xlsx")doset_var=%%~zG
EchoFilesizeis%_var%
Passingvaluesfromonepartofascripttoanother:
::UsingCALLtojumptoasubroutine
CALL:s_staffSMITH100
::CallingasubroutinefromaFORcommand
FOR/F%%GIN('DIR/b*.*')DOcall:s_subroutine%%G
Agiftispurewhenitisgivenfromthehearttotherightpersonattherighttimeandattherightplace,andwhenwe
expectnothinginreturn~TheBhagavadGita
Related:
Bugwhenusing~stodisplayshortfile/foldernames
CALLCallonebatchprogramfromanother.
CMDStartanewDOSshell(cmd.exe)
IFTestthatrequiredinputsareinplace(notNULL)
FORConditionallyperformacommandseveraltimes.
SETLOCALControlthevisibilityofenvironmentvariables
SHIFTShiftthepositionofreplaceableparametersinabatchfile.
StackOverflowHowdoestheWindowsCommandInterpreter(CMD.EXE)parsescripts?
Equivalentbashcommand(Linux):dirnameConvertafullpathnametojustapath.
http://ss64.com/nt/syntaxargs.html

3/4

19/10/2016

Parameters/Arguments|WindowsCMD|SS64.com

optionsparser
inC++11
Parsingcommandline
arguments.autoconfigfile,
doc,subcommand
github.com

CopyrightSS64.com19992016
Somerightsreserved

http://ss64.com/nt/syntaxargs.html

4/4

Das könnte Ihnen auch gefallen