search path with For's %~$PATH:I?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
taripo
Posts: 228
Joined: 01 Aug 2011 13:48

search path with For's %~$PATH:I?

#1 Post by taripo » 16 Jun 2015 13:19

I created a file a.bat and I try to find it with the $PATH technique mentioned in for /? but I cannot see how

Where am I going wrong in my usage of %~$PATH?

Code: Select all

C:\Windows>dir a.bat
 Volume in drive C has no label.
 Volume Serial Number is B411-D580

 Directory of C:\Windows

16/06/2015  08:15 PM                 0 a.bat
               1 File(s)              0 bytes
               0 Dir(s)   6,522,785,792 bytes free

C:\Windows>for /? | find "$PATH"
    %~$PATH:I   - searches the directories listed in the PATH
    %~dp$PATH:I - searches the directories listed in the PATH

C:\Windows>
C:\Windows>echo %~$PATH:a.bat
%~$PATH:a.bat

C:\Windows>

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: search path with For's %~$PATH:I?

#2 Post by aGerman » 16 Jun 2015 13:56

This modifier expands a filename to its full path only if it was found in one of the directories saved in the %PATH% environment variable.

The following example extracts the command names from the HELP command and separates them into "internal commands" of cmd.exe and "external commands" that are programs found in the PATH environment.

Code: Select all

@echo off &setlocal EnableDelayedExpansion
set /a int=0, ext=0
for /f %%i in ('help^|findstr /rbc:"[A-Z][ABCDEFGHIJKLMNOPQRSTUVWXYZ]"') do (
  for /f "tokens=1,2 delims=?" %%j in ("%%i.exe?%%i.com") do (
    if "%%~$PATH:j%%~$PATH:k"=="" (
      set /a int+=1
      set "line=%%i                                                  "
      echo !line:~,50! - internal
    ) else (
      set /a ext+=1
      echo %%i "%%~$PATH:j%%~$PATH:k"
    )
  )
)

echo(
echo internal %int%
echo external %ext%
pause>nul

Regards
aGerman

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: search path with For's %~$PATH:I?

#3 Post by taripo » 16 Jun 2015 14:37

You write "This modifier expands a filename to its full path only if it was found in one of the directories saved in the %PATH% environment variable."

It is in the PATH

Code: Select all

C:\Windows>where a.bat
C:\Windows\a.bat

C:\Windows>for /? | find "$PATH"
    %~$PATH:I   - searches the directories listed in the PATH
    %~dp$PATH:I - searches the directories listed in the PATH

C:\Windows>echo %~$PATH:a.bat
%~$PATH:a.bat

C:\Windows>

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: search path with For's %~$PATH:I?

#4 Post by aGerman » 16 Jun 2015 15:20

It is in the PATH

What is in the PATH? I assume you are talking about your a.bat.

Code: Select all

C:\>for %I in (a.bat) do @echo %~$PATH:I

Of course the ~$PATH: modifier does only work with FOR variables. That means it can only be used in the scope of a FOR loop.

Regards
aGerman

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: search path with For's %~$PATH:I?

#5 Post by taripo » 16 Jun 2015 17:55

ah, thanks

Post Reply