Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi all,
I have a string containing a complete path of a file in my BATCH file
. I would like to extract only the path leaving the file name and previous directory.
For example: k:\My test\bin\execute\test.bat.
I just need to remove execute and test.bat and get the remaining of the string in my batch file. Please let me know the DOS command which i should use to achieve the same.
Thanks in advance
Sujie

Much depends on the version of DOS.
=====================================
If at first you don't succeed, you're about average.M2

If your environment is a NT kernel based operating system, i.e. Windows NT/2K/XP, the batch below satisfies your need
@Echo Off
Set Var=K:\My test\bin\execute\test.batFor %%i in ("%Var%") Do Set CF=%%~dpi
Set CF=%CF:~0,-1%
:LOOP
If "%CF:~-1,1%"=="\" GoTo :DONE
Set CF=%CF:~0,-1%
GoTo :LOOP:DONE
Set CF=%CF:~0,-1%
Echo %CF%Where Var holds the path to be parsed and CF the result.
If however you are running a DOS based OS as Windows 9X/ME, I leave M2 the challenge.

Hey thanks for that..it does work but
For %%i in ("%Var%") Do Set CF=%%~pi
Set CF=%CF:~0,-1%After this line the drive letter is removed and CF is assigned to this?
\My test\bin\execute\test.batAny idea on this?
Sujie

Yes, I edited the script I posted as I realized the minor bug, replace
For %%i in ("%Var%") Do Set CF=%%~pi
with
For %%i in ("%Var%") Do Set CF=%%~dpi
as you can see in the edited post #2 and you are done.

Thanks a lot..It was of great help..
and it worked fine..I really appreciate your prompt response.:-)
Sujie

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |