-1

I am trying to check if several folders are empty in batch. By empty, I mean "has no files". I am passing each folder to a separate script. Seems like it has a syntax issue. Could you provide help please ? Thanks ! Please note that I am looking at SEVERAL FOLDERS, ONE BY ONE. Thanks!

Script CopyFiles.bat

for /F %%i in ('dir /b %~1*.*') do (
   echo Folder is NON empty
 )
echo Folder is empty or does not exist

Main Script :

@echo off

for /F %%i in ('dir /b C:\*.*') do 
(
    call CopyFiles.bat %%i
)
LouInNY
  • 111
  • 1
  • 2
  • 9
  • 1
    Possible duplicate of [Check if any type of files exist in a directory using BATCH script](http://stackoverflow.com/questions/10813943/check-if-any-type-of-files-exist-in-a-directory-using-batch-script) – Squashman Oct 05 '16 at 01:25
  • 1
    There *is* a syntax issue - in `Main`, the `(` following the `do` **must** be on the same physical line as the `do`. You should specify what happens for you, since we can't see over your shoulder and it may behave differently on others' machines. There *are* other issues, but following the myriad examples of how to approach your (unstated) aim available on SO should lead you to a solution. – Magoo Oct 05 '16 at 01:39
  • Thanks @Magoo What I am trying to do is to go through all the folders in C and check one by one if there are empty. In C, I have a succession of folders : User1, User2, User3 , etc. And I want to know which one is empty, which one is not. Let me know if you have any ideas . Thank you ! – LouInNY Oct 05 '16 at 01:48
  • 1
    Depends on **Your** definition of `empty`. Do you mean 'has no files` or `has no subdirectories` or `has neither files nor subdirectories`? All of this data should be provided by editing it into the question (edit button) as we don't assemble data from comments (usually) - all of the relevant data in one place, please! (and follow Squishman's link, which will provide relevant clues) – Magoo Oct 05 '16 at 01:51
  • Thanks again @Magoo. I edited the question accordingly. I meant ":has no files" by empty. Also, Squishman's link solves the issue for only one folder, and not several folders as I need. Thanks. – LouInNY Oct 05 '16 at 01:54
  • 1
    @LouInNY, if you understand the code from the link I posted, you can adapt that to your needs. Pretty sure the i on the keyboard is nowhere near the a. – Squashman Oct 05 '16 at 01:59
  • Thanks Squashman. U mean something along those lines ? I am not really good at batch files .. : @echo off for /F %%i in ('dir /b C:\*.*') do ( dir /a-d "%%i\*" && (echo Files exist) || (echo No file found) ) – LouInNY Oct 05 '16 at 02:09
  • I got it to work ! Thanks ! – LouInNY Oct 05 '16 at 02:18

2 Answers2

0

One idea is to loop on the folders and use the "dir /a-d" command as suggested by Squashman :

 @echo off

 for /F %%i in ('dir /b "C:\*.*"') do (
    echo C:\%%i
    dir /a-d "C:\%%i\*" && (echo Files exist) || (echo No file found)
 )
 pause
LouInNY
  • 111
  • 1
  • 2
  • 9
  • As written, this won't work for folders with spaces or ampersands. You're also processing files without the /ad switch. I would suggest changing the FOR /F to `FOR /D %%i IN (C:\*) DO (` – soja Oct 05 '16 at 04:45
0

Example:

@WHERE /Q "%~1:*"&&(@ECHO=Has files&@TIMEOUT 4)||(@ECHO=Has no files&@TIMEOUT 4)
Compo
  • 36,585
  • 5
  • 27
  • 39