| 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 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 |
|