Link to home
Start Free TrialLog in
Avatar of isnoend2001
isnoend2001Flag for United States of America

asked on

Find and rename files

This question kinda goes hand in hand with question i asked yesterday
Yesterdays question******************************
I distribute roof estimating software, that shps with a lot of sample data in .ini, .txt and rtf files.
With each new version i rename these files as to not overwrite users data. I have been using this
    FirstFile =   gMyPath &  "\GridData.dat"
     SecondFile = gMyPath & "\PPSGrids.dat"
     ThirdFile = gMyPath & "\PPSNumbers.dat"

(Dir(FirstFile)) > 0 Then
         Myfile = FirstFile
           Kill ThirdFile
           
       ElseIf Len(Dir(SecondFile)) > 0 Then
         Myfile = SecondFile
           Kill ThirdFile 'we don't need it          
       
     Else
           Myfile = ThirdFile  'use the new file          
    End If
I have about 8 different files to check like this
This works but the code is getting to be a pain
Does anyone know of a better way ?
end Yesterdays question******************************

I have decided to release my next set of files into a folder as to not overwrite users files. i need code to find the users old  files and rename them to the new files names and kill the new sample files and put the files into the new folder for instance:
 New folder name SavedData containing sample files
search for  App.Path   &  "\GridData.dat"  if found rename to PPSNumbers.dat and put it into the SavedData folder and kill or override the PPSNumbers.dat containing sample data

How can i write this?
Avatar of PaulHews
PaulHews
Flag of Canada image

Use Kill to remove existing files and Name As to move existing files:
Dim Dst As String, Src As String
 
Dst = App.Path & "\SavedData\PPSNumbers.dat"
Src = App.Path & "\GridData.dat"
 
If Len(Dir(Dst)) Then
    Kill Src
End If
Name Src As Dst

Open in new window

Sorry, typo...
Dim Dst As String, Src As String
 
Dst = App.Path & "\SavedData\PPSNumbers.dat"
Src = App.Path & "\GridData.dat"
 
If Len(Dir(Dst)) Then
    Kill Dst
End If
Name Src As Dst

Open in new window

You may also wish to test for the existence of both files before performing the action, especially if this is in start up code that runs every time.
Dim Dst As String, Src As String
    
Dst = App.Path & "\SavedData\PPSNumbers.dat"
Src = App.Path & "\GridData.dat"
 
If Len(Dir(Src)) And Len(Dir(Src)) Then
    Kill Dst
    Name Src As Dst
End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of isnoend2001

ASKER

I havent tried it yet, but i am assuming these files cannot be open ?
Deleting or moving an open file will result in a runtime error.
hey Paul
This line:
If Len(Dir(Src)) And Len(Dir(Dst)) Then
Is this the same as
 If Len(Dir(Src)) > 0 And Len(Dir(Dst)) > 0 Then
I have seen code written like that. Is that a default
I jumped the gun a little with this thread being done in your example
I want to end up with this \SavedData\PPSNumbers.dat in this folder
if the first file exists copy over the example data in \SavedData\PPSNumbers.dat
if the second file exists copy over the example data in \SavedData\PPSNumbers.dat
else
use  the example data in \SavedData\PPSNumbers.dat

if the first file exists then the second file will not exist
If the second file exists then copy it over the example
we don't need to check the len(\SavedData\PPSNumbers.dat) it will be supplied with the program
>is this the same as
>If Len(Dir(Src)) > 0 And Len(Dir(Dst)) > 0

Yes, it's the same and ensures that both the GridData.dat file and the \SavedData\PPSNumbers.dat exist.

>if the first file exists copy over the example data in \SavedData\PPSNumbers.dat
if the second file exists copy over the example data in \SavedData\PPSNumbers.dat<

Possible that they will both exist?  Or will it be one of the other.





No they will not both exist, just one or the other,  or maybe neither which ever one exists overwrite the \SavedData\PPSNumbers.dat with the one that exists.
if neither exist use the \SavedData\PPSNumbers.dat
Here's an improved version with arrays that will let you handle more than one group of files (You mentioned you had multiple files.)  The logic is essentially.

If file to replace with exists then
[
   If destination file exists then delete it.
   Move replacement file to destination.
]

Option Explicit
 
Private Sub InitializeFiles()
 
    'Use the arrays to rename other files as you need.
    Dim FirstFile(0) As String, SecondFile(0) As String, DestFile(0) As String
    Dim i As Integer
    
    FirstFile(0) = App.Path & "\GridData.dat"
    SecondFile(0) = App.Path & "\PPSGrids.dat"
    DestFile(0) = App.Path & "\SavedData\PPSNumbers.dat"
    
    'Initialize other file paths if you need to here.
    'Then you can loop through and handle them all.
    
    For i = LBound(FirstFile) To UBound(FirstFile)
        If Len(Dir(FirstFile(i))) Then
            If Len(Dir(DestFile(i))) Then Kill DestFile(i)
            Name FirstFile(i) As DestFile(i)
        End If
        
        If Len(Dir(SecondFile(i))) Then
            If Len(Dir(DestFile(i))) Then Kill DestFile(i)
            Name SecondFile(i) As DestFile(i)
        End If
    Next
 
End Sub

Open in new window

Thanks Paul
I think i understand, for another set of files i add this:
 FirstFile(1) = App.Path & "\Templates.RTF"
    SecondFile(1) = App.Path & "\Paragraphs.RTF"
    DestFile(1) = App.Path & "\SavedData\Paragraphs.RTF"
notice i have changed the index
exactly... and change the upper bound of the declared array... :)
Thanks Paul, your the man