Reputation: 95
How can get the name of the folder in a for loop. I have the following directory structure
c:\Main\**FolderName1**\FolderName3\somefile.txt
c:\Main\**FolderName2**\FolderName4\somefile1.txt
And i want to get the name of the FolderName1 and FolderName2 inside a for loop.
This is how i am doing right now:
set "errfolderpath=C:\Main\"
FOR /D /R %errfolderpath% %%K in (.\*) DO (
SET folderName=%%~nK
echo foldername=!folderName!
)
when i run the above program it is printing the name of all the subfolders whereas i just want the name of FolderName1 and FolderName2 and not to loop rest of the folders.
I hope i was able to make it clear. Thanks.
Upvotes: 2
Views: 7577
Reputation: 77737
The /R folder
parameter specifies both the starting folder and the recursive search, while you only need the former and not the latter.
So, just use your root folder with the file mask, like this:
set "errfolderpath=C:\Main\"
FOR /D %%K in ("%errfolderpath%*") DO (
SET folderName=%%~nK
echo foldername=!folderName!
)
Upvotes: 1