How to check if parameter is file (or directory)?
Moderator: DosItHelp
How to check if parameter is file (or directory)?
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
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)?
I'm not expert .
I see such one bat:
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)?
'
Hi pstein
You can use for /?
This will do verification without existence
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"
Re: How to check if parameter is file (or directory)?
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.
This should work on Win7 perhaps not on XP. I remember I had problems on XP to catch the errorlevel of pushd.
Regards
aGerman
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)?
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
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)?
@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
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)?
At a DOS prompt I have just issued 5 commands
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.
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)?
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)?
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%