The FOR command in Windows has some options that it did not have in DOS. Specifically, it supports returning filename segments as follows.
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
%~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
When I try to use the following command, it does not work. Instead of printing \Program Files, it prints \.
for %i in ("%programfiles%") do echo %~pi
It seems that only the p argument is incorrect because the other arguments return the expected results.
> for %i in ("%programfiles%") do echo %i
C:\Program Files
> for %i in ("%programfiles%") do echo %~i
C:\Program Files
> for %i in ("%programfiles%") do echo %~fi
C:\Program Files
> for %i in ("%programfiles%") do echo %~is
C:\PROGRA~1
> for %i in ("%programfiles%") do echo %~id
C:
Does anyone know what the problem could be?
This is not a problem, this is the correct and expected result.
~pextracts the path part of a given filespec.In this case
c:is the drive, returned by%~di;\is the path, returned by%~pi;Program Filesis the name, returned by%~ni;C:\Program Filesis the complete expanded filespec, returned by%~fi, or by%~dpnxi.