How do I save a file to a name that doesnt exist in the folder?

1. Save a file to a folder.
2. Check to see if the file exists.
3. If it does, append a sequential number to the file name.
4. Check to see if that file exists.
5. If it does, increment the sequential number to the file name.
Loop this process until a file name does not exist in the folder.


contents of c:\temp  -->  test.txt    test1.txt    test2.txt

save test.txt in c:\temp

   does test.txt exist?  Yes, rename to test1.txt
   does test1.txt exist?  Yes, rename to test2.txt
   does test2.txt exist? Yes, rename to test3.txt
   does test3.tst exist? no, save as test3.txt

Who is Participating?
sirbountyConnect With a Mentor Commented:
Just need to check for test.txt first...
Try this...(assuming you've got your data you want to save in the file...I used strData of "My Text")

fname = Split("test.txt", ".")
strData = "My Text"
ext = "." & fname(1)
fname = fname(0)
strName = fname
If Dir("C:\temp\" & fname & ext) = fname & ext Then
    x = 1
    Do While Dir("C:\temp\" & fname & x & ext) = fname & x & ext
        x = x + 1
    Open ("C:\temp\" & fname & x & ext) For Output As #1
        Write #1, strData
    Close #1
    Open ("C:\temp\" & fname & ext) For Output As #1
        Write #1, strData
    Close #1
End If
TimCotteeHead of Software ServicesCommented:
Hi seckel,

This logic will never end, as you rename test.txt to test1.txt it will be there so will be renamed again as test2.txt and so on until you reach the largest number you can.

Are you really looking for a way to save a file with the most recent sequential number?

Tim Cottee
Brainbench MVP for Visual Basic
Dim MyFile as String
Dim TargetFile As String
Dim i as Integer
MyFile = Dir("C\:Test.txt")
Do While i<=3
If MyFile<>"" Then
   Name MyFile As TargetFile
     Msgbox "File not found
     Exit Do
End If
MyFile =Dir(TargetFile)
TargetFile = "C:\Test" & i & ".txt"
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

seckelAuthor Commented:

Good attempt, but all this did was overwrite test.txt with "My Text".

I need a to save a file but not overwrite existing files.  If the file name exists, I want to save the file with a numeric extenstion.

Are you certain?
I ran it here with no problem...
The code states that if a Dir of C:\temp\test.txt returns successfully, then begin the procedure to append a number to it, so I don't see how this wouldn't have worked...
Can you try stepping through the code and determine if that first If statement suceeds or not...
seckelAuthor Commented:
My appologies.  I had "Test.txt"  when you had "test.txt".  It works.  Thank you much.  Thats the logic I was looking for...
LOL - I had the same problem initially.
You may want to use
to avoid this problem...

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.