0

Most companies I have worked for start new projects with a templated folder structure. Windows will automatically flag empty folders (the icon shown in the file explorer is that of an empty folder) however, folders whose subfolders are also empty will not be flagged (their icon shows a folder containing files). This can create confusion and lead to mistakes during the early stages of a project as it would appear at a glance that some folders have had their content added when they really only contain empty subfolders.

So my question is:

How can I iterate over folders with empty subfolders and "flag" them via the use of a Batch file.

The .bat file would need to search through subfolders to determine if a folder has any real content. If the folder does not have real content then the .bat file would need to flag it (flagging could be done with a change of its icon or filename). This would make it much easier to navigate new projects with large templated folder structures.

I don't need a completed file, I would just like to know how it could be achieved. However, any tips or suggestions on how to achieve this functionality would be more than welcome!

*Edit

Just to clarify I will show an example: If I create an empty folder called 'Project' it will display with the Empty Folder icon. As shown Below:

enter image description here

Now I will add a new folder to my project folder called 'I am Empty':

enter image description here

The folder 'Project' no longer displays with the Empty Folder icon. It now uses the icon that shows it with content. As shown Below:

enter image description here

What I want is a .bat file that will parse the contents of the 'Project' folder and determine that it only contains the 'I am Empty' folder, which is empty and "flag" that folder (to flag it we could change the icon of the 'Project' folder back to the Empty Folder icon, change the name or "gray it out"). As shown Below:

enter image description here

11
  • "Windows will automatically flag empty folders" - I don't understand, what you are trying to say here. When I create an empty folder, Windows doesn't do anything about it. It's just another folder in the file system. Commented Apr 4, 2016 at 14:44
  • Right, I want to flag folders who contain empty folder... Commented Apr 4, 2016 at 14:48
  • If PowerShell is an option, you could make your life a lot easier (see Windows PowerShell Tip of the Week: Finding All the Empty Folders in a Directory Tree). I still don't quite get, what you mean by "flag". Might make your question clearer, if you explained that a bit better. Or leave it out altogether, since that's unrelated to your question: Identifying all empty directories. Commented Apr 4, 2016 at 15:02
  • I just added some examples that demonstrated the problem better than words alone. Also, in my original question I state: (flagging could be done with a change of its icon or filename). Thus if changing the icon is not possible. I would also be ok with the .bat file renaming the folder from 'Project' to 'Project(empty)'. Commented Apr 4, 2016 at 15:07
  • What you called "flagging" and attributed to Windows is really a feature of File Explorer's Details pane. It is neither related to Windows nor a property assigned to a directory, or one that you could assign to a folder. If you want to customize the display of Explorer, a .bat file won't do. You'd have to implement a shell extension (I think a Preview Handler, but I'm not entirely sure about that). Commented Apr 4, 2016 at 15:48

2 Answers 2

2
cd example for /d %%i in (*) do @dir /b /s /a-d "%%i">nul 2>&1|| @echo "%%i" has no files 

give for an additional /r if you want to check subfolders too.

The trick is to list files only (/a-d) in the folder and all subfolders (/s) and if this fails (||) (because there are no files), do something with this folder (just echo here, but rd /s /q or ren is also possible)

Sign up to request clarification or add additional context in comments.

Comments

2

Building on the answer given by Stephan and looking at other related stack exchange posts I pieced this solution together and it works well for my needs:

@ECHO OFF PUSHD "%~dp0" FOR /f "tokens=* delims=" %%F in ('dir /b/s/ad *') DO ( @dir /s /a-d-s "%%F">nul 2>&1|| ATTRIB +H "%%F" @dir /s /a-d-s "%%F">nul 2>&1&& ATTRIB -H "%%F" ) POPD EXIT 

I opted for a solution that did not modify file names. While testing that approach I realized it doesn't create the best user experience.

Instead, running this batch file hides the folders that are effectively empty and if your folder settings allow you to see hidden folders then they appear faded out. It also re-shows hidden files that have new content since the last time the batch file was ran.

For those of you coming to this solution who, like myself, are relatively inexperienced with batch scripting. I will explain what the code does and why (as I understand it).

I don't want to change the batch file for each implementation so I call PUSHD "%~dp0 to set the active directory to the folder containing the batch file (this lets me include the batch file in the folder-structure template, which is copied and pasted for each project)

Since I decided to use the hidden attribute for folder flagging, I needed to modify the FOR loop . FOR loops typically ignore hidden files which becomes troublesome if you need show a file that was previously hidden because it has new content. Running FOR /f "tokens=* delims=" %%F in ('dir /b/s/ad *') DO () allows the batch file to loop through all files mainly because of the /f attribute, but check out this post about looping through hidden folders for more information.

Inside the for loop I am pretty much doing what Stephen suggested in his answer with the added logic to remove the hidden attribute on folders that no longer need it.

The only thing this batch file is lacking, is the ability to auto update on folder modifications or to auto-run on folder open (I hear this might be possible with an .ini file?) however, for my needs it will suffice to rerun the batch file, manually, after making changes to the folder.

Batch scripting is way outside of my comfort zone as far as scripting languages go so please forgive and correct me if I have made any mistakes or if there is a more reliable way to do what I need.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.