Generating list of folders by batch file

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Generating list of folders by batch file

Post by MigrationUser »

20 May 2008 09:25
smirno

Hi,

I'm trying to write simple batch file to generate a list of last level folders in windows xp.
E.g.
When I'm running this script in folder C:\scripts and in this folder I have folders first, second and third
and in second folder I have folder "fourth space" it should make output like:
first
second
fourth space
third

When I use: dir /b /a:d /s > list.txt

It will list all path's I need but I need remove everything before last folder name.

I try simple think like: %variable:StrToFind=NewStr% but this doesn't work in for loop as I was expected:

Code: Select all

@ECHO off
cls
dir /b /a:d /s > list.txt
FOR /F "delims=      " %%i in (list.txt) do (
SET "_ccc=%%%i:%~dp0=%"
ECHO %_CCC% >> folders.txt)
Or I was thinking of use something like:

Code: Select all

FOR /F "tokens=* delims=\" %%i in (list.txt) do ( ECHO %%i >> folders.txt)
but I don't know how to tell for loop to use only the last token.
Is the only way to use e.g. IF ELSE command to get the last token?
Have somebody any idea?

----------------------------

#2 20 May 2008 12:37
bluesxman


If I understand you correctly, you're saying that in this fictitious "dir /b/s/a:d" listing:

C:\scripts\dir1
C:\scripts\dir1\dir1.1
C:\scripts\dir2
C:\scripts\dir3
C:\scripts\dir3\dir3.1
C:\scripts\dir3\dir3.1\dir3.1.1

You want to display:

dir1
dir1.1
dir2
dir3
dir3.1
dir3.1.1

If so, this should do the trick:

Code: Select all

@echo off

for /f "usebackq tokens=*" %%a in (`dir /b/s/a:d c:\scripts`) do (
    echo:%%~nxa
)

pause
cmd | *sh | ruby | chef

----------------------------

#3 20 May 2008 14:33
smirno


Thank you.
I didn't realized that %~nxI works also for folders.

But command dir don't produced what I want. Problem is order in witch the folder's names are.

When I use dir I get this:
dir1
dir2
dir3
dir1.1
dir3.1
dir3.1.1
But I need this:
dir1
dir1.1
dir2
dir3
dir3.1
dir3.1.1

I used FOR /R and in list2 i have what I wanted:

Code: Select all

FOR /R %%i in (.) do (echo %%~fi >> list1.txt)
FOR /F "tokens=*" %%i in (list1.txt) do (echo %%~nxi >> list2.txt)
Is there a way I could make it without list1.txt in one step?

Last edited by smirno (20 May 2008 14:39)

----------------------------

#4 20 May 2008 16:26
bluesxman


"for /r" was showing me some strange behaviour, so I despensed with that ... this should do it:

Code: Select all

@echo off
(for /f "tokens=* usebackq" %%i in (`dir /b/s/a:d`) do @(echo:%%~nxi )) | sort > list1.txt
Last edited by bluesxman (20 May 2008 16:39)

cmd | *sh | ruby | chef

----------------------------

#5 21 May 2008 09:56
smirno


Hi.
This make it even worse, it sort output alphabetically.

I think the only way is to use FOR /R to get a list of folders in order as they are saved on the drive.

Thank you for you help. You helped me a lot even it was only two characters what I needed but without your help, I wouldn't realize I could use it in this way.

This is version with FOR \R that works very fine for me.

Code: Select all

@Echo Off
CLS
IF EXIST folderslist.txt (goto :exit1)
FOR /R %%i in (.) do (echo %%~nxi>>folderslist.txt)
ECHO List of all folders and subfolders in folder:%~dp0%
ECHO.
ECHO was created and was saved in file: folderslist.txt in the same folder.
ECHO For end and closing of the window press any key
PAUSE
GOTO :exit2
:exit1
ECHO Error - folder list not created
ECHO File folderslist.txt already exist. Delete it or move it and try again.
Pause
:exit2
Last edited by smirno (21 May 2008 11:09)
Post Reply