Home > Archive > Visual Basic Syntax > January 2006 > file creation object
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
file creation object
|
|
|
| Hello,
I've create a control to generate a txt file in a loop.
Set A = fs.CreateTextFile("c:\skidlabel.txt", False).
Is there a way to increment the filename everytime it loops, don't want to
override the file, just generate a different filename everytime it loops.
Creating this in Access 2003 using code builder in VB.
Vincent
| |
| Ken Halter 2006-01-25, 7:20 pm |
| "Vince" <Vince@discussions.microsoft.com> wrote in message
news:625475FA-DBFE-437B-8B8B-863CD43A4CA7@microsoft.com...
> Hello,
>
> I've create a control to generate a txt file in a loop.
>
> Set A = fs.CreateTextFile("c:\skidlabel.txt", False).
>
> Is there a way to increment the filename everytime it loops, don't want to
> override the file, just generate a different filename everytime it loops.
>
> Creating this in Access 2003 using code builder in VB.
>
> Vincent
I never use the FSO (it's for scripting) but this should work.
It'll just show the command you'd use. It won't actually create anything.
'========
Private Sub Command1_Click()
Dim sBaseName As String
Dim sActualFileName As String
Dim iCounter As Integer
sBaseName = "c:\skidlabel"
For iCounter = 1 To 9
sActualFileName = sBaseName & iCounter & ".txt"
Debug.Print "Set A = fs.CreateTextFile('" _
& sActualFileName & "', False)"
Next
End Sub
'========
--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Freeware 4 color Gradient Frame? http://www.vbsight.com/GradFrameCTL.htm
| |
|
| Ken,
Thanks but it doesn't work in my script. do you have anything on generating
random filename ?
Vincent
"Ken Halter" wrote:
> "Vince" <Vince@discussions.microsoft.com> wrote in message
> news:625475FA-DBFE-437B-8B8B-863CD43A4CA7@microsoft.com...
>
> I never use the FSO (it's for scripting) but this should work.
> It'll just show the command you'd use. It won't actually create anything.
> '========
> Private Sub Command1_Click()
> Dim sBaseName As String
> Dim sActualFileName As String
> Dim iCounter As Integer
>
> sBaseName = "c:\skidlabel"
>
> For iCounter = 1 To 9
> sActualFileName = sBaseName & iCounter & ".txt"
>
> Debug.Print "Set A = fs.CreateTextFile('" _
> & sActualFileName & "', False)"
>
> Next
>
> End Sub
> '========
>
> --
> Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
> DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
> Freeware 4 color Gradient Frame? http://www.vbsight.com/GradFrameCTL.htm
>
>
>
| |
| Ken Halter 2006-01-26, 7:07 pm |
| "Vince" <Vince@discussions.microsoft.com> wrote in message
news:54E7BB8A-234F-47C0-8D35-5B2A35F57411@microsoft.com...
> Ken,
>
> Thanks but it doesn't work in my script. do you have anything on
> generating
> random filename ?
>
> Vincent
Not random (random values aren't guaranteed unique).... but Unique... click
Command1 to see a filename unique to any folder you pass (there are hundreds
of ways to do this)
'=============
Option Explicit
Private Sub Command1_Click()
'Path must include trailing backslash
Debug.Print UniqueName("C:\Temp\", "txt")
End Sub
Private Function UniqueName(Path As String, Ext As String) As String
Dim sRet As String
Dim iChr1 As Integer
Dim iChr2 As Integer
Dim dt As Date
iChr1 = 65 ' "A"
iChr2 = 65 ' "A"
dt = Now
Do
sRet = Path & Chr$(iChr1) & Chr$(iChr2) & CDbl(dt) & "." & Ext
If FileExists(sRet) Then
iChr2 = iChr2 + 1
If iChr2 > 90 Then
iChr2 = 65
iChr1 = iChr1 + 1
If iChr1 > 90 Then
MsgBox "Can't continue"
Exit Do
End If
End If
Else
'This is a unique name, bail
Exit Do
End If
Loop
UniqueName = sRet
End Function
Private Function FileExists(FileName As String) As Boolean
On Error Resume Next
If Len(FileName) > 0 Then
FileExists = ((GetAttr(FileName) And vbDirectory) = 0)
Err.Clear
End If
End Function
'=============
--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Freeware 4 color Gradient Frame? http://www.vbsight.com/GradFrameCTL.htm
|
|
|
|
|