Yeah, that's my question. I saw several batch scripts using this ~ character. But what is it for?
And I've got another question:
|
Full Version: %~1 In Batch Files?
Yeah, that's my question. I saw several batch scripts using this ~ character. But what is it for? And I've got another question:
The text is crap, but I wonder why the SET /P doesn't work. Whatever I enter, it always says: ""blabla" is syntatically not processable at this place." (I translated, so it isn't right Anybody help?
I think ~ is for files Long name
in dos help...
SET /P variable=[prompt string] but unless you understand german... i don't.. it looks like there's extra words.. the ? character is a wildcard, meaning 1 character is suppost to be there, but it doesn't matter which... only having one at that location doesn't look right ~ is used in FOR loops (and maybe somewhere else)... %~a (can be any letter, but must be the same both places in loop) the ~ removes quotes more help on FOR loops? go to CMD and type "FOR /?" Example: FOR %x IN (\random\directory\*) DO ECHO %~x shows everything in that directory... btw, in for loops, %fx would be full path, full file name is default, if i remember correctly EDIT: just noticed it was a number... numbers work like this.. Filename Special Words would go around here (%0---------%1-----%2---%3-%4----%5--%6) and can be used like this: IF %1 == "Gary" THEN ECHO "You suck"
Check This out
usefull !
The "~" character is used in truncated directory/filenames. It appears because when you're running a batch file you are running it in an MS-DOS environment. DOS is designed to work with FAT 3.2 file system (if I'm right), which is why directories and files are accessed with the 8+3 character filename format. eg. If you have a directory called "Documents and Settings" on the root of your H.D.D (for arguments sake call it C:) then to access it you would type "cd c:\docume~1". If you wanted to edit a text file called "thisistoolong.txt" then you would type, in a command prompt or DOS itself, "edit c:\thisisto~1.txt". Also, the reason why your getting a syntax error for the set command is in the first line - SET /P %homedir%=Welches Verzeichnis soll gepackt werden? ..."homedir" is the actual variable and "%homedir%" is the reference to the variables data. So, in that line above, what your command interpreter is actually trying to do is prompt a user to set a totally different variable (whatever is stored inside homedir). Just remove the "%" signs before and after homedir in the SET lines and you'll be fine.
I didn't really understand that so I'm not sure our friend BlaStA will. Numbers preceeded with a "%" sign usually reference parameters passed to programs. Eg. If you had a batch file (temp.bat) with this contents inside: @echo off cls echo %1 %2 %3 %4 %5 ...and you run it from your command prompt like this: C:\Documents and Settings\#####\Desktop\> temp.bat Matthew Mark Luke John Then you would get a clear screen then the words "Matthew Mark Luke John" printed out on your screen. %1 matches up with the first parameter passed to temp.bat and %2 with Mark etc.
I figured out what the set-thing was. It wasn't the line itself, but the line AFTER the set-command. With "IF EXISTS" you can only check if a FILE exists and not a directory. @LittleHacker: Thanks, gonna give it a read. @T3cHn0b0y: I don't mean the ~-sign which is used to override long filenames. I mean the one used in batch files (%~1). I don't think it's the same?!
Yes, I do understand german cause I am german //edit: I know about the parameters.
It should work better like this:
Damn I hate the german language.... (yes I CAN read/write/speak it). ps: ~ is also used with the "set" command like this: %PATH:~10,5% would expand the PATH environment variable, and then use only the 5 characters that begin at the 11th (offset 10) character of the expanded result. If the length is not specified, then it defaults to the remainder of the variable value. If either number (offset or length) is negative, then the number used is the length of the environment variable value added to the offset or length specified. %PATH:~-10% would extract the last 10 characters of the PATH variable. %PATH:~0,-2% would extract all but the last 2 characters of the PATH variable.
Sometimes, a ~ (or any other arbitrary character) may appear in a batch file "IF "command to avoid a possible null field in the interpreted command.
For example, to determine if a first parameter was passed to the batch file, I might write "If ~%1 == ~ ..." If there were no first parameter, this would test true; if there were a first parameter, this would test false. In execution, with a first parameter of dog, the test would interpret as "If ~dog == ~ ..." (false) and with no first parameter the test would interpret as "If ~ == ~ ..." (true). In the latter case, if you didn't include the "~" (or any other arbitrary character) the test would interpret as "If == ...", which would be invalid syntax. I've seen people use "a" and other letters for this purpose, but I prefer the "~" for readability: it doesn't blend in with my actual data argument.
In fact you can use any character for that.... Even more then one: if .%1==. echo Empty input if ~%1==~ echo Empty input if "%1"=="" echo Empty input if blaat%1==blaat echo Empty input This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
|
||||||||||||||