PC Review


Reply
Thread Tools Rate Thread

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

 
 
Dmitry Anikin
Guest
Posts: n/a
 
      4th Apr 2005
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?
 
Reply With Quote
 
 
 
 
Dmitry Anikin
Guest
Posts: n/a
 
      4th Apr 2005
Putting your thoughts to paper really improves thinking,
I've found a nicer way:

for /f %%i in ('dir /a /b folder') do goto notempty

And also it checks hidden files.
But still, I'd like something which could be used in an IF
 
Reply With Quote
 
Jerold Schulman
Guest
Posts: n/a
 
      4th Apr 2005
On Mon, 4 Apr 2005 15:47:36 +0400, "Dmitry Anikin" <(E-Mail Removed)> wrote:

>Putting your thoughts to paper really improves thinking,
>I've found a nicer way:
>
>for /f %%i in ('dir /a /b folder') do goto notempty
>
>And also it checks hidden files.
>But still, I'd like something which could be used in an IF


set pipe="%TEMP%\Found_%RANDOM%.TMP"
dir /a /b folder>%pipe% 2>&1
set OK=Y
for /f "Tokens=*" %%f in ('type %pipe%^|FINDSTR /B /L /C:"File Not Found"') do set OK=N
if "%OK%" EQU "N" goto MT


Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
 
Reply With Quote
 
Dean Wells [MVP]
Guest
Posts: n/a
 
      4th Apr 2005
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.

--
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

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?



 
Reply With Quote
 
Dean Wells [MVP]
Guest
Posts: n/a
 
      4th Apr 2005
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?



 
Reply With Quote
 
alex
Guest
Posts: n/a
 
      5th Apr 2005
> I tried:
> if exists folder\*.*
> if exists folder\*
> if exists folder\?
> if exists folder\?.?


use this:

IF EXIST "C:\Windows"

or

IF /I EXIST "C:\Windows"

or (not 100% sure)

IF EXIST "C:\Windows\Nul"


Alex


 
Reply With Quote
 
Al Dunbar [MS-MVP]
Guest
Posts: n/a
 
      6th Apr 2005

"alex" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> > I tried:
> > if exists folder\*.*
> > if exists folder\*
> > if exists folder\?
> > if exists folder\?.?

>
> use this:
>
> IF EXIST "C:\Windows"
>
> or
>
> IF /I EXIST "C:\Windows"
>
> or (not 100% sure)


IF EXIST is not case sensitive - it doesn't do a string comparison between
the name in the script and the correctly capitalized name of a file whose
name contains the same characters but of a different case, so the /I is not
applicable.

> IF EXIST "C:\Windows\Nul"


I think the problem here is that in 9x and earlier:

- if exist folder ALWAYS failed;
- no actual file or folder could exist with the name NUL;
- every actual folder contained an instance of every device, including NUL

This differentiation, as embodied in the "...\nul" trick you mention, made
it possible to determine if a potential name referred to:

- an actual file
- an actual folder
- a non-existent file or folder.

Since IF EXIST returns true for folders and files with NT and up, it is now
a little trickier to make this same determination.

/Al


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
check in "empty (deleted items) folder" bjwendell Windows Vista Mail 5 22nd Nov 2008 06:10 PM
Check for file in folder, if not there, open folder to rename file Don M. Microsoft Excel Programming 9 22nd Oct 2008 07:34 PM
file folder icons display non-empty folders as empty nodaway Windows Vista File Management 0 24th Dec 2007 07:57 PM
Solution to: Can't create file:.xls. Right click folder you want to create the file in and then click properties on the shortcut menu to check your permissions on this folder Ky_fanatic Microsoft Outlook Discussion 1 8th Dec 2006 06:15 PM
check whether a folder is empty =?Utf-8?B?Q3VyaW91c01l?= Windows XP WMI 1 8th Jan 2006 10:07 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:19 PM.