How to Create a File List with some modification using Simple Batch File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 198
Joined: 31 Jul 2017 09:57

How to Create a File List with some modification using Simple Batch File

#1 Post by goodywp » 17 Jul 2018 08:17

Hi all,

I search up on internet to create a file list all the files and folders in a plain txt file as below code

Code: Select all

dir /b > fileslist.txt
I used the below code to test my case and it did perfectly as below:

Code: Select all

@echo off
cd C:\auto_pkg_build\Workspace\Build_pkg\T592-08878-0101_Link2500_TCPX_NARInfra_GEN_PROD\T592-08878-0101

if exist bottom_contents.txt (del bottom_contents.txt)

dir /b > bottom_contents.txt

if exist contents.lst (del contents.lst)

rename bottom_contents.txt contents.lst
the output is showing as below:
  • bottom_contents.txt
    Contents.lst
    Customer Download
    Documents
    Factory Download
The file contents.lst which I really want is as below format
  • Contents.lst -> Directory contents (plain text)
    Customer Download\ -> subdirectories
    Documents\ -> subdirectories
    Factory Download\ -> subdirectories
So the difference between these two is that
1) I need to remove the first line, bottom_contents.txt
2) I need to add after contents.lst -> Directory contents (plain text)
3) I need to add after subfolders -> subdirectories
4) if text file I also need to add after the file -> text file

One more big issue is that the above only list the current folder, I also need to list all the subfolders in which generate one of each contents.lst for each subfolders level....
The tool we have can generate contents.lst file but not command supported...
Any thoughts or suggestion?
Thanks

aGerman
Expert
Posts: 3969
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to Create a File List with some modification using Simple Batch File

#2 Post by aGerman » 17 Jul 2018 10:29

What is that funny bottom_contents.txt for?

Recursion should work.

Code: Select all

@echo off &setlocal
call :listdir "C:\auto_pkg_build\Workspace\Build_pkg\T592-08878-0101_Link2500_TCPX_NARInfra_GEN_PROD\T592-08878-0101"
goto :eof

:listdir
pushd "%~1"
2>nul del "bottom_contents.txt"
>"Contents.lst" type nul
for /f "delims=" %%i in ('dir /b') do (
  if "%%~ai" geq "d" (
    >>"Contents.lst" echo %%i\ -^> subdirectories
  ) else if /i "%%i"=="Contents.lst" (
    >>"Contents.lst" echo %%i -^> Directory contents (plain text^)
  ) else if /i "%%~xi"==".txt" (
    >>"Contents.lst" echo %%i -^> text file
  ) else (
    >>"Contents.lst" echo %%i
  )
)
for /f "delims=" %%j in ('dir /ad /b') do call :listdir "%%j"
popd
goto :eof
Steffen

goodywp
Posts: 198
Joined: 31 Jul 2017 09:57

Re: How to Create a File List with some modification using Simple Batch File

#3 Post by goodywp » 17 Jul 2018 12:34

aGerman wrote:
17 Jul 2018 10:29
What is that funny bottom_contents.txt for?

Recursion should work.

Code: Select all

@echo off &setlocal
call :listdir "C:\auto_pkg_build\Workspace\Build_pkg\T592-08878-0101_Link2500_TCPX_NARInfra_GEN_PROD\T592-08878-0101"
goto :eof

:listdir
pushd "%~1"
2>nul del "bottom_contents.txt"
>"Contents.lst" type nul
for /f "delims=" %%i in ('dir /b') do (
  if "%%~ai" geq "d" (
    >>"Contents.lst" echo %%i\ -^> subdirectories
  ) else if /i "%%i"=="Contents.lst" (
    >>"Contents.lst" echo %%i -^> Directory contents (plain text^)
  ) else if /i "%%~xi"==".txt" (
    >>"Contents.lst" echo %%i -^> text file
  ) else (
    >>"Contents.lst" echo %%i
  )
)
for /f "delims=" %%j in ('dir /ad /b') do call :listdir "%%j"
popd
goto :eof
Steffen
Hi Steffen,

I cann't help saying that you are genius. Yes it works perfectly !!!! :D

The funny bottom_contents.txt can be ignored after I modified to add the top part of this contents.lst. Here is the one with addition.

Code: Select all

@echo off &setlocal

call C:\auto_pkg_build\Scripts\scheme_replace\get_src.cmd
call :listdir "C:\auto_pkg_build\Workspace\Build_pkg\%npkg%\%nptn%"
goto :eof

:listdir
pushd "%~1"
2>nul del "bottom_contents.txt"
>"Contents.lst" type nul

set dir=DIRECTORY: \"%nptn%"
set vol=VOLUME:  %nptn%
set des=DESCRIPTION:  Directory Contents
set prj=PROJECT:  Tetra %dev_cust%

>>"Contents.lst" echo %dir%
>>"Contents.lst" echo %vol%
>>"Contents.lst" echo %des%
>>"Contents.lst" echo %prj%
>>"Contents.lst" echo.
>>"Contents.lst" echo DIRECTORY CONTENTS
>>"Contents.lst" echo ------------------

for /f "delims=" %%i in ('dir /b') do (

    if "%%~ai" geq "d" (
    >>"Contents.lst" echo %%i\ -^> subdirectories
  ) else if /i "%%i"=="Contents.lst" (
    >>"Contents.lst" echo %%i -^> Directory contents (plain text^)
  ) else if /i "%%~xi"==".txt" (
    >>"Contents.lst" echo %%i -^> text file
  ) else (
    >>"Contents.lst" echo %%i
  )
)
for /f "delims=" %%j in ('dir /ad /b') do call :listdir "%%j"
popd
goto :eof
From the above code, how can I add a dynamic directory level in the below set command
set dir=DIRECTORY: \"%nptn%" --> set dir=DIRECTORY: \"%nptn%\subfolder" (this one for the subfolder level contents.lst)

One more thing, is possible to group subfolders list on top of the other type of list, more like windows explorer display....
Currently, it list in alphabetical order
Thanks aaaaagain !!!

aGerman
Expert
Posts: 3969
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to Create a File List with some modification using Simple Batch File

#4 Post by aGerman » 17 Jul 2018 13:30

goodywp wrote:
17 Jul 2018 12:34
From the above code, how can I add a dynamic directory level in the below set command
set dir=DIRECTORY: \"%nptn%" --> set dir=DIRECTORY: \"%nptn%\subfolder" (this one for the subfolder level contents.lst)
Sorry but you lost me. After the PUSHD command the current working directory is changed to the passed folder. Maybe you're just looking for the %cd% variable?
goodywp wrote:
17 Jul 2018 12:34
One more thing, is possible to group subfolders list on top of the other type of list, more like windows explorer display....
Sure, /ad is for folders and /a-d excludes folders in the DIR command. Just use two separate FOR /F loops.

Untested

Code: Select all

@echo off &setlocal
call :listdir "C:\auto_pkg_build\Workspace\Build_pkg\T592-08878-0101_Link2500_TCPX_NARInfra_GEN_PROD\T592-08878-0101"
goto :eof

:listdir
pushd "%~1"
2>nul del "bottom_contents.txt"
>"Contents.lst" type nul
for /f "delims=" %%i in ('dir /ad /b') do (
  >>"Contents.lst" echo %%i\ -^> subdirectories
  call :listdir "%%i"
)
for /f "delims=" %%i in ('dir /a-d /b') do (
  if /i "%%i"=="Contents.lst" (
    >>"Contents.lst" echo %%i -^> Directory contents (plain text^)
  ) else if /i "%%~xi"==".txt" (
    >>"Contents.lst" echo %%i -^> text file
  ) else (
    >>"Contents.lst" echo %%i
  )
)
popd
goto :eof
Steffen

goodywp
Posts: 198
Joined: 31 Jul 2017 09:57

Re: How to Create a File List with some modification using Simple Batch File

#5 Post by goodywp » 17 Jul 2018 13:52

aGerman wrote:
17 Jul 2018 13:30
goodywp wrote:
17 Jul 2018 12:34
From the above code, how can I add a dynamic directory level in the below set command
set dir=DIRECTORY: \"%nptn%" --> set dir=DIRECTORY: \"%nptn%\subfolder" (this one for the subfolder level contents.lst)
Sorry but you lost me. After the PUSHD command the current working directory is changed to the passed folder. Maybe you're just looking for the %cd% variable?
goodywp wrote:
17 Jul 2018 12:34
One more thing, is possible to group subfolders list on top of the other type of list, more like windows explorer display....
Sure, /ad is for folders and /a-d excludes folders in the DIR command. Just use two separate FOR /F loops.

Untested

Code: Select all

@echo off &setlocal
call :listdir "C:\auto_pkg_build\Workspace\Build_pkg\T592-08878-0101_Link2500_TCPX_NARInfra_GEN_PROD\T592-08878-0101"
goto :eof

:listdir
pushd "%~1"
2>nul del "bottom_contents.txt"
>"Contents.lst" type nul
for /f "delims=" %%i in ('dir /ad /b') do (
  >>"Contents.lst" echo %%i\ -^> subdirectories
  call :listdir "%%i"
)
for /f "delims=" %%i in ('dir /a-d /b') do (
  if /i "%%i"=="Contents.lst" (
    >>"Contents.lst" echo %%i -^> Directory contents (plain text^)
  ) else if /i "%%~xi"==".txt" (
    >>"Contents.lst" echo %%i -^> text file
  ) else (
    >>"Contents.lst" echo %%i
  )
)
popd
goto :eof
Steffen
Yes! works as expected!!!
Now come to the first question. This is the output of my addition.

DIRECTORY: \"T592-08878-0101"
VOLUME: T592-08878-0101
DESCRIPTION: Directory Contents
PROJECT: Tetra Link2500_TCPX

For each level contents.lst, it always sit at the top and same, only difference is the
DIRECTORY: \"T592-08878-0101"
it depends on the subfolder level....
Not sure how %cd% shall works? or just move in my addition below PUSHD command ?
Only one place I need in my added code

Code: Select all

...
set dir=DIRECTORY: \"%nptn%\%cd%"
...

aGerman
Expert
Posts: 3969
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to Create a File List with some modification using Simple Batch File

#6 Post by aGerman » 17 Jul 2018 14:06

The passed folder is in %~1. The %cd% variable is set automatically and contains the current working directory.

Steffen

goodywp
Posts: 198
Joined: 31 Jul 2017 09:57

Re: How to Create a File List with some modification using Simple Batch File

#7 Post by goodywp » 17 Jul 2018 14:35

aGerman wrote:
17 Jul 2018 14:06
The passed folder is in %~1. The %cd% variable is set automatically and contains the current working directory.

Steffen
I tried %cd% as below code and works but got some issue

Code: Select all

set path=%cd%
for /f "tokens=1,2,3,4,5,6,7,8,9,10,11 delims=\ " %%a in ("%path%") do set base=%%f\%%g\%%h\%%i\%%j\%%k
set dir=DIRECTORY: \"%base%"
When I run and got the expected result. However, I also had some side effect if it his to %%f then I got %%f\\\\\
if it hit %%g then I got %%f\%%g\\\\, how I can get rid of the tail part of \\\. :oops:
another bad thing from the above code is that if the folder name as "factory download" it treated as ..\factory\download\.. instead of ..\factory download\..
Or you have better code for this?

Squashman
Expert
Posts: 4197
Joined: 23 Dec 2011 13:59

Re: How to Create a File List with some modification using Simple Batch File

#8 Post by Squashman » 17 Jul 2018 15:09

goodywp wrote:
17 Jul 2018 14:35
another bad thing from the above code is that if the folder name as "factory download" it treated as ..\factory\download\.. instead of ..\factory download\..
The DELIMS option is using a \ and SPACE as delimiters. Remove the space.

And I am pretty sure you have been told this before but I will say it again. DO NOT EVER OVERWRITE THE SYSTEM VARIABLE PATH.

If you are looking to split up a folder path into multiple variables you can do this.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set i=1
set "x=%CD%"
set "x!i!=%x:\=" & set /A i+=1 & set "x!i!=%"
set x
pause
output

Code: Select all

x=C:\Users\Squashman\Desktop
x1=C:
x2=Users
x3=Squashman
x4=Desktop

aGerman
Expert
Posts: 3969
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to Create a File List with some modification using Simple Batch File

#9 Post by aGerman » 17 Jul 2018 15:20

DO NOT OVERWRITE the predefined PATH variable!
You don't even need an additional variable because you already have %cd%.

Code: Select all

for /f "tokens=5* delims=\" %%a in ("%cd%") do set "base=%%b"
Steffen

goodywp
Posts: 198
Joined: 31 Jul 2017 09:57

Re: How to Create a File List with some modification using Simple Batch File

#10 Post by goodywp » 17 Jul 2018 16:00

aGerman wrote:
17 Jul 2018 15:20
DO NOT OVERWRITE the predefined PATH variable!
You don't even need an additional variable because you already have %cd%.

Code: Select all

for /f "tokens=5* delims=\" %%a in ("%cd%") do set "base=%%b"
Steffen
Excellent! works as expected... :)

Post Reply