How to check if parameter is file (or directory)?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

How to check if parameter is file (or directory)?

#1 Post by pstein » 12 Nov 2011 02:15

Assume I pass a file or directory as parameter to a DOS batch script (under Win7).

How can I check inside this DOS batch script if the parameter is a file?
How can I check inside this DOS batch script if the parameter is a directory?

Keep in mind: I do NOT ask how to check if the passed object exists!

Peter

leescott
Posts: 7
Joined: 05 Nov 2011 18:58

Re: How to check if parameter is file (or directory)?

#2 Post by leescott » 12 Nov 2011 05:53

I'm not expert .
I see such one bat:

Code: Select all

@echo off
if exist test\nul (echo FOLDER) else echo FILE
pause
goto :eof

@echo off
pushd test 2>nul&&echo FOLD||echo FILE
popd
pause
goto :eof

@echo off
set "cur_dir=%cd%"
cd /d test 2>nul&&echo FOLDER||echo FILE
cd /d "%cur_dir%"
pause
goto :eof

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to check if parameter is file (or directory)?

#3 Post by Ed Dyreen » 12 Nov 2011 06:10

'
Hi pstein

You can use for /?

Code: Select all

for %%r in ( "C:\This Works\This\Works\This Works.TMP" ) do echo.set "$VAR=%%~dpr"
This will do verification without existence :wink:

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to check if parameter is file (or directory)?

#4 Post by aGerman » 12 Nov 2011 07:03

Hmm, since you could create a DIRECTORY named "file.txt" or a FILE named "folder" you can't verify it if it didn't exist.

Code: Select all

@echo off &setlocal
set "c0=folder"
set "c1=file"
set "c2=nothing"

set "f1=c:\windows"
echo "%f1%"
call :check "%f1%"
call echo is %%c%errorlevel%%%
echo(

set "f2=c:\windows\system32\cmd.exe"
echo "%f2%"
call :check "%f2%"
call echo is %%c%errorlevel%%%
echo(

set "f3=c:\does\not.exist"
echo "%f3%"
call :check "%f3%"
call echo is %%c%errorlevel%%%
echo(

pause
goto :eof

:check
pushd "%~1" 2>nul&&(popd&exit /b)||(if exist "%~1" (exit /b 1) else exit /b 2)

This should work on Win7 perhaps not on XP. I remember I had problems on XP to catch the errorlevel of pushd.

Regards
aGerman

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

Re: How to check if parameter is file (or directory)?

#5 Post by dbenham » 12 Nov 2011 10:29

I agree with aGerman - It is impossible to determine if a path represents a file path or a directory path if the path does not exist.

A file name cannot end with <backSlash>, <forwardSlash> or <colon>. So any path that ends with any of these characters must represent a folder path.

But the converse is not true. A path that does not end with <backSlash>, <forwardSlash> or <colon> could represent either.

Dave Benham

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: How to check if parameter is file (or directory)?

#6 Post by pstein » 22 Nov 2011 05:20

@aGerman:

Great, thank you.

However I would appreciate a solution WITHOUT subroutine.
Lets say I want to check if a parameter is a file. If it is a folder or does not exist it should go to the else branch. When I code now something like:

pushd "D:\data\foobar" 2>nul&&(popd&exit /b)||(if exist D:\data\foobar" (exit /b 1) else exit /b)
if %errorlevel%==1 ( Echo filebranch )else( Echo otherbranch)

then the "exit" command exists the whole script and not only the command.

So is there another solution without subroutines?

Thank you
Peter

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How to check if parameter is file (or directory)?

#7 Post by alan_b » 22 Nov 2011 06:40

At a DOS prompt I have just issued 5 commands

Code: Select all

C:\Users\Alan>MD A\B\C\D\
C:\Users\Alan>MD E\F\G\H
C:\Users\Alan>ECHO %TIME% > A\X\
The filename, directory name, or volume label syntax is incorrect.
C:\Users\Alan>ECHO %TIME% > A\X
C:\Users\Alan>type A\X
12:21:53.54

I find that I have a folder D on the path A\B\C\
and I have a folder H on the path E\F\G\

Conclusions :-

Regardless of whether there is a trailing back-slash, it is always possible that the final item is a directory.
A trailing backslash can only designate a directory and NOT a file - just is not legal.
Absence of a trailing backslash means nothing at all.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to check if parameter is file (or directory)?

#8 Post by aGerman » 22 Nov 2011 13:05

pstein wrote:Lets say I want to check if a parameter is a file.

In this case you could also work with "%~1".


pstein wrote:If it is a folder or does not exist it should go to the else branch

Call this code with a path as (first) parameter or drag/drop a file or folder on it.

Code: Select all

@echo off &setlocal

set $isfile=1&pushd "%~1" 2>nul&&(popd&set $isfile=)||(if not exist "%~1" set $isfile=)

if defined $isfile (
  echo filebranch
) else (
  echo otherbranch
)

pause

Regards
aGerman

OJBakker
Expert
Posts: 90
Joined: 12 Aug 2011 13:57

Re: How to check if parameter is file (or directory)?

#9 Post by OJBakker » 22 Nov 2011 15:16

A similar solution using only the file-attributes.

Code: Select all

@echo off&setlocal enableextensions
set attr=%~a1&set otype=?
if defined attr if "%attr:~0,1%" == "d" (set otype=D) else (set otype=F)
echo otype=%otype%

Post Reply