0

EDIT: I have just changed the if to an if else

IF "%CD:~1%"==":\" ( set FIR=%CD:~0,2% ) ELSE ( set FIR=%CD% )

Trying to use a wildcard to detect if a batch file is on the root directory (As root causes there to be a backslash at the end of the DIR)

I need to determine with a simple if statement whether it is *:\ or not. I have tried using wildcards *:\ and ?:\ but to no avail. If I use the drive letter, example: G:\ it works.

if %CD%==G:\ echo SOMETHING WRONG HERE

works perfectly well, until the drive letter changes. Where as

if %CD%==?:\ echo SOMETHING WRONG HERE

or

if "%CD%"=="?:\" echo SOMETHING WRONG HERE

Doesn't work.

EDIT: After reading up it turns out if and wildcards are not compatible!

Built on Sin
  • 351
  • 2
  • 6
  • 19
  • Wildcards Wildcards are not supported by IF, so %COMPUTERNAME%==SS6* will not match SS64 A workaround is to retrieve the substring and compare just those characters: SET _prefix=%COMPUTERNAME:~0,3% IF %_prefix%==SS6 GOTO they_matched – Built on Sin Jul 03 '13 at 01:38

2 Answers2

2

try this:

if "%CD:~-2%"==":\" (echo root) else echo NOT root

see docu help set.

Endoro
  • 37,015
  • 8
  • 50
  • 63
  • this might be more accurate `if "%cd:~-2%"==":\" echo is root` – foxidrive Jul 03 '13 at 07:54
  • @foxidrive for what case are you thinking? – Endoro Jul 03 '13 at 09:38
  • 1
    If the path used is c:\abc\ then your code will also return that it is the root. With my suggestion it will return that it is not the root. It's perhaps a moot point as %cd% will return d:\abc and your code will work fine in all cases. – foxidrive Jul 03 '13 at 11:38
  • @foxidrive yes, if you actively assign a value to `cd`. But nevertheless, it might be safer. – Endoro Jul 03 '13 at 11:43
  • @Foxdrive Actually my script later on writes the directories /abc/Icon itself. So when run from root it writes C:\\abc\Icon else %CD% does work fine. – Built on Sin Jul 03 '13 at 16:04
0

Wildcards are not usable within IF statements in command.

Built on Sin
  • 351
  • 2
  • 6
  • 19