Batch get path via FOR

We have a batch file where we have a variable %FileName which is set eg. to D:\dir\test.txt. We want to get the path without the filename eg. D:\dir\ out of the variable %FileName.

How could we do that using FOR and %~pI in our batch script???

Many thanks in advance!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

deroodeSystems AdministratorCommented:
The following seems to work: (appending the Driveletter and Path)

for %i in (%filename) do set filepath=%~di%~pi

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You could also have done it like this:

    for %a in (%filename) do set filepath=%~dpa

If you're doing this in a batch file then you need this:

    for %%a in (%filename) do set filepath=%%~dpa
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
It's good style to write the FOR var in uppercase, to separate it from the modifies (~d drive, ~p path, ~n name, ~x extension, ...) (batch file syntax):

for %%F in (%filename%) do set filepath=%%~dpF

Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Personally, I feel that's a matter of opinion although I have seen other people say the same. I can see a potential concern in the following though:

for %%p in (%filename%) do set filepath=%%~dp           <-- intending to return just the drive letter


for %%p in (%filename%) do set filepath=%%~dpp         <-- intending to return both drive and path
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Yes, those cases are the reason why I said this. And "good style" is "matter of opinion" naturally.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
you chose the sub-optimal solution. It's unusual to do this in to var expansions, the file in %i will be checked for each occurance of %~...i, so it's much better to use a all-in-one expansion like %~dpi. It might not count in this single file case, but in others there is a significant overhead.
Next, it is not working in a batch file, as you have to double the percents, as t0t0 told you.
I am quite annoyed by your decision to choose deroode's code as your solution for the following reasons:

1) You asked for a batch file. deroode gave you a DOS command. deroode's code WILL NOT WORK inside a batch file as he uses '%i' instead of '%%i'.

2) deroode uses 'filepath=%~di%~pi'. The correct syntax is actually 'filepath=%%~dpa' (combining the 'd' and the 'p' in a single expression - see Qlemo's notes on this).

3) Even though you chose deroode's code (which DOES NOT WORK in a batch file), AND even though I provided you with the CORRECT solution, not only did you not accept my code as your solution but you didn't even award me ASSISTED status.

This is unfair. You totally ignored my solution in the lower half of my comment (ID: 24491678). If this was a genuine oversight then I urge yiou to re-open this question and re-distribute the points FAIRLY!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.