To List Path of file name and Folders
Moderator: DosItHelp
-
- Posts: 11
- Joined: 29 Apr 2013 22:54
To List Path of file name and Folders
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
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
Re: To List Path of file name and Folders
Just because you may not be aware of it, this gives you all files in a useful way to process. /a-d
So you want a list of empty folders, right? What is the purpose?
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?
Re: To List Path of file name and Folders
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.
-
- Posts: 11
- Joined: 29 Apr 2013 22:54
Re: To List Path of file name and Folders
foxidrive wrote:Just because you may not be aware of it, this gives you all files in a useful way to process. /a-dCode: 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.
Re: To List Path of file name and Folders
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
-
- Posts: 11
- Joined: 29 Apr 2013 22:54
Re: To List Path of file name and Folders
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-dCode: 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
Re: To List Path of file name and Folders
my code does accurate the same .....
-
- Posts: 11
- Joined: 29 Apr 2013 22:54
Re: To List Path of file name and Folders
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.
Re: To List Path of file name and Folders
delete empty folders:
the rd command without /s removes only 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
Re: To List Path of file name and Folders
Aacini wrote: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.Code: Select all
rem If name have extension, file is assumed
if "%%~Xa" neq "" (
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