1.

Solve : How to extract a string from some text using a batch file.?

Answer»

I have some text in the form of drive:\folder1\folder2.
I would like to extract the last folder name (folder2 in this case) inside a batch file.
Could someone please help?
Thanks
first you need to set the string into a variable, im just going to assume you already know how , then you need to expand the variable using :~ heres an example:

set a=C:\folder\other
echo %a:~-5,5%


would echo other, open cmd.exe and type set /?where is this text stored? in a variable? or in a text file?The drive:\folder1\folder2 comes from:
set pname=%CD%

from pname I would like to extract the last folder name into another variable 'fname'.
ThanksI have an idea. Unfortunately, it doesn't work yet. I need some help with it.
The following is the code:

echo off
set curdir=%cd%
set n=0
:loop
set /a n=%n%+1
set inter=%curdir:~-%n%%
if not %inter:0,1%==\ GOTO loop
set /a n=%n%-1
set inter=%curdir:~-%n%%
echo %inter%
pause

I think that =%curdir:~-%n%% is wrong but don't know how to insert a variable inside the % signs.

Any ideas about how to fix this?
Thanks Quote from: Frank on August 19, 2008, 11:02:00 PM

I think that =%curdir:~-%n%% is wrong but don't know how to insert a variable inside the % signs.

You can't.

I won't be home for 8 hours to TEST this, but if you are actually logged in to %cd% as the current directory I think you could do a full DIR and using FOR with Find and appropriate tokens you could extract the folder name from the line which includes "Directory Of".

Yes I am logged into %cd%.
However, I don't understand "tokens" in a batch file and haven't used FOR before.
I'll wait for you. No problem.
Thanks
I checked, you don't need tokens. You need to parse the output of the cd command. Used without PARAMETERS, it echoes the corrent folder's drive letter and PATH to the console. The %%~nx FOR variable modifier extracts just the final part, the folder name.

Code: [Select]for /f "delims=" %%A in ('cd') do set foldername=%%~nxA
Some more examples of extracting information about the current folder:

1. folder-info.bat

Code: [Select]echo off
for /f "delims=" %%A in ('cd') do (
     set fullpath=%%A
set driveletter=%%~dA
set folderpath=%%~pA
set foldername=%%~nxA
set folderdate=%%~tA
)
echo.
echo -----------------------------
echo information about this folder
echo -----------------------------
echo.
echo Drive and path : %fullpath%
echo Drive letter   : %driveletter%
echo Folder path    : %folderpath%
echo Folder name    : %foldername%
echo Folder date    : %folderdate%
echo.
2. An example of output

Code: [Select]
S:\Test\Batch\curdir>folder-info.bat

-----------------------------
information about this folder
-----------------------------

Drive and path : S:\Test\Batch\curdir
Drive letter   : S:
Folder path    : \Test\Batch\
Folder name    : curdir
Folder date    : 20/08/2008 18:44



Thanks,
That's simple and brilliant.
Great solution to my problem


Discussion

No Comment Found