If not exist *

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

If not exist *

#1 Post by alleypuppy » 10 Jun 2011 20:12

Hi all,

I am trying to check if a folder contains files and I want to use

Code: Select all

if not exist * echo Folder is empty!
but it doesn't seem to work. Any suggestions?

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: If not exist *

#2 Post by renzlo » 10 Jun 2011 20:25

Hi aleypuppy,

Try this:

Code: Select all

if not exist * (echo Folder is not empty!) else (echo Folder is empty!)


-renzlo

alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

Re: If not exist *

#3 Post by alleypuppy » 10 Jun 2011 20:43

renzlo wrote:Hi aleypuppy,

Try this:

Code: Select all

if not exist * (echo Folder is not empty!) else (echo Folder is empty!)


-renzlo

Ehh this isn't working properly either. It says "Folder is not empty!" even if it is empty.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: If not exist *

#4 Post by dbenham » 10 Jun 2011 21:11

I believe the if not exist * is seeing the implicit . folder (current directory) that exists in every folder, therefore it will never test true.

The FOR command can be used instead.

Use this to see if the current directory contains any files:

Code: Select all

set "file="
for %%f in (*) do set file=1
if defined file (echo directory contains one or more files) else echo directory contains NO files


Use this to see if the current directory contains any folders

Code: Select all

set "dir="
for /d %%f in (*) do set dir=1
if defined dir (echo directory contains one or more folders) else echo directory contains NO folders


Of course you could do something like

Code: Select all

set "file="
for %%f in (*) do set /a file+=1
if you wanted to get a count of the number of files. File will still be undefined if no files exist, otherwise it will contain the count of files. Or you could initialize file to 0 if you always want a value.

I'm not sure what will happen if the directory contains only hidden or system files or directories. I'm not sure if the FOR command will see them.

Dave Benham

alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

Re: If not exist *

#5 Post by alleypuppy » 10 Jun 2011 21:28

dbenham wrote:I believe the if not exist * is seeing the implicit . folder (current directory) that exists in every folder, therefore it will never test true.

The FOR command can be used instead.

Use this to see if the current directory contains any files:

Code: Select all

set "file="
for %%f in (*) do set file=1
if defined file (echo directory contains one or more files) else echo directory contains NO files


Use this to see if the current directory contains any folders

Code: Select all

set "dir="
for /d %%f in (*) do set dir=1
if defined dir (echo directory contains one or more folders) else echo directory contains NO folders


Of course you could do something like

Code: Select all

set "file="
for %%f in (*) do set /a file+=1
if you wanted to get a count of the number of files. File will still be undefined if no files exist, otherwise it will contain the count of files. Or you could initialize file to 0 if you always want a value.

I'm not sure what will happen if the directory contains only hidden or system files or directories. I'm not sure if the FOR command will see them.

Dave Benham

Thank you dbenham, that worked perfectly! :wink:

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: If not exist *

#6 Post by !k » 12 Jun 2011 10:31

Use this to see if the current directory contains any files:
...
Use this to see if the current directory contains any folders
...

what about

Code: Select all

set "empty=true"
for /f %%a in ('dir /b/x') do set "empty=false"

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: If not exist *

#7 Post by Acy Forsythe » 12 Jun 2011 21:20

This is an old trick, but I needed this exact same thing a couple of weeks ago, so it's fresh in my memory and it works up to Windows7.

IF NOT EXIST C:\MyDirectory\NUL ECHO It's NOT THERE


On a related note, this was a few weeks ago, but I *think* it had problems with spaces in the path, and quoting doesn't seem to fix it. I could be thinking of another command, but I'm fairly certain it was this one that had the problem.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: If not exist *

#8 Post by !k » 13 Jun 2011 12:08

[off]

Code: Select all

IF NOT EXIST C:\MyDirectory\NUL ECHO It's FILE but _NOT_ FOLDER

[/off]

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: If not exist *

#9 Post by Acy Forsythe » 13 Jun 2011 13:42

[off]

Code:
IF NOT EXIST C:\MyDirectory\NUL ECHO It's FILE but _NOT_ FOLDER

[/off]


I misread the question... No harm done.

Post Reply