JSI Tip 10229. How can a script determine if a file system object is a folder or a file?


I have scripted IsFolder.bat to determine if a file system object is a folder.

The syntax for using IsFolder.bat is:

\[CALL\] IsFolder <FSO> YNX

Where:

<FSO> is the fully qualifed folder or file name.

YNX   is a call directed environment variable that will contain:
           X - If the <FSO> does NOT exist.
           Y - If the <FSO> is a folder.
           N - If the <FSO> is NOT a folder.
IsFolder.bat contains:
@echo off
if \{%2\}==\{\} @echo Syntax: IsFolder Object YNX&goto :EOF
if not exist %1 set %2=X&goto :EOF
if exist "%TEMP%\IsFolder.vbs" goto doit
@echo.Set objArguments = Wscript.Arguments>"%TEMP%\IsFolder.vbs"
@echo.Obj = objArguments(0)>>"%TEMP%\IsFolder.vbs"
@echo.Wscript.Echo IsFolder(Obj)>>"%TEMP%\IsFolder.vbs"
@echo.Wscript.Quit>>"%TEMP%\IsFolder.vbs"
@echo.Function IsFolder(folder)>>"%TEMP%\IsFolder.vbs"
@echo.  IsFolder = "N">>"%TEMP%\IsFolder.vbs"
@echo.  Dim objFSO>>"%TEMP%\IsFolder.vbs"
@echo.  Set objFSO = CreateObject("Scripting.FileSystemObject")>>"%TEMP%\IsFolder.vbs"
@echo.  If objFSO.FolderExists(folder) Then IsFolder = "Y">>"%TEMP%\IsFolder.vbs"
@echo.  Set objFSO = Nothing>>"%TEMP%\IsFolder.vbs"
@echo.End Function>>"%TEMP%\IsFolder.vbs"
:doit
for /f "Tokens=1" %%a in ('cscript //nologo "%TEMP%\IsFolder.vbs" %1') do (
 set %2=%%a
)



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish