(Basic Question) DIR Not Working

DOS specific questions.
Post Reply
Username
Posts: 30
Joined: Jun 22, 2020 5:56

(Basic Question) DIR Not Working

Post by Username »

In FreeBASIC, when I use this code:

Code: Select all

Dir("C:\prmpt\*")
For tt As UInteger = 0 To 10
	?Dir() + " (File)"
Next
It doesn't work (C:\prmpt\ is the directory for it, in FreeDOS).

I have a directory listing routine that lists the subdirectories, but it won't list files.
Any advice?
Thanks
Laaca
Posts: 27
Joined: Dec 31, 2007 14:24

Re: (Basic Question) DIR Not Working

Post by Laaca »

Maybe helps this?: Dir("C:\prmpt\*.*")
fxm
Moderator
Posts: 12193
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: (Basic Question) DIR Not Working

Post by fxm »

With your code above you are not printing the first item found in 'C:\prmpt\'.
I added a print for the first one too:

Code: Select all

?Dir("C:\prmpt\*")
For tt As UInteger = 0 To 10
	?Dir() + " (File)"
Next
GeoffB17
Posts: 9
Joined: Feb 02, 2023 18:33
Location: Guisborough, UK
Contact:

Re: (Basic Question) DIR Not Working

Post by GeoffB17 »

I'm not quite clear exactly what you're trying to do here.

If you use Google and search re 'FreeBasic DIR()' you will get a a very detailed document regarding the DIR() command, it's various options, and examples. This includes reference to using attributes as part of the command's options, which will differentiate between displaying normal files, and directories. You are not using these at present, so it's not doing what you expect. The basic code you show above is heading in the right direction, but is not complete as per the examples.

Following the doc referred to should be a great help to you?

Geoff
GeoffB17
Posts: 9
Joined: Feb 02, 2023 18:33
Location: Guisborough, UK
Contact:

Re: (Basic Question) DIR Not Working

Post by GeoffB17 »

Further to the above, this page is the same as you'd get if you go to the Wiki, and click on the DIR command.

There is another way at getting a similar result, there may be advantages/disadvantages between the two methods depending on just what you want to achieve.

Within the example progs within the standard installation, there's a small prog pipe.bas which invokes the standard dos 'DIR' command, I would expect allowing all the usual DIR options (selection, sort, format of display, etc). The output of DIR, via a pipe, is then fed back to your prog line by line so that you can display the result, or modify/select the detail as your require, and either use the data within your prog, or display it just like the original DIR listing.

Geoff
GeoffB17
Posts: 9
Joined: Feb 02, 2023 18:33
Location: Guisborough, UK
Contact:

Re: (Basic Question) DIR Not Working

Post by GeoffB17 »

I was interested myself!

I just tried the pipe.bas variant. This is in examples\files\ dir.

I merely altered the initial definition from 'dir *.*' to 'dir *.bas /o-d', where the /o-d alters the order of the listing to date order (descending), i.e. newest first.

Worked perfectly?

I say ? as:

1. there was a delay on running the compiled console prog while the dir listing was sorted, an extra second or two,

2. the standard dir listing contains headings, and totals at the end, and you might not want this, but easy enough to get rid of these lines as each line is printed as there is one or more leading space while the actual dir lined have no leading space.

Geoff
GeoffB17
Posts: 9
Joined: Feb 02, 2023 18:33
Location: Guisborough, UK
Contact:

Re: (Basic Question) DIR Not Working

Post by GeoffB17 »

This seems to work, as in, it does what I think you're trying to do.

Code: Select all

rem TEST_DIR.BAS

#include once "dir.bi"

dim inpline as string

inpline = dir("d:\prog\fb2\*.*", fbDirectory)

for tt as integer = 1 to 3
    if(left(inpline, 1) <> ".") then
       print left(inpline + space(30), 30) + "  (dir)"
    end if
    inpline = dir("", fbDirectory)
next

inpline = dir("d:\prog\fb2\*.*", fbNormal)

for tt as uinteger = 1 to 10
    print left(inpline + space(30), 30) + "  (file)"
    inpline = dir("", fbNormal)
next

Geoff
GeoffB17
Posts: 9
Joined: Feb 02, 2023 18:33
Location: Guisborough, UK
Contact:

Re: (Basic Question) DIR Not Working

Post by GeoffB17 »

I did the above just out of interest, and to just get it working. It's not supposed to exhibit any clever programming. Feel free to 'fix' or 'improve' it any way you like.

NB one thing. The order of the files in the dir used seems to be affected by the dir setting. There was initially a problem with the order, so I ran 'dir /o' from the command prompt and the order changed. Maybe some similarity with the way the 'pipe.bas' prog works? Watch out for that.

I'm using an XP machine, doing things from a command prompt (full screen). Your machine may work differently!!

No, I don't use any IDE. I *_HATE_* IDEs! Simple text editor, and batch files, work great for me.

The DIR() command provides access to the filename ONLY. If this is ALL you need, then OK. One (possible) benefit of the 'pipe' method is that you have access to the complete data provided by the dos dir command, i.e. the date/time stamp and the filesize. Although NOT the file attribute?

Geoff
Post Reply