I need to write a batch script.

I have a folder on my desktop name "folder". inside it has many sub folders. there is no way of telling how much sub folder there are. Insidde some of sub folder there is a file.

folder 
 - sub folder 1
   - sub folder 1
   - sub folder 2
   - sub folder 3
     - file 
 - sub folder 2
   - sub folder 1
     - file 
 - sub folder 3
   - sub folder 1
 - sub folder 4
   - sub folder 1
      - sub folder

What I want to do:

  1. loop though all folder / sub folders
  2. check to see if that folder is empty or not
  3. print name of folder and weither is empty or not

Here is a example of output:

folder is not empty 
folder/sub folder 1 is not empty 
folder/sub folder 1/sub folder 1 is empty

this is power shell... i want to use batch. here is what i wrote but it print all sub folders.

@echo off

SET "MYPATH=C:\Users\dave\Desktop\shows"

 for /d %%a in ("%mypath%\*") do (
    dir /b /s "%MYPATH%"
    pause
)

i wan tto print just empty sub folders.

commented: powershell can be scripted or called from a COMMAND.COM shell. +11

Copy the following code into a file named empty.vbs

set fso = CreateObject("Scripting.FileSystemObject") 
set arg = Wscript.Arguments

if arg.Count > 0 then
    if fso.FolderExists(arg(0)) then
        ScanFolder fso.GetFolder(".")
    else
        Wscript.Echo "Folder",arg(0),"not found"
    end if
else
    ScanFolder fso.GetFolder(".")
end if

Sub ScanFolder (folder)

    on error resume next    'don't need this if running as admin

    if folder.Files.Count = 0 and folder.SubFolders.Count = 0 Then
        Wscript.Echo folder.Path,"is empty"
    end if

    for each subfolder in folder.SubFolders
        ScanFolder(subfolder)
    next

End Sub

The on error line will prevent it from aborting on an access violation if you are running as a normal user. You can run this in two ways

cscript empty.vbs foldername

will list all the empty folders. Or you can make cscript.exe (console version) the default script engine and just type

empty foldername

To make cscript the default (only have to do this once) open a command shell as Administrator and type

cscript //nologo //h:cscript //s

If you don't do this then it will use wscript.exe (windowed processor) and every wscript.echo will pop up a message box. If you don't provide a folder name then it will start in the current folder.

Your post calls for a script and then "batch" but left out that is must be in "bash", cmd.exe, command.com or other shells. Powershell can be called from a cmd or command.com shell so to be this is fine.

Yeah, but powershell requires an enormous amount of effort to learn before you can do anything really useful. And once you have a powershell script it is essentially unreadable. The nice thing about vbscript is that it is easy to understand and does not require a lot of knowledge to use effectively. Not to mention that the code skills transfer nicely to vb.net. Is

$a = Get-ChildItem C:\Scripts -recurse | Where-Object {$_.PSIsContainer -eq $True}

immediately obvious? Is $_.PSIsContainer meaningful?

commented: Yes and +1. +11

My mistake. The original code had a typo (which I have fixed in the original post). The code

if fso.FolderExists(arg(0)) then
        ScanFolder fso.GetFolder(".")

has been changed to

if fso.FolderExists(arg(0)) then
        ScanFolder fso.GetFolder(arg(0))            

My apologies

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.