Computing.Net > Forums > Programming > How tell if folder has files or not

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

How tell if folder has files or not

Reply to Message Icon

Name: CKotlowski
Date: August 24, 2008 at 21:55:54 Pacific
OS: vista
CPU/Ram: dual core
Product: toshiba
Comment:

If the following script finds the deepest subfolders of the directory:

@echo off
setlocal enabledelayedexpansion
set /a N=0
for /r "C:\MainFld" %%a in (.) do ( dir /ad /b "%%a" | find /v "" >nul
if errorlevel 1 (
set /a N+=1
ren "%%a" Fld!N!
)
)

How could I modify this to find every subfolder in the directory that have one or more files... excluding the subfolders that have no files...

For example, given this path:
C:\mainFld\Folder\SubFolder\SubSubFolder

And say there were stray files under Folder and SubSubFolder, but not SubFolder... (say the only thing under SubFolder was SubSubfolder and no files)... how could I exclude SubFolder from the above script.
Thank you for your time and continued help!



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: August 25, 2008 at 07:52:18 Pacific
Reply:

I don't follow your script, but you can find which folders are empty [no files].

dir /b/a-d foldname
if errorlevel 1 echo it's empty


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: CKotlowski
Date: August 26, 2008 at 07:06:30 Pacific
Reply:

Thank-you M2, this works...

But how would I do this for every folder in a directory?

I tried

setlocal enabledelayedexpansion

for /f "tokens=* delims= " %%a in ('dir /b/a-d "C:\MainFolder"') do (
if errorlevel 1 echo %%a it's empty
)

but couldn't get this (or other attempts) to work...

Thank you for your help.


0

Response Number 3
Name: Mechanix2Go
Date: August 26, 2008 at 21:19:05 Pacific
Reply:

@echo off
setLocal EnableDelayedExpansion

set /p D=start where?
pushd !D!

for /f "tokens=* delims= " %%a in ('dir/b/s/ad') do (
dir/b/a-d "%%a" > nul 2> nul
if errorlevel 1 echo %%a is empty
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 4
Name: CKotlowski
Date: August 28, 2008 at 07:03:53 Pacific
Reply:

Thank you M2! works like a charm.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


batch move files log help Batch Extract Text File &...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: How tell if folder has files or not

How to edit a .bat file or text file?? www.computing.net/answers/programming/how-to-edit-a-bat-file-or-text-file/2682.html

C programming (array) www.computing.net/answers/programming/c-programming-array/6811.html

Make new folder from file name www.computing.net/answers/programming/make-new-folder-from-file-name/16189.html