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

HOW CAN I MAKE THIS POSSIBLE - WHATS THE CODECould you PLEASE make sense?I need for this batch file to check if its being ran from the root of any drive Ex: C:\batch.bat or D:\batch.bat
and if it is then this code START %~d0\ will open that root drive of where its being ran from in a new explorer window..Quote

@echo off

if "%~0"=="%~d0\%~nx0" (goto yes) ELSE goto no

:yes
START %~d0\
pause

:no
echo Not in root.
pause

Probably a better way...Quote
Probably a better way...

Probably...

code saved as F:\fpath.cmd:

Quote

@echo off
echo Deltaslaya's code:
echo if "%~0"=="%~d0\%~nx0" (goto yes) else goto no
echo.
echo my code:
set rootfolder=%~d0\
set thisfolder=%~dp0
echo if "%thisfolder%"=="%rootfolder%" then whatever


Quote

F:\>fpath
Deltaslaya's code:
if "fpath"=="F:\fpath.cmd" (goto yes) else goto no

my code:
if "F:\"=="F:\" then whatever




Quote from: contrex on August 07, 2007, 02:10:27 PM
Quote
Probably a better way...

Probably...

code saved as F:\fpath.cmd:

Quote

@echo off
echo Deltaslaya's code:
echo if "%~0"=="%~d0\%~nx0" (goto yes) else goto no
echo.
echo my code:
set rootfolder=%~d0\
set thisfolder=%~dp0
echo if "%thisfolder%"=="%rootfolder%" then whatever


Quote

F:\>fpath
Deltaslaya's code:
if "fpath"=="F:\fpath.cmd" (goto yes) else goto no

my code:
if "F:\"=="F:\" then whatever






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

:yes
START %~d0\
pause

:no
echo Not in root.
pause

From the C:\ directory the output is:

Quote
C:\>if "C:\Test.bat" == "C:\Test.bat" (goto yes ) else goto no

C:\>START C:\

C:\>pause
Press any key to continue . . .

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

C:\>fpath
Deltaslaya's code:
if "fpath"=="C:\fpath.cmd" (goto yes) else goto no
my code:
if "C:\"=="C:\" then whatever

C:\>test.bat
Deltaslaya's code:
if "test.bat"=="C:\test.bat" (goto yes) else goto no
my code:
if "C:\"=="C:\" then whatever


This is what it does under XP command.com

Quote

C:\>command
Microsoft(R) Windows DOS
(C)Copyright Microsoft Corp 1990-2001.

C:\>fpath
Deltaslaya's code:
if "FPATH"=="C:\fpath.cmd" (goto yes) else goto no
my code:
if "C:\"=="C:\" then whatever




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]
Copyright (c) 2006 Microsoft Corporation. All rights reserved.

C:\Users\DeltaSlaya>echo %cmdcmdline%
"C:\Windows\system32\cmd.exe"

C:\Users\DeltaSlaya>cmd
Microsoft Windows [Version 6.0.6000]
Copyright (c) 2006 Microsoft Corporation. All rights reserved.

C:\Users\DeltaSlaya>echo %cmdcmdline%
cmd

C:\Users\DeltaSlaya>


Discussion

No Comment Found