View previous topic :: View next topic |
Author |
Message |
njepsen
Joined: 13 Aug 2007 Posts: 469
|
Posted: Fri Feb 21, 2014 4:23 am Post subject: dir() and dir(*.*) again |
|
|
This is a follow-on from my previous post. I have still not resolved that issue, so decided to post some code.
Here is a loop, which does a dir(*.*) of a folder on an SD card, and reads a file to a modem for upload to a server. If the upload is successful, the loop then does a dir() to get the next file in the folder and do another upload etc etc .This works fine. If there is a problem with uploading tot he server, an error file is created which is saved to the SD card. ( this is nooped out in the listing below).
Code: |
Chdir "\" 'root
Chdir Cachefoldernamestr
Myfilename = Dir( "*.*") 'get first file in sub
Do
Print #1 , ">> myfilename = " ; Myfilename ; "length=" ; len(myfilename)
If Myfilename = "" Then
Print #1 , ">> no files left to send"
Exit Do 'exit when no files left to send
End If 'This is the normal exit
call Tcp_connect(main_server , 80 , 3) 'www.name, port,retries
If Msg_id <> 0 Then Exit Do 'give up. Not able to connect
Gosub Prepare_to_post
Gosub Read_83file_to_telitmodem
Print #1 , ">> waiting for gudupld" 'gudupld comes from the server
Call Waitfortelitresponse_sec( "GUDUPLD" , 30) 'gudupld from server arrives here
If Msg_id = 0 Then 'good response
Chdir "\"
Chdir Cachefoldernamestr
Kill Myfilename 'delete from sd card
Print #1 , ""
Print #1 , ">> Deleting " ; Myfilename
Else
Print #1 , ">> incorrect or no response from server"
error_msg = error_msg + "incorr or no resp frm srver" + Datetimestr + chr(13) + chr(10)
End If
Chdir "\"
Chdir Cachefoldernamestr
Myfilename = Dir() 'get the next file in the folder
If Myfilename = "" Then
Print #1 , ">> No files left to send"
if Msg_id = 0 then gosub setmyrtc
Exit Do 'exit when no files left to send
End If
Print #1 , ">> Closing Socket1. wait 30 sec..."
'call write_to_errorlog(Error_msg , "errors.txt") 'copy to error_log & clr msg
Call Timedelay_ms(30000)
loop 'get next file
|
This code runs OK, and if I set the server to bounce the uploads, the correct response "gudupld" is not sent and the loop continues to loop through all the files, trying them one at a time.
Here is the problem: If I add in the commented line call write_to_errorlog.... then the error file gets written to the SD card OK, BUT next time I try to upload a file to the server, the file length is shown as zero, and the upload fails.
Here is the subroutine that saves (appends) the error file. Code: |
'***********************************************************************
sub Write_to_errorlog(errtext , errorlogmyfilename)
'--------------------------------------------------------------------------------------
'Appends current error to SD card file and creates file if file not exist.
'Datetime in datetimestr
'error is in errtxt
'--------------------------------------------------------------------------------------
gosub sd_error_check 'do driveinit and initfile if reqd
Disable Interrupts
Chdir "\" 'to root
Mkdir "Errorlog" 'mk dir if not exist
Chdir "\"
Chdir "Errorlog"
Open errorlogfilename For Binary As #128
Filelength = Lof(#128)
Filelength = Filelength + 1 'find where to append
Seek #128 , Filelength 'set pointer
Tempb = Len(errtext)
Put #128 , errtext , , Tempb
Cr = 13 : Lf = 10
Put #128 , Cr , , 1 'CRLF between records
Put #128 , Lf , , 1
Flush #128
Close #128
Print #1 , ">> Entry saved to errorlog "
Print #1 , ""
Chdir "\" 'back to root dir
errtext = "" 'erase global (byref used)
Enable Interrupts
end sub
'*********************************************************************** |
For some reason, this sub screws up reading the next file on the do...loop, and I have not been able to find out why. Its not the seek, or any of the PUTs.
Edit: several hours later.
What I have found is that the simple act of changing directories in the called subroutine write_to_errorlog() is what causes the issue. Havent been able to determine why, or fix it yet. _________________ Neil |
|
Back to top |
|
|
Paulvk
Joined: 28 Jul 2006 Posts: 1257 Location: SYDNEY
|
Posted: Fri Feb 21, 2014 9:21 am Post subject: |
|
|
Hello Neil
Try saving all the registers in the sub as they may be being altered in it
Regards Paul |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Fri Feb 21, 2014 10:19 am Post subject: |
|
|
dir(mask) and the following dir() work on the current folder.
when you change folder using chdir , the next dir() will not be valid anymore. that is how i understand how it works. but maybe Josef can comment on it. _________________ Mark |
|
Back to top |
|
|
njepsen
Joined: 13 Aug 2007 Posts: 469
|
Posted: Fri Feb 21, 2014 9:03 pm Post subject: |
|
|
Even if you go back to the original folder before doing the dir() ?
It certainly looks as though you are right. _________________ Neil |
|
Back to top |
|
|
njepsen
Joined: 13 Aug 2007 Posts: 469
|
Posted: Tue Mar 29, 2022 2:33 am Post subject: |
|
|
March 2022 Update on dir (*.*"), dir()
It looks like if you do a dir("*.*") whilst in a directory "eg num", then you call a subroutine or function that takes you out of that directory, then later on, go back into the same directory (i.e back into "num"), then dir() does NOT get you the next file in "num", and in fact dir() will now not see any files. _________________ Neil |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Tue Mar 29, 2022 9:34 am Post subject: |
|
|
yes, the dir() is just to find if a file exists or to list all files.
i added this to the help :
You should not use directory/file manipulation functions between DIR(mask) and DIR(). This because the first use of DIR() with a mask will search the FAT and will set up a pointer to this table. The next use of DIR() will continue to search the next match. But when you change the directory, or create a file or directory the pointer will be lost. _________________ Mark |
|
Back to top |
|
|
njepsen
Joined: 13 Aug 2007 Posts: 469
|
Posted: Tue Mar 29, 2022 9:17 pm Post subject: |
|
|
Thanks for that Mark. It certainly explains the issue I was having. My Code looked a bit like like this:
Code: |
do
chdir"\"
chdir"cache"
myfilename = dir("*.*")
'do some things and check for errors
'if err detected - save err msg to SD card in a different directory
'then go back to the "cache" directory
chdir"\"
chdir"cache"
myfilename = dir() '<<<<<<<<<< This line here did not find remaining files
if len(filename = 0 then
'there re no more files, so exit the loop
exit do
end if
loop
|
Taking out the error save step ( which involved a dir change) fixed the problem, but it took me a while
to discover why.
Your comment " Do not do any directory manipulation between dir(*.*") and dir()" is the answer _________________ Neil |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Thu Mar 31, 2022 8:59 am Post subject: |
|
|
i did not know it either. i had to check the code in the lib to see how it worked.
but it does make sense. _________________ Mark |
|
Back to top |
|
|
|
|
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
|
|