1

I am creating a program that allows the user to 'search' the computer for a file, and if the file exists, it will create a shortcut to start that file in a new directory. Any and all extensions can be used. The problem is, in order to start certain extensions (like .jar), I need the path of the file.

None of the examples I've seen so far have been successful in helping me with this, as they usually require an already given path to split.

Ex.

(dir %Filename%.%extension% /b /s /a:-D)

How would I be able to store only the path given by this into a variable?

Any help is appreciated.

Caspr
  • 57
  • 6

1 Answers1

2

If you're using a for loop to process the output of a dir command, batch will know the path and the filename automatically. From there, it's simply a matter of using the well-known %~dp syntax to get the path.

for /f "delims=" %%A in ('dir /b /s /a:-d') do echo %~dpA

where d gets the drive and p gets the path.

Other options (from for /?):

%~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
SomethingDark
  • 13,229
  • 5
  • 50
  • 55