Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Dir(*.*) and Dir()

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> AVR-DOS
View previous topic :: View next topic  
Author Message
njepsen

Bascom Member



Joined: 13 Aug 2007
Posts: 403

newzealand.gif
PostPosted: Fri Jan 11, 2013 12:48 am    Post subject: Dir(*.*) and Dir() Reply with quote

The help says that Dir() will return an empty string when there are no more files to read. This is true BUT not in some circumstances.
I have written a little loop to do a directory of an SD card.
Code:

 Do
      Chdir "\"
      Filecount = 0
      Tempstr = ""
      Tempstr = Dir( "*.*")                                 'get first file name
      While Tempstr <> ""
         Incr Filecount
         Print #1 , ">> " ; Tempstr                         'list file
         Tempstr = Dir()
         Reset Watchdog
      Wend
         Print #1 , ">> " ; Filecount ; "files"
      Call Timedelay(1000)
      Print #1 , ""

    Loop


Which works fine, and the following printout appears on screen every second:
Code:


>> LOG2013.
>> CACHE.
>> ERRORLOG.
>> 3files

>> LOG2013.
>> CACHE.
>> ERRORLOG.
>> 3files

etc

 


However if I unplug the SD card while the loop is running, it continues to give me the SAME printout - ie it suggest it is still reading the SD card which course it is not. This tells me that dir() does not check if there is a directory present, and that the old values are not cleared before a new Dir() read is done ??I have no idea where the txt is coming from, because filecount and tempstr are cleared every time thru' the loop.

_________________
Neil
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5024
Location: Holland

blank.gif
PostPosted: Fri Jan 11, 2013 10:12 am    Post subject: Reply with quote

Maybe Josef can comment on it?
_________________
Mark
Back to top
View user's profile Visit poster's website
njepsen

Bascom Member



Joined: 13 Aug 2007
Posts: 403

newzealand.gif
PostPosted: Fri Jan 11, 2013 10:25 am    Post subject: Reply with quote

Thanks Mark. Look forward to it.

I have made a reply to 2 previous posts of mine, which 'd appreciate your views on:

PostPosted: Mon Dec 10, 2012 4:19 pm Post subject: Help Txt for config serialin confusing

PostPosted: Thu Dec 13, 2012 9:29 pm Post subject: IDE search panel bug

In the 2nd one I have included a (bad) U-tube clip showing the problem.

_________________
Neil
Back to top
View user's profile
oe9vfj

Moderator



Joined: 17 Jun 2004
Posts: 269
Location: Austria, Hard

austria.gif
PostPosted: Fri Jan 11, 2013 6:58 pm    Post subject: Reply with quote

Hi,

If AVR-DOS is working with a disk, it reads the according sector into the RAM and works with that copy in the RAM unless a function needs another sector. Than AVR-DOS writes the RAM-image of the sector to the disk back, if something was changed in this sector and reads the new sector into the RAM.
Each opened file has its own RAM-Area and also the FAT and Directory depending of the Parameter Csepfathandle.

One directory-sector can hold 16 Directory-Entries, so in your Situation AVR-DOS can load all information from the RAM-image of the directory-Sector and there is no reason to load the information from the card again. Only at the point, when AVR-DOS needs a new Sector from the card, it will detect, that no card is present.

IF you want to detect the removing of the card immediately, you have to do it by checking the card-present line if your sd-card slot provide it.

best regards
Josef

_________________
regards Josef

DOS - File System for BASCOM-AVR on http://members.aon.at/voegel
Back to top
View user's profile Visit poster's website
njepsen

Bascom Member



Joined: 13 Aug 2007
Posts: 403

newzealand.gif
PostPosted: Fri Jan 11, 2013 9:35 pm    Post subject: Reply with quote

Hi Joseph and thanks for your reply. I was aware of the fix, and I do already monitor the SD card socket switch. However i was more concerned with a faulty card. Currently i do a write to the card, and 15 minutes later the card info sent to a server. i wanted to make sure the dir data i was sending was the new data and not the old data, because it seems that if dir() can't read the card for some reason ( eg faulty) it presents the previous read as valid ? Is there any way i can clear the old dir() info and force a new read of the Sd card.
_________________
Neil
Back to top
View user's profile
oe9vfj

Moderator



Joined: 17 Jun 2004
Posts: 269
Location: Austria, Hard

austria.gif
PostPosted: Mon Jan 14, 2013 3:44 pm    Post subject: Reply with quote

Hi,

If you detect a change of the card, you should use DriveInit and InitFileSystem to restart AVR-DOS with the new card. Otherwith it could happen, that you corrupt the new Card, if you make write operations to the new card with card information of the old card.

best regards
Josef

_________________
regards Josef

DOS - File System for BASCOM-AVR on http://members.aon.at/voegel
Back to top
View user's profile Visit poster's website
njepsen

Bascom Member



Joined: 13 Aug 2007
Posts: 403

newzealand.gif
PostPosted: Thu Feb 20, 2014 4:06 am    Post subject: Reply with quote

A further point of clarification ( one year later).
The help says:
Quote:
The first function call needs a file mask. All other calls do not need the file mask. In fact when you want to get the next filename from the directory, you must not provide a mask after the first call.


Obviously "you must not provide a mask after the first call" must terminate at some stage. Is this true until the next SDinit or until the next chdir? In other words, after a dir(*.*), subsequently dir() will return the next file each time until all done. However if I change directories, do i do dir(*.*) or just dir() to get the next filename? The reason i ask this seemingly trivial question is that I have a loop like this:


Code:



chdir"\"
chdir somefilename

myfilename = dir(*.*)   ' get the first file
do
 dosomething
 dosomething
 dosomething

myfilename = dir()   'get the next file
if myfilename="" then exit do

loop


This code works fine, BUT if in the line "dosomething" I call another sub which involves some file handling, the dir() that follows gives unexpected results.

_________________
Neil
Back to top
View user's profile
njepsen

Bascom Member



Joined: 13 Aug 2007
Posts: 403

newzealand.gif
PostPosted: Thu Dec 15, 2016 2:30 am    Post subject: Reply with quote

if a directory does not yet exist and you change to that directory and call dir*.*,
the filename becomes ".", so any followng code may give an unexpected result.

In the code below, I am expecting a folder with the const name cachefoldernamestr, and if it is not empty
I connect to the server and POST the files.
However if for some reason, the directory doesnt yet exist ( a new SD card for example), then my
myfilename becomes ".", which means there is an unnecessary TCP connect, for a non existent file.


Code:

            Chdir "\"                                                        'root
            Chdir Cachefoldernamestr                                'my data
            Myfilename = Dir( "*.*")                                'get first file
            Print #1 , ">> myfilename = " ; Myfilename
               
               If Myfilename = "" Then
                    Print #1 , ">> no files left to send"
                    Exit Do                                       'exit when no files left to send
               else                                              

                   call tcp_connect(main_server , 80)             'connect to the server
                   etc
                    etc

               end if

 

_________________
Neil
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> AVR-DOS All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum