Government Security
Network Security Resources

Jump to content

%~1 In Batch Files?

- - - - -
  • Please log in to reply
8 replies to this topic

#1 Guest_BlaStA_*

Guest_BlaStA_*
  • Guests

Posted 13 April 2004 - 10:53 AM

Yeah, that's my question. I saw several batch scripts using this ~ character. But what is it for?

And I've got another question:

SET /P %homedir%=Welches Verzeichnis soll gepackt werden?
IF NOT EXISTS %targetdir% GOTO NOTEXISTS
echo Quellverzeichnis auf "%homedir%" gesetzt.
SET /P targetdir=Wohin sollen die ZIP-Dateien? 
IF NOT EXISTS %targetdir% GOTO NOTEXISTS
echo Zielverzeichnis auf "%homedir%" gesetzt.
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 :D ) But what it generally says is, that this entered string is not placed correct there.

Anybody help?

#2 Guest_LittleHacker_*

Guest_LittleHacker_*
  • Guests

Posted 13 April 2004 - 11:29 AM

I think ~ is for files Long name

#3 Logan

Logan

    Specialist

  • Sergeant Major
  • 1,596 posts

Posted 13 April 2004 - 11:34 AM

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"


#4 Guest_LittleHacker_*

Guest_LittleHacker_*
  • Guests

Posted 13 April 2004 - 11:55 AM

Check This out
usefull !

#5 Guest_T3cHn0b0y_*

Guest_T3cHn0b0y_*
  • Guests

Posted 13 April 2004 - 11:58 AM

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.

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"


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.

#6 Guest_BlaStA_*

Guest_BlaStA_*
  • Guests

Posted 13 April 2004 - 12:15 PM

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?!

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

Yes, I do understand german cause I am german ;) But I don't understand what you meant with the wildcard, the ? character and so on. What does that have to do with my problem? :blink:

//edit: I know about the parameters. :)

#7 dmg

dmg

    Corporal

  • Members
  • 156 posts

Posted 13 April 2004 - 01:37 PM

Yeah, that's my question. I saw several batch scripts using this ~ character. But what is it for?

And I've got another question:

SET /P %homedir%=Welches Verzeichnis soll gepackt werden?
IF NOT EXISTS %targetdir% GOTO NOTEXISTS
echo Quellverzeichnis auf "%homedir%" gesetzt.
SET /P targetdir=Wohin sollen die ZIP-Dateien? 
IF NOT EXISTS %targetdir% GOTO NOTEXISTS
echo Zielverzeichnis auf "%homedir%" gesetzt.
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 :D ) But what it generally says is, that this entered string is not placed correct there.

Anybody help?

It should work better like this:

SET /P %homedir%="Welches Verzeichnis soll gepackt werden? "
IF NOT EXIST "%homedir%" GOTO NOTEXISTS
echo Quellverzeichnis auf "%homedir%" gesetzt.
SET /P targetdir="Wohin sollen die ZIP-Dateien? "
IF NOT EXIST "%targetdir%" GOTO NOTEXISTS
echo Zielverzeichnis auf "%targetdir%" gesetzt.

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.

#8 DanM

DanM

    Private

  • Members
  • 1 posts

Posted 13 April 2004 - 08:25 PM

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.

#9 dmg

dmg

    Corporal

  • Members
  • 156 posts

Posted 16 April 2004 - 02:48 AM

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