To List Path of file name and Folders

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Asley Arvind
Posts: 11
Joined: 29 Apr 2013 22:54

To List Path of file name and Folders

#1 Post by Asley Arvind » 29 Apr 2013 23:10

Hi Friends,

I m trying to create a batch file to generate files and folders inside a directory with below criteria.

I have to list all files path inside the directory and also i have to list the folders path if and only if the folder is empty.

I have tried the below code it gives me all files and folders, but i don't need to get the folder path if any files are inside it. (Example:- In my C drive "C:\" If i have folder "Apple" and inside it i have a file "apple.txt" I need to extract only "C:\Apple\apple.txt" instead of getting both "C:\Apple\" and "C:\Apple\apple.txt" and if the "Apple" Folder is empty folder the i have to extract the path "C:\Apple\"

@echo off
dir /s/b C:\ > filelist.xls

please help me in this

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: To List Path of file name and Folders

#2 Post by foxidrive » 30 Apr 2013 02:45

Just because you may not be aware of it, this gives you all files in a useful way to process. /a-d

Code: Select all

@echo off
dir /s /b /a-d C:\ > filelist.xls


So you want a list of empty folders, right? What is the purpose?

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: To List Path of file name and Folders

#3 Post by Aacini » 30 Apr 2013 03:08

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set lastDir=
rem Process names of files and folders
for /F "delims=" %%a in ('dir /S /B C:\') do (
   rem If name have extension, file is assumed
   if "%%~Xa" neq "" (
      rem If is there a previous folder
      if defined lastDir (
         rem If this file's path is different than previous folder
         if "%%~DPa" neq "!lastDir!" (
            rem Previous folder is empty
            echo !lastDir!
         )
         set lastDir=
      )
      rem Show file name
      echo %%a
   ) else (
      rem If is there a previous folder, it is empty
      if defined lastDir echo !lastDir!
      rem Save previous folder
      set "lastDir=%%a\"
   )
)

This program is untested. It differentiate folders from files by the extension; if this method fails (files without extension or folders with extension), the program must be modified.

Asley Arvind
Posts: 11
Joined: 29 Apr 2013 22:54

Re: To List Path of file name and Folders

#4 Post by Asley Arvind » 30 Apr 2013 11:56

foxidrive wrote:Just because you may not be aware of it, this gives you all files in a useful way to process. /a-d

Code: Select all

@echo off
dir /s /b /a-d C:\ > filelist.xls


So you want a list of empty folders, right? What is the purpose?


Thanks for your reply.. I am aware the code you given and it will give us only files not folders but I need the empty folders path as well to clean up unnecessary folders.. i don't want to check it manually, if it is empty folder simply i can delete it from there.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: To List Path of file name and Folders

#5 Post by Endoro » 30 Apr 2013 13:21

try this:

Code: Select all

@echo off & setlocal
set "startfolder=."
set "file=filelist.xls"

pushd "%startfolder%"
(for /r %%i in (*) do echo(%%~fi
for /r /d %%i in (*) do (
   set "empty="
   for /f %%a in ('dir /b /a "%%~i"') do set "empty=%%~a"
   if not defined empty echo(%%~fi\
))>"%file%"
popd

Asley Arvind
Posts: 11
Joined: 29 Apr 2013 22:54

Re: To List Path of file name and Folders

#6 Post by Asley Arvind » 30 Apr 2013 14:04

Asley Arvind wrote:
foxidrive wrote:Just because you may not be aware of it, this gives you all files in a useful way to process. /a-d

Code: Select all

@echo off
dir /s /b /a-d C:\ > filelist.xls


So you want a list of empty folders, right? What is the purpose?


Thanks for your reply.. I am aware the code you given and it will give us only files not folders but I need the empty folders path as well to clean up unnecessary folders.. i don't want to check it manually, if it is empty folder simply i can delete it from there.



Hi Friend,

My requirement is to know what are the files and folders available inside the production folder. If we have empty folder or files that are unrelated to production environment, we have to delete it once after confirmation from the client so first i need extract the file names and empty folders list.

I have tried the below code..
@echo off
dir /b/a-d/s E:\Arv\ > try.xls
for /d /r E:\Arv\ %1 %%A in (.) do (
dir /a /b "%%~fA" 2>nul | findstr "^" >nul || echo %%~fA
)

Line 1. "dir /b/a-d/s E:\Arv\ > try.xls" This will generate a excel file which lists all files inside the Folder "E:\Arv\"
and i tried the below line of code to get empty folders inside the folder "E:/Arv/". Please correct me if i am wrong because i am new to dos commands.

Line 2. for /d /r E:\Arv\ %1 %%A in (.) do (
dir /a /b "%%~fA" 2>nul | findstr "^" >nul || echo %%~fA
)
This Line 2. code generates empty folders list output in dos prompt.
My Question is how to get the output of Line 2 in a excel file and also to merge both (Line 1 & Line 2) output into a single excel file(Sheet1 and Sheet2)?

It will be helpful if i get better way to do this.

Thanks in advance for your help

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: To List Path of file name and Folders

#7 Post by Endoro » 30 Apr 2013 14:22

my code does accurate the same ..... :)

Asley Arvind
Posts: 11
Joined: 29 Apr 2013 22:54

Re: To List Path of file name and Folders

#8 Post by Asley Arvind » 30 Apr 2013 14:47

Endoro wrote:my code does accurate the same ..... :)
Endoro wrote:try this:

Code: Select all

@echo off & setlocal[quote="Endoro"]my code does accurate the same ..... :)[/quote][quote="Endoro"]my code does accurate the same ..... :)[/quote]
set "startfolder=."
set "file=filelist.xls"

pushd "%startfolder%"
(for /r %%i in (*) do echo(%%~fi
for /r /d %%i in (*) do (
   set "empty="
   for /f %%a in ('dir /b /a "%%~i"') do set "empty=%%~a"
   if not defined empty echo(%%~fi\
))>"%file%"
popd



Hi Endoro,

This works perfect. superb.. Thank you very much for your help.

I need one more help on the same . could you please tell me how to delete those empty folders alone.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: To List Path of file name and Folders

#9 Post by Endoro » 30 Apr 2013 15:21

delete empty folders:

Code: Select all

@echo off & setlocal
set "startfolder=."

pushd "%startfolder%"
for /f "delims=" %%i in ('dir /b /ad /s ^| sort /r') do rd  "%%~fi" 2>nul
popd


the rd command without /s removes only empty folders

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: To List Path of file name and Folders

#10 Post by Liviu » 30 Apr 2013 22:38

Aacini wrote:

Code: Select all

   rem If name have extension, file is assumed
   if "%%~Xa" neq "" (
This program is untested. It differentiate folders from files by the extension; if this method fails (files without extension or folders with extension), the program must be modified.

In your context, with a loop variable plus delayed expansion enabled, the following should differentiate files vs. directories more safely by checking attributes.

Code: Select all

set "aa=%%~aa"
if "!aa:~0,1!" neq "d" (

Liviu

Post Reply