1.

Solve : Win/Batch: How to get the folder name from path?

Answer»

Hi,

Can anyone teach me how to get the string of the folder name from a directory path by DOS batch command?

For example: if the directory path is "d:\app program\test folder", how to extract the string "test folder"?

Thanks
thomasThis will provide the name of the current working directory.

Code: [Select]for /f "delims=" %%A in ('cd') do (
set foldername=%%~nxA
)

echo. Current Folder Name: %foldername%
Hello
C:\app program\test folder>cd \

C:\>dir /s foldertest.txt
Volume in drive C has no label.
Volume Serial Number is F4A3-D6B3

Directory of C:\app program\test folder

10/01/2009 03:59 PM 9 foldertest.txt
1 File(s) 9 bytes

Total Files Listed:
1 File(s) 9 bytes
0 Dir(s) 306,328,834,048 bytes free

C:\>
C:\>type testextract.bat

REM To find the path to test folder,we place a file foldertest.txt in the folder
REM We then searched for that file to get the path string and extract
REM test folder ( the folder name we were after
REM We can USE any drive for drive D: We used C: drive
Code: [Select]@echo off
cd C:\app program\test folder\
echo CD = %CD%
cd \
echo CD = %CD%

dir /s foldertest.txt | findstr "Directory" > tmpFile.txt

echo type tmpFile.txt
type tmpFile.txt

set /P foldername= < tmpFile.txt

set foldername=%foldername:Directory of C:\app program\test folder=test folder%


echo foldername = %foldername%
Output:

C:\> testextract.bat

CD = C:\app program\test folder

CD = C:\

type tmpFile.txt

Directory of C:\app program\test folder

foldername = test folder
C:\>

REM
This line of code:

set foldername=%foldername:Directory of C:\app program\test folder=test folder%

was with the help of the following site for string manipulation:

http://www.dostips.com/DtTipsStringManipulation.php

but I can no longer reach the site.

We replaced one substring of a string with another string. In this case the substring was the complete string. The new string was the folder name.

A more generic batch file might use command arguments and the folder name would not be hard coded in the batch code.

Good luck Actually, my situation is a bit complicated.

I have to get the directory path (say "C:\app program\test folder") from user input and then extract the folder name (say "test folder") for further manipulation.

I think I have to store the path by a variable and then extract the sting by a for
loop.

But I don't know code for the process.

Here is my partial code:

@echo off
echo Please input the path of program files
set /p foldername=
rem then I need the code to manipulate the require field
pause
exit


Quote from: lwkt on October 01, 2009, 11:24:57 PM

Actually, my situation is a bit complicated.

I have to get the directory path (say "C:\app program\test folder") from user input and then extract the folder name (say "test folder") for further manipulation.

I think I have to store the path by a variable and then extract the sting by a for
loop.

But I don't know code for the process.

Here is my partial code:

@echo off
echo Please input the path of program files
set /p foldername=
rem then I need the code to manipulate the require field
pause
exit


If you are going to ask the user for the folder name, then the computer need not find it.

After the folder name is assigned to a variable, how will the variable be used?

If you have the folder name, the computer can find the path to the folder:

dir /s foldername from the root directory will find the path.

Most users cannot enter a complete path correctly. And your goal is to assign a foldername to a variable. The user provides the folder name.

I don't really understand your goal or problem.
Actually I am going to perform backup for the content of folder from different users' request.

User should input the full path of source folder (say "C:\app program\test folder") for backup. I am going to use XCOPY command to perform the backup task. Then, the newly created folder after backup will be appended by a TIME stamp (say ""C:\app program\test folder_2009-10-02").

Therefore I need to extract the folder name for manipulation.

Here is my code: -
=========================================================
echo Please input the name of folder that you want to backup and press "Enter" key
set/p src=
echo.
REM create folder name(say "test folder_2009-10-02" and assigned to variable tgt)
xcopy /a /c /d /e /k /h /y "%src%"\*.* "%tgt%"*.*
echo.
pause
exit
=========================================================

I think ASSIGNING the path string to a variable is feasible step that I can use
FOR loop to extract the required field for manipulation. If so, I hope someone can tell me the WAY to extract the required string.

BTW, any other suggestions are welcome.






Quote from: lwkt on October 02, 2009, 07:13:25 AM
I think assigning the path string to a variable is feasible step that I can use
FOR loop to extract the required field for manipulation. If so, I hope someone can tell me the way to extract the required string.

if %%F is path, %%~nF is lowest folder.

Code: [Select]set mypath="C:\Program Files\X Ray Vision\Bin"
for /f "delims==" %%F in (%mypath%) do set foldername=%%~nF
echo %foldername%
Code: [Select]BinThanks for Salmon's code. it solve my problem.

But may I know the reason to use the option "delims==" in
the FOR command?

Quote from: lwkt on October 03, 2009, 08:55:19 PM
Thanks for Salmon's code. it solve my problem.

But may I know the reason to use the option "delims==" in
the FOR command?



In case path has any spaces, set delims to be beginnning and end of line. Alternative is "delims=".

Code: [Select]@echo off
set mypath="C:\Program Files\X Ray Vision\Bin"

echo with "delims=="

for /f "delims==" %%F in (%mypath%) do (
echo Drive %%~dF
echo Path %%~pF
echo Folder %%~nF
)

echo.
echo without "delims=="

for /f %%F in (%mypath%) do (
echo Drive %%~dF
echo Path %%~pF
echo Folder %%~nF
)


Code: [Select]with "delims=="
Drive C:
Path \Program Files\X Ray Vision\
Folder Bin

without "delims=="
Drive C:
Path \
Folder Program


Got it !! thanks.


Discussion

No Comment Found