Page 1 of 1

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

Posted: 17 Jul 2018 08:17
by goodywp
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

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

Posted: 17 Jul 2018 10:29
by aGerman
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

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

Posted: 17 Jul 2018 12:34
by goodywp
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 !!!

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

Posted: 17 Jul 2018 13:30
by aGerman
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

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

Posted: 17 Jul 2018 13:52
by goodywp
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%"
...

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

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

Steffen

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

Posted: 17 Jul 2018 14:35
by goodywp
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?

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

Posted: 17 Jul 2018 15:09
by Squashman
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

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

Posted: 17 Jul 2018 15:20
by aGerman
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

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

Posted: 17 Jul 2018 16:00
by goodywp
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... :)