0

I'm using tshell to connect to a device and I need to check if a given path is a file or directory.

I was hoping to use the cmd-device function which runs cmd commands on the device, but there doesn't seem to be a cmd command to do this.

Does someone have a way to check whether a given path is to a directory or to a file using the standard cmd functions?

2 Answers2

1

Since you have PowerShell tagged in your question, one option would be to check the object type returned by the Get-Item cmdlet.

(Get-Item C:\Windows) -is [System.IO.DirectoryInfo]

# Shorter version
(gi C:\Windows) -is [IO.DirectoryInfo]
Rynant
  • 23,153
  • 5
  • 57
  • 71
  • That works for the computer side, but unfortunately I can't actually run the powershell on the device as far as I know. I think I have to do this using cmd.exe commands – user3700394 Jun 02 '14 at 21:59
  • @user3700394 I didn't think you would have to run PowerShell on the device. Can you run the command on the PC and change the path to point to one that exists on the device? – Rynant Jun 03 '14 at 12:43
1

The usual test for a folder existence is

if exist "x:\somewhere\" echo FOLDER FOUND

but, in some cases (i know the case of novell netware redirector) the previous condition will always evaluate to true, both when "x:\somewhere" is a file or when it is a folder

One alternative can be

@echo off
    setlocal enableextensions

    set "target=c:\windows"

    set "what=NOT EXIST"
    for %%a in ("%target%") do for /f "delims=r-" %%b in ("%%~aa%%~za"
    ) do if "%%b"=="d" ( set "what=FOLDER" ) else ( set "what=FILE" )

    echo %what%

    endlocal

Check for the presence of an initial d in the list of attributes of the file/folder.

Each of the attributes in the list can be a letter (depending on the attribute) or a dash.

The two first are d for directory and r for readonly. So, second for uses r- as delimiters to separate the possible d from the rest of attributes.

If the file does not have any attributes set, the tokenizer in for command will eliminate all the dashes, non assigning data to the replaceable parameter and in consecuence not executing the code inside the do clause. To avoid it, %%~za (the size of the element) is appended to the string, so, there will always be data to be processed if the file/folder exists. If it does not exist, the expresion %%~aa%%~za is evaluated to a empty string and the code in the do clause will not execute.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • `if exist "<<..>>\"` doesn't work in some cases, that may be somehow related to { Symbolic Links / NTFS Reparse Points } – irvnriir Apr 27 '21 at 12:23