Question

I like to scan a set of files using cygwin's "file" command. I like to invoke this command from within my own program.

The program is written in REALbasic (RB), which is similar to Visual Basic (e.g. has similar ways to interface DLL APIs with the "declare" statement).

RB comes with a Shell class that lets me invoke tools of the "DOS" command type. I understand that I could use that to invoke a cygwin shell (e.g. sh or bash, as suggested here), which I then tell to invoke the "file" command. This seems rather inefficient to me as it would load the bash shell every time I want to invoke its file tool.

However, I wonder if there's a dll that comes with a cygwin installation that provides a direct access to its command shell. By this I hope to speed things up as I could hopefully load its shell once, then just invoke the file cmd as needed.

Maybe there's even a VB class that already does this, which I could then port to RB? I could not find anything like that yet, though.

Was it helpful?

Solution

Turns out this is pretty easy:

Simply invoke the tool with its full path, e.g. "C:\cygwin\bin\file.exe".

I was misled by so many googled suggestions that suggest that I need to invoke the shell in order to use the file command that I didn't even try this direct approach first.

The only (small) challenge is to pass any paths to the tool in POSIX format. REALbasic only gives me the DOS-style path format (e.g. from FolderItem.AbsolutePath), and the cygwin tools usually complain about this with a warning. I found to solutions to this:

Either disable the warning in the cygwin installation. That's done with setting the environment variable option "nodosfilewarning".

The other way is to provide a proper POSIX path. The cygwin1.dll provides a function for this (''cygwin_conv_path''), but I could not get this working in RB. Instead, I wrote my own, hoping it covers all cases:

dim objPath as String = f.AbsolutePath
if objPath.Mid(2,2) = ":\" then
  objPath = "/cygdrive/"+objPath.Mid(1,1).Lowercase+objPath.Mid(3).ReplaceAll("\","/")
end if
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top