Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 487 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to test if a file is a directory in a batch script?

#1
Is there any way to find out if a file is a directory?

I have the file name in a variable. In Perl I can do this:

if(-d $var) { print "it's a directory\n" }
Reply

#2
<h2>The NUL technique seems to only work on 8.3 compliant file names.</h2>

<h3>(In other words, `D:\Documents and Settings` is "bad" and `D:\DOCUME~1` is "good")</h3>

---------------

I think there is some difficulty using the "NUL" tecnique when there are SPACES in the directory name, such as "Documents and Settings."

I am using Windows XP service pack 2 and launching the cmd prompt from %SystemRoot%\system32\cmd.exe

Here are some examples of what DID NOT work and what DOES WORK for me:

(These are all demonstrations done "live" at an interactive prompt. I figure that you should get things to work there before trying to debug them in a script.)

**This DID NOT work:**

`D:\Documents and Settings>if exist "D:\Documents and Settings\NUL" echo yes`

**This DID NOT work:**

`D:\Documents and Settings>if exist D:\Documents and Settings\NUL echo yes`

**This DOES work (for me):**

`D:\Documents and Settings>cd ..`

`D:\>REM get the short 8.3 name for the file`

`D:\>dir /x`

` Volume in drive D has no label.`
` Volume Serial Number is 34BE-F9C9`

` Directory of D:\`
<BR>
`09/25/2008 05:09 PM <DIR> 2008`<BR>
`09/25/2008 05:14 PM <DIR> 200809~1.25 2008.09.25`<BR>
`09/23/2008 03:44 PM <DIR> BOOST_~3 boost_repo_working_copy`<BR>
`09/02/2008 02:13 PM 486,128 CHROME~1.EXE ChromeSetup.exe`<BR>
`02/14/2008 12:32 PM <DIR> cygwin`<BR>

[[Look right here !!!! ]]<br>
`09/25/2008 08:34 AM <DIR> DOCUME~1 Documents and Settings`<BR>

`09/11/2008 01:57 PM 0 EMPTY_~1.TXT empty_testcopy_file.txt`<BR>
`01/21/2008 06:58 PM <DIR> NATION~1 National Instruments Downloads`<BR>
`10/12/2007 11:25 AM <DIR> NVIDIA`<BR>
`05/13/2008 09:42 AM <DIR> Office10`<BR>
`09/19/2008 11:08 AM <DIR> PROGRA~1 Program Files`<BR>
`12/02/1999 02:54 PM 24,576 setx.exe`<BR>
`09/15/2008 11:19 AM <DIR> TEMP`<BR>
`02/14/2008 12:26 PM <DIR> tmp`<BR>
`01/21/2008 07:05 PM <DIR> VXIPNP`<BR>
`09/23/2008 12:15 PM <DIR> WINDOWS`<BR>
`02/21/2008 03:49 PM <DIR> wx28`<BR>
`02/29/2008 01:47 PM <DIR> WXWIDG~2 wxWidgets`<BR>
` 3 File(s) 510,704 bytes`<BR>
` 20 Dir(s) 238,250,901,504 bytes free`<BR>

`D:\>REM now use the \NUL test with the 8.3 name`

`D:\>if exist d:\docume~1\NUL echo yes`

`yes`

**This works, but it's sort of silly, because the dot already implies i am in a directory:**

`D:\Documents and Settings>if exist .\NUL echo yes`
Reply

#3
Here's a script that uses FOR to build a fully qualified path, and then pushd to test whether the path is a directory. Notice how it works for paths with spaces, as well as network paths.

@echo off
if [%1]==[] goto usage

for /f "delims=" %%i in ("%~1") do set MYPATH="%%~fi"
pushd %MYPATH% 2>nul
if errorlevel 1 goto notdir
goto isdir

:notdir
echo not a directory
goto exit

:isdir
popd
echo is a directory
goto exit

:usage
echo Usage: %0 DIRECTORY_TO_TEST

:exit

Sample output with the above saved as "isdir.bat":

C:\>isdir c:\Windows\system32
is a directory

C:\>isdir c:\Windows\system32\wow32.dll
not a directory

C:\>isdir c:\notadir
not a directory

C:\>isdir "C:\Documents and Settings"
is a directory

C:\>isdir \
is a directory

C:\>isdir \\ninja\SharedDocs\cpu-z
is a directory

C:\>isdir \\ninja\SharedDocs\cpu-z\cpuz.ini
not a directory

Reply

#4
I use this:

if not [%1] == [] (
pushd %~dpn1 2> nul
if errorlevel == 1 pushd %~dp1
)
Reply

#5
Further to my previous offering, I find this also works:

if exist %1\ echo Directory

No quotes around %1 are needed because the caller will supply them.
This saves one entire keystroke over my answer of a year ago ;-)
Reply

#6
Under Windows 7 and XP, I can't get it to tell files vs. dirs on mapped drives. The following script:

<pre>
@echo off
if exist c:\temp\data.csv echo data.csv is a file
if exist c:\temp\data.csv\ echo data.csv is a directory
if exist c:\temp\data.csv\nul echo data.csv is a directory
if exist k:\temp\nonexistent.txt echo nonexistent.txt is a file
if exist k:\temp\something.txt echo something.txt is a file
if exist k:\temp\something.txt\ echo something.txt is a directory
if exist k:\temp\something.txt\nul echo something.txt is a directory
</pre>

produces:

<pre>
data.csv is a file
something.txt is a file
something.txt is a directory
something.txt is a directory
</pre>

So beware if your script might be fed a mapped or UNC path. The pushd solution below seems to be the most foolproof.
Reply

#7
This works perfectly

if exist "%~1\" echo Directory

we need to use %~1 to remove quotes from %1, and add a backslash at end. Then put thw whole into qutes again.
Reply

#8
Based on [this article](

[To see links please register here]

) titled "How can a batch file test existence of a directory" it's "not entirely reliable".


BUT I just tested this:

@echo off
IF EXIST %1\NUL goto print
ECHO not dir
pause
exit
:print
ECHO It's a directory
pause

and it seems to work
Reply

#9
This is the code that I use in my BATCH files

```
@echo off
set param=%~1
set tempfile=__temp__.txt
dir /b/ad > %tempfile%
set isfolder=false
for /f "delims=" %%i in (temp.txt) do if /i "%%i"=="%param%" set isfolder=true
del %tempfile%
echo %isfolder%
if %isfolder%==true echo %param% is a directory

```

Reply

#10
A very simple way is to check if the child exists.

If a child does not have any child, the `exist` command will return false.

IF EXIST %1\. (
echo %1 is a folder
) else (
echo %1 is a file
)

You may have some false negative if you don't have sufficient access right (I have not tested it).
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through