1

I'm trying to convert a program from Linux to use on Windows, and it calls test -f, or test -d on Linux. I need it to do the same thing on Windows. Is there a built-in command, or another program I can use to do the same thing?

I'm programming using FreeBASIC (horrible, but it's what I got).

EDIT: An external program is the best option here. I've looked at the API, and it's not good.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
bradlis7
  • 3,375
  • 3
  • 26
  • 34
  • Why is this tagged `batch`? Do you want a batch file solution or a FreeBASIC one? – Joey Jun 18 '10 at 21:36
  • Yeah, batch, like `test -f` on linux. Just edited it to reflect that. – bradlis7 Jun 18 '10 at 21:45
  • add some quick info to my answer about how you might be able to sort out your own tool (even if you don't know C++ you might be able to cut out the crust from that sample and then ask other questions here to fix any remaining problems). – Hans Olsson Jun 18 '10 at 22:00

4 Answers4

2

Never heard of FreeBasic before but looking at the help there's a DIR command that supports using fbDirectory as one of the attribute patterns to filter for.
And looking slightly further down on that page I just saw that they have a sample for checking if the objects found are files or directories. Just look here, look at the second example on that page.

Not sure what exactly those test commands do, but if you want to test if a specific object is a directory you should be able to call Dir("exactname", fbDirectory, something) I'd thought. And then you could test for a file by putting a Not in somewhere (assuming that FreeBasic supports that).

Edit: To make your own tool, here's a sample that shows a tiny C++ app that could easily be changed to look for directories or not. Look for the FindFirstFile in the sample and shortly after that it checks if it's readonly, which could be changed for FILE_ATTRIBUTE_DIRECTORY.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • Yeah, that would be good if it worked 100%. I read somewhere that Dir() is buggy, which is not documented. I will give it a shot. – bradlis7 Jun 18 '10 at 21:44
  • @bradlis7: If the language features are buggy, you might be better of following Ed's suggestion :). However, if you can execute external executables and get a return value it would be easy to write a tiny exe in C++ or something to look for this (you could probably find some sample code for this easily) and thne just create the tool yourself with some free C++ compiler. – Hans Olsson Jun 18 '10 at 21:52
  • Well, I ended up using Dir() and fbDirectory. It seems to work. I'm stuck with freebasic, unfortunately... – bradlis7 Jun 21 '10 at 21:48
2

http://unxutils.sourceforge.net/

you can use test.exe just like under linux

npocmaka
  • 55,367
  • 18
  • 148
  • 187
1

Not sure about FreeBASIC, have you looked into vbscript? You could use the FileSystemObject

Dim fso, msg
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FolderExists(fldr)) Then
   'Do Something here
Else
   'Do Something
End If
If (fso.FileExists(filespec)) Then
   'Do Something here
Else
   'Do Something
End If
EDJ
  • 475
  • 1
  • 7
  • 16
  • If I had not created 2,000 lines of code for Linux, I would be ok with doing it. An external program would work: `exec("isfile filename")`. – bradlis7 Jun 18 '10 at 21:42
0

You can also use Kiwi for FreeBasic in order to check if a path leads to a file or directory. You can find Kiwi on Github (https://github.com/nsiatras/kiwi)

#include once "kiwi\kiwi.bi"
#include once "kiwi\io.bi"

' Declare a new file
Dim myFile as File = File("C:\Users\nsiat\Desktop\Test.txt")

' Check if file exists, is a File or is a Directory
print "File exists: " & myFile.exists()
print "Is file: " & myFile.isFile()
print "Is Directory: " & myFile.isDirectory()
Nikos
  • 67
  • 5