Page 1 of 1
How to check if parameter is file (or directory)?
Posted: 12 Nov 2011 02:15
by pstein
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
Re: How to check if parameter is file (or directory)?
Posted: 12 Nov 2011 05:53
by leescott
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
Re: How to check if parameter is file (or directory)?
Posted: 12 Nov 2011 06:10
by Ed Dyreen
'
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
Re: How to check if parameter is file (or directory)?
Posted: 12 Nov 2011 07:03
by aGerman
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
Re: How to check if parameter is file (or directory)?
Posted: 12 Nov 2011 10:29
by dbenham
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
Re: How to check if parameter is file (or directory)?
Posted: 22 Nov 2011 05:20
by pstein
@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
Re: How to check if parameter is file (or directory)?
Posted: 22 Nov 2011 06:40
by alan_b
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.
Re: How to check if parameter is file (or directory)?
Posted: 22 Nov 2011 13:05
by aGerman
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
Re: How to check if parameter is file (or directory)?
Posted: 22 Nov 2011 15:16
by OJBakker
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%