Uses "FILE"
#PROFILE On
$tob32_charset = "0123456789ABCDEFGHJKMNPQRTUVWXYZ"
'0123456789ABCDEFGH JK MN PQR TUVWXYZ
%tob32_charsetptr = StrPtr($tob32_charset) As DWord
$testfilename = "C:\users\usr1\tst\ab.txt"
For i As Word=0 To 200
FILE_Append( uniquefilenamev1( $testfilename ) , "hello world" )
Next
MsgBox 0, "done"
Function uniquefilenamev1(unsafe As String) As String
If Not FILE_Exists(unsafe) Then Return unsafe
Return sequencefilenamev1( _
FILE_PathSplit(unsafe, %PATH_ROOTPATHFILE), _
"." & FILE_PathSplit(unsafe, %PATH_EXT), _
0)
End Function
Function uniquefilenamev2(unsafe As String) As String
If Not FILE_Exists(unsafe) Then Return unsafe
Return sequencefilenamev2( _
FILE_PathSplit(unsafe, %PATH_ROOTPATHFILE), _
"." & FILE_PathSplit(unsafe, %PATH_EXT), _
"0")
End Function
Function sequencefilenamev1(filename As String, dotext As String, seqnum As Number) As String
Dim retfilename As String = filename & "-" & tob32(seqnum) & dotext
If Not FILE_Exists(retfilename) Then Return retfilename
Return sequencefilenamev1(filename,dotext,seqnum+1)
End Function
Function sequencefilenamev2(filename As String, dotext As String, seqnum As String) As String
Dim retfilename As String = filename & "-" & seqnum & dotext
If Not FILE_Exists(retfilename) Then Return retfilename
Return sequencefilenamev2(filename,dotext,b32plusone(seqnum))
End Function
Function tob32(nu As Number) As String
Static n As DWord
Static res As String
res=""
Repeat
n=nu And 31
res= Memory_Get(%tob32_charsetptr+n,1) & res
nu=SHIFT RIGHT nu, 5
Until nu=0
Return res
End Function
Function b32plusone(thestring As String) As String
'returns a base 32 string with a value of one plus the given string's value
'the encoding it assumes is 0123456789ABCDEFGH JK MN PQR TUVWXYZ
'it is case sensitive
Static carry,tmp As Byte
Static i,j As DWord
i= Len(thestring)
j= StrPtr(thestring)-1
carry=0
tmp=Asc(thestring,i)
' HI,KL,NO,RS,9:,Z[
If(In(Poke(j+i,tmp+1),73,76,79,83,58,91)>0) Then _
If (In(Poke(j+i,tmp+2),59,92)>0) Then _
If Poke(j+i,tmp+8)<>65 Then _
carry=Poke(j+i,48)
For i =i-1 To 1 Step -1 While ((carry>0) And (Asc(thestring,i)=90))
Poke(j+i, 48)
Next
If (i<=0) Then
If (carry>0)Then Return "1" + thestring
Else
tmp=Asc(thestring,i)
If (carry>0) Then carry = 1
If(In(Poke(j+i,tmp+carry),73,76,79,83,58)>0) Then _
If Poke(j+i,tmp+2)=59 Then Poke(j+i,65)
End If
Return thestring
End Function
Bookmarks