Re: How can I check if a folder is empty in bat file?



Having sync'd my newsgroups it seems you'd already found that one ... or
a close derivative!

I've since taken another look at using the "if" statement to achieve
your goal and found many other way to get there, none of which use "if"
exclusively ... there may well be a creative way but I've yet to find it
I'm afraid!

Sorry ... Dean

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l

Dean Wells [MVP] wrote:
> Within a script, use the following (no doubt inadvertently line
> wrapped) one-liner:
>
> if not "%~f1"=="" for /f %%e in ('dir "%~f1\*" /b/a') do goto
>> LABELINDICATINGDIRECTORYNOTEMPTY
>
> Create a label named accordingly that handles your requirements when
> the directory is not empty.
>
>
> Dmitry Anikin wrote:
>> I'm using win xp sp2.
>> The folder exists, that I know for sure, but I must know if it
>> contains any file or subfolder. Is there any SIMPLE way to do it????
>>
>> I tried:
>> if exists folder\*.*
>> if exists folder\*
>> if exists folder\?
>> if exists folder\?.?
>> ---- it always true, I think because there're always "." and ".."
>> subfolders in a folder.
>> I could use something like: dir folder /b > tmpfile, then check
>> whether tmpfile is empty but it is very cumbersome and I (don't want)
>>>> (cannot ) use temp
>> files.
>> I tried dir folder /b | set /p myvar=
>> But you cannot access myvar later, 'cause it's created in another
>> context. By the way, why doesn't this work:
>> dir folder /b | (set /p myvar= & echo %myvar%)
>> or
>> dir folder /b | (setlocal enabledelayedexpansion & set /p myvar= &
>> echo !myvar!) -it echoes string '!myvar!', although
>> dir folder /b | (set /p myvar= & set myvar)
>> shows that myvar really gets the name of first item in a dir
>>
>> I tried xcopy /e folder\*.* nul and it always returns an error
>> although copy folder\*.* nul can be used and it returns errorlevel 1
>> if no file exists, but it doesn't care for subfolders! Also, copying
>> a big folder even to nul can be a performance problem.
>>
>> for %%i in (folder\*) ---- doesn't check subdirs
>> if FOR checks subdirs (like in: for /r %%i in (.), for /d ...) it
>> doesn't check files For now I've settled for this:
>>
>> for %%i in (folder\*) do goto notempty
>> for /d %%i in (folder\*) do goto notempty
>> rem here it's empty
>> goto cont
>>> notempty
>> rem and here it is not
>>
>> but it's way too complex for such an easy task,
>> and also FOR doesn't check for hidden files.
>>
>> Any suggestions?


.



Relevant Pages

  • Re: How can I check if a folder is empty in bat file?
    ... directory is not empty. ... > subfolders in a folder. ... but it doesn't care for subfolders! ... > rem and here it is not ...
    (microsoft.public.win2000.cmdprompt.admin)
  • Re: The directory is not empty
    ... directory is not empty. ... undeletable folder. ... >all the subfolders and files within it. ... >> files and subfolders and send them to the recycle bin ...
    (microsoft.public.win2000.file_system)
  • My Documents Subfolders ? Limit?
    ... Is there a limit to the number of subfolders that can be put into My ... I just moved the (EMPTY) 'my documents' folder to the second hard drive. ...
    (microsoft.public.windowsxp.configuration_manage)
  • Re: Directory.Delete(path, true) raise an exception for non empty directories..
    ... > What command should I run to delete folders even if theirs subfolders are ... > not empty with files ... the folder cannot be deleted. ... you shold catch the exception and prompt it to user to handle. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Disappearing Network Share Subfolders
    ... Thank you for posting to the SBS Newsgroup. ... I understand that some of your Windows XP SP2 clients cannot view the ... subfolders in share folders on SBS 2K Server. ... Does the problematic share folder locate at NTFS or FAT32 disk? ...
    (microsoft.public.windows.server.sbs)

Loading