Page 1 of 1
If not exist *
Posted: 10 Jun 2011 20:12
by alleypuppy
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?
Re: If not exist *
Posted: 10 Jun 2011 20:25
by renzlo
Hi aleypuppy,
Try this:
Code: Select all
if not exist * (echo Folder is not empty!) else (echo Folder is empty!)
-renzlo
Re: If not exist *
Posted: 10 Jun 2011 20:43
by alleypuppy
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.
Re: If not exist *
Posted: 10 Jun 2011 21:11
by dbenham
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
Re: If not exist *
Posted: 10 Jun 2011 21:28
by alleypuppy
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!
Re: If not exist *
Posted: 12 Jun 2011 10:31
by !k
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"
Re: If not exist *
Posted: 12 Jun 2011 21:20
by Acy Forsythe
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.
Re: If not exist *
Posted: 13 Jun 2011 12:08
by !k
[off]
Code: Select all
IF NOT EXIST C:\MyDirectory\NUL ECHO It's FILE but _NOT_ FOLDER
[/off]
Re: If not exist *
Posted: 13 Jun 2011 13:42
by Acy Forsythe
[off]
Code:
IF NOT EXIST C:\MyDirectory\NUL ECHO It's FILE but _NOT_ FOLDER
[/off]
I misread the question... No harm done.