Saved Bookmarks
| 1. |
Solve : START %~d0\ IF THIS FILE IS BEING RAN FROM THE ROOT OF ANY DRIVE? |
|
Answer» START %~d0\ IF THIS FILE IS BEING RAN FROM THE ROOT OF ANY DRIVE @echo off Probably a better way...Quote Probably a better way... Probably... code saved as F:\fpath.cmd: Quote
Quote
Quote from: contrex on August 07, 2007, 02:10:27 PM QuoteProbably a better way... How did my code do: " "fpath"=="F:\fpath.cmd" " Quote from: DeltaSlaya on August 07, 2007, 02:17:13 PM How did my code do: " "fpath"=="F:\fpath.cmd" " Like this... Variable modifiers (V=variable) Edited from FOR /? %~fV - expands %V to a FULLY qualified path name %~dV - expands %V to a drive letter only (and colon) EG D: %~pV - expands %V to a path only %~nV - expands %V to a file name only %~xV - expands %V to a file extension only %~sV - expanded path contains short names only %~aV - expands %V to file attributes of file %~tV - expands %V to date/time of file %~zV - expands %V to size of file modifiers can be combined to get compound results: %~dpV - expands %V to a drive letter and path only %~nxV - expands %V to a file name and extension only %~fsV - expands %V to a full path name with short names only Your code: if "%~0"=="%~d0\%~nx0" (goto yes) else goto no ^ ^ ^ | | | filename drive filename and extension | | | fpath F: \ fpath.cmd How come when I run the code: Quote if "%~0"=="%~d0\%~nx0" (goto yes) else goto no From the C:\ directory the output is: Quote C:\>if "C:\Test.bat" == "C:\Test.bat" (goto yes ) else goto no how odd. Will investigate. Whether I call it fpath.cmd or test.bat It works the same. My OS is XP Professional SP2, is yours different? I am running under cmd.exe Quote
This is what it does under XP command.com Quote
I know why, %~0 just shows how the batch file was initially started. If you open it in explorer it gives the file path, if you open it in a prompt it gives the line that started it.So the batch file would have to be written so it works regardless of how it was started. Well mine doesn't work if it is started from the command prompt by just its name, ie. fpath. Because then %~0 would expand to fpath. I suppose in a way it's like %cmdcmdline%, the line that initiated command prompt. ...system32\cmd.exe Though if it is started by typing "cmd" %cmdcmdline returns "cmd": Quote Microsoft Windows [Version 6.0.6000] |
|