1.

Solve : Set Var=??

Answer»

Thank you thank you thank you.

Yes that will work perfectly. Thank you for all your help. Thought of something. After running the BAT file I want to rename or delete the bat file so it can't be run again. I know I could set a variable using the %CD%, but I noticed that is the bat file is run from the command as follows ...

Code: [SELECT]c:
cd program files\bat
"folder\edit.bat"

If I set local=%CD% in the bat file that is run, I will get 'C:\Program Files\bat' as my local variable. Is there a way around this to force it to take the actual location of the bat file so I can remove it? QUOTE from: nothlit on May 15, 2008, 09:55:11 AM

Is there a way around this to force it to take the actual location of the bat file so I can remove it?

the filename + extension of a batch file is contained in the variable %0 (percent zero). The full name would be

%~dpnx0

so, adding quotes in case the path and/or name has spaces,

del "%~dpnx0"

would make a batch delete itself, but you would get an error message because you have deleted the running batch from within itself.


Ok, so %0 gives me the whole path, but what is the ~dpnx REFER to? or is that like sending commands to the delete command? Ok, I think I found something. I did Call /? in a command prompt and find %~dp1 - expands %1 to a drive letter and path only, but still confused on the nxThey are standard variable modifiers.

If you typed for /? at the command prompt, didn't you see this?

%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a FULLY qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file

they can be combined to get compound results:

%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only

so if %0 is a batch file's own name and extension, then

%~d0 is its drive letter with a colon
%~p0 is the full path to its folder
%~n0 is its bare name without extension
%~x0 is its extension with a dot before in front of it (usually .bat or .CMD)

Put them together adding quotes in case the path has spaces and you get:

"%~dpnx0"


Thank you for the explanation.


Discussion

No Comment Found