Android Question Incrementing csv file

Subatanx86

Member
Licensed User
I'm trying to auto increment csv files, but I'm not having any luck. I found this post https://www.b4x.com/android/forum/threads/b4x-automatically-increment-file-names.95196/ but I was unable to get it to work. I may just be missing something. This is the code I'm currently using to create the csv.
B4X:
Sub btnRCSV_Click
    
    Log("hit")
    Dim List As List
    Dim cursor As Cursor
    Dim cols(3) As String
'   
    List.Initialize
    Log("init")
    cursor=SQL.ExecQuery("Select * From Receiving")
    Log("query")
    For i = 0 To cursor.RowCount - 1
        Log("cycle " & i)
        cursor.Position = i
        For z = 0 To 2
            Log(i & " - " & cursor.GetString2(z))
        Next
        cols = Array As String(cursor.GetString2(0),cursor.GetString2(1),cursor.GetString2(2))
        List.Add(cols)
    Next
    Log("done cycle")
    su.SaveCSV2(File.DirRootExternal, "ReceivingEdge.csv", ",", List, Array("Tracking", "DateTime", "Location"))
    Log("save")
    cursor.Close
    Log("close")
      
    Msgbox("CSV file saved","CSV")


    
End Sub
 

josejad

Expert
Licensed User
Longtime User
If I've understood, you're trying to increment your csv file name. But I can't see where you'r etrying to increment it. You use a fixed name (ReceivingEdge.csv).

Where are you using the code you've found in that link? You should use something like
B4X:
su.SaveCSV2(File.DirRootExternal, lastfilename, ",", List, Array("Tracking", "DateTime", "Location"))

or

su.SaveCSV2(File.DirRootExternal, newname, ",", List, Array("Tracking", "DateTime", "Location"))
 
Last edited:
Upvote 0

Subatanx86

Member
Licensed User
If I've understood, you're trying to increment your csv file name. But I can't see where your trying to increment it. You use a fixed name (ReceivingEdge.csv).

Where are you using the code you've found in that link? You should use something like
B4X:
su.SaveCSV2(File.DirRootExternal, lastfilename, ",", List, Array("Tracking", "DateTime", "Location"))

or

su.SaveCSV2(File.DirRootExternal, newname, ",", List, Array("Tracking", "DateTime", "Location"))

I'm trying to figure out how I have to change my existing code while also adding the subs in the example link. I will try your suggestion.
 
Upvote 0

emexes

Expert
Licensed User
Some random things to consider:

- is the numbering just to ensure unique filenames? does each file have an associated docket/invoice/purchase number? can you use that?

- can you use the date and time as part of filename, eg filename could be docket-yyyymmdd-hh-nnn.csv where nnn is a sequence number of dockets dockets for the hour? or docket-yyyymmdd-nnn.csv where nnn is a sequence number of dockets for the day?

- using the date as part of the filename means that you can easily archive old data to keep disk space usage under control, eg, to compress all of last month's docket files into a single Zip file:
PKZIP -ADD - MOVE docketarchive-201909.zip docket-201909*.csv

- are the files to be called eg docket8.csv, docket9.csv, docket10.csv, docket11.csv, or perhaps docket008.csv, docket009.csv, docket010.csv, docket011.csv
the advantage of using a fixed number of digits is that file listings sorted by name will also be sorted by number
the disadvantage is that you are limited to eg, 1000 3-digit numeric combinations

- how to find the next sequence number to use

1/ brute-force: try all numbers 1, 2, 3... until find a number not yet used
B4X:
Dim FileNameNumber As Int = 0
Dim FileName As String
Do
    FileNameNumber = FileNameNumber + 1
    FileName = "docket-" & FileNameNumber & ".csv"
Loop Until DIR$(FileName) = ""     'check file does not already exist - function probably called File.Exist() or similar

2/ brute-force via file listing
B4X:
get list of files in folder
MaxUsedFileNumber = 0
go through list of files, find highest number already used
FileName = "docket-" & (MaxUsedFileNumber + 1) & ".csv"

3/ record last used file number in a separate file:
B4X:
'at program start:
FileNameNumber = 0
If "lastdocketnumber.csv" file exists then
    read FileNameNumber from file "lastdocketnumber.csv"
End If

'when need next number
'use code from 1/ to get next available file number

'at program end or activity pause or similar
write FileNameNumber to file "lastdocketnumber.csv"
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Probably the simplest way to get it working right now is a function that returns the next available filename, eg:
B4X:
'Before = start of file name, before the number, eg "invoice-"
'After = end of file name (usually the file type), after the number, eg ".csv"
Sub NextReceivingFileName(DirName As String, Before As String, After As String) As String,

    Dim FileNameNumber As Int = 0
    Dim FileName As String
    Do
        FileNameNumber = FileNameNumber + 1
        FileName = Before & FileNameNumber & After
    Loop Until File.Exists(DirName, FileName) = False

    Return FileName

End Sub
then call with eg:
B4X:
Dim ReceivingFileName As String = NextReceivingFileName(File.DirRootExternal, "ReceivingEdge-", ".csv")
'returns eg: "ReceivingEdge-123.csv"

su.SaveCSV2(File.DirRootExternal, ReceivingFileName, ",", List, Array("Tracking", "DateTime", "Location"))
 
Last edited:
Upvote 0
Top