Solved

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

Posted on 2004-04-15
7
376 Views
Last Modified: 2006-11-17
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.

Example:

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



0
Comment
Question by:seckel
7 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 10832201
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
http://www.brainbench.com
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 10832336
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
    Loop
    Open ("C:\temp\" & fname & x & ext) For Output As #1
        Write #1, strData
    Close #1
Else
    Open ("C:\temp\" & fname & ext) For Output As #1
        Write #1, strData
    Close #1
End If
0
 
LVL 5

Expert Comment

by:Pi7
ID: 10832351
Dim MyFile as String
Dim TargetFile As String
Dim i as Integer
MyFile = Dir("C\:Test.txt")
TargetFile="C:\Test1.txt"
i=1
Do While i<=3
If MyFile<>"" Then
   Name MyFile As TargetFile
Else
     Msgbox "File not found
     Exit Do
End If
i=i+1
MyFile =Dir(TargetFile)
TargetFile = "C:\Test" & i & ".txt"
Loop
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:seckel
ID: 10832448
sirbounty

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.

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 10832470
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...
0
 

Author Comment

by:seckel
ID: 10832562
My appologies.  I had "Test.txt"  when you had "test.txt".  It works.  Thank you much.  Thats the logic I was looking for...
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 10832599
LOL - I had the same problem initially.
You may want to use
UCASE
to avoid this problem...

Thanx.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access 2016 VB code 9 116
VB6 - Convert HH:MM into Decimal 8 56
Advice in Xamarin 21 80
Need Nag Screen for Reboot if system up time is over 14 days 12 51
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question