Test if parameter is file or directory

January 31, 2010 at 07:49:51
Specs: Windows 7
trying to make a batch file to put in sendto

if it's run on a file, I want it to run:

app.exe %1

if it's a directory, I want it to run:

for /d /r %%a in (%1) do app.exe %%a


See More: Test if parameter is file or directory

Report •


#1
January 31, 2010 at 09:18:23
Perhaps something like this(untested):

rem backslash to check if it is a dir
if exist "%~1\" (
    for /d /r %%a in ("%~1") do app.exe "%%~a"
) else (
    app.exe "%~1"
)


Batch Variable how to


Report •

#2
January 31, 2010 at 09:28:25
The following script relays upon the fact that the returned size of a folder is always zero. However if the parameter is a file with lenght 0 it will be recognized as a directory.

Maybe some guy/gale may offer a better solution.

@echo off
for %%j in ("%*") do if "%%~zj"=="0" (
  for /D /R %%a in ("%*") do app.exe %%a
) else (
  app.exe %*
)


Report •

#3
January 31, 2010 at 11:31:22
for /f %%a in (%1) do set test=%%~aa
set test=%test:~0,1%
if /i %test% equ d (
for /d /r %%a in (%1) do app.exe %%a
) else (
app.exe %1
)

Report •

Related Solutions

#4
February 21, 2010 at 20:29:17
thanks all seem to work with the exception when there's a file and directory named the same thing, but i doubt there's anything that can be done about that

Report •

#5
February 21, 2010 at 22:58:49
nah! yet another version...

@echo off & setlocal
set target=%1
pushd %target%
if %errorlevel% 1 (
echo nonexistant or not a directory
) else (
echo scoping out directory %cd%
)
popd
echo ok, back home! %cd%

i think i see your dilemma, but not sure.
Obv. you can't have file and subdir with same name in any given dir. so you must be working the whole drive? or from cur.dir down... then it has to be put up for a choice
(by the sender-to-er)


Report •

#6
February 22, 2010 at 04:17:06
@tnntp25: "when there's a file and directory named the same thing"

Is that even possible?


Report •

#7
February 22, 2010 at 04:25:53

Report •

Ask Question