Solved

Replacing old file with new file

Posted on 2004-09-22
6
165 Views
Last Modified: 2010-05-02
Need help writing code.
Each user has a separate folder (user1, user2, user3, etc.)
There are two folders under each user (folder1, folder2)
I want to write a VB program (standard.exe) to replace files in folder1 (file1_user1.txt and file2_user2.txt) and in folder2 (file3_user3.txt) with new files in folder1 (newfile1_user1.txt, newfile2_user2.txt) and folder2 (newfile3_user3.txt)
Path is C:\User1\folder1 and fodler2
C:\User2\folder1 and fodler2
C:\User3\folder1 and fodler2
..
..
Which user gets which file replaced depends on "_user" in the filename

For example: John has folders "c:\John\folder1" and ...\folder2
HIs files in folder1 are  file1_John.txt, file2_John.txt and file in folder2 is file3_John.txt
These files are replaced with newfile_John.txt

"_" is common to all files

Could you please help with code?





0
Comment
Question by:Vasi04
6 Comments
 
LVL 8

Expert Comment

by:mladenovicz
ID: 12124479
FileCopy SourceFile, DestinationFile
Kill FileName 'deletes FileName
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12124483
What code have you got so far?
0
 

Author Comment

by:Vasi04
ID: 12125880
Can anyone please help me with code.
FYI, I am NOT an IT guy.
I am a normal commerce guy trying to do things on my own.
Thanks a lot.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:Prestaul
ID: 12127047
Is this a one-time batch or a regular task.  If it is a one time task then you may want to just do it by hand.  This is a simple program but it sounds like you need full source code.

Are the three files all replaced with the same file or should one of your last lines have read:

These files are replaced with newfile1_John.txt, newfile2_John.txt, and newfile3_John.txt

?
0
 

Author Comment

by:Vasi04
ID: 12130564
Hi Prestaul,

>>These files are replaced with newfile1_John.txt, newfile2_John.txt, and newfile3_John.txt
 Replaced with different file names

Batch job? You mean, program which runs according to a schedule.
 .exe program can be made to run every week or so?

I would be grateful to you, if you could provide me with full code. Thanks again
0
 
LVL 1

Accepted Solution

by:
crjor earned 500 total points
ID: 12131587
Try this:

Open a new VB project
Put a command button on the form
Past the  sourec below into your form

Option Explicit

Dim DirList(2) As String

Private Sub Command1_Click()
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim x As Integer
    Dim sFile As String
    Dim sTemp As String
    Dim FileList As Variant
    Dim oldFile As Variant
    Dim newFile As Variant
    x = 0
   
    On Error GoTo HandleError
    For i = 0 To UBound(DirList)
        sFile = Dir(DirList(i))
        sTemp = ""
        Do While sFile <> ""
             If sFile <> "." And sFile <> ".." Then
                sTemp = sTemp & sFile & vbCrLf
            End If
            sFile = Dir
        Loop
       
        If sTemp <> "" Then
            FileList = Split(sTemp, vbCrLf)
            For j = 0 To UBound(FileList) - 1
                For k = 0 To UBound(FileList) - 1
                    If FileList(j) <> FileList(k) Then
                        If InStr(1, FileList(k), FileList(j)) > 0 Then
                            If x > 0 Then
                                ReDim Preserve oldFile(x)
                                ReDim Preserve newFile(x)
                            Else
                                ReDim oldFile(x)
                                ReDim newFile(x)
                            End If
                            oldFile(x) = DirList(i) & FileList(j)
                            newFile(x) = DirList(i) & FileList(k)
                            x = x + 1
                        End If
                    End If
                Next k
            Next j
        End If
    Next i
    sTemp = ""
    If x > 0 Then
        For j = 0 To x - 1
            sTemp = sTemp & newFile(j) & "=>" & oldFile(j) & vbCrLf
        Next j
    End If
    If sTemp <> "" Then
        sTemp = "Are you sure you want to rename the following files ?" & vbCrLf & vbCrLf & sTemp
        If MsgBox(sTemp, vbYesNo Or vbQuestion, "Rename Files") = vbYes Then
            For j = 0 To x - 1
                Kill oldFile(j)
                DoEvents
                FileCopy newFile(j), oldFile(j)
                DoEvents
                Kill newFile(j)
                DoEvents
            Next j
        End If
    End If
   
    Exit Sub
HandleError:
    Select Case Err
        Case Else
            MsgBox Err.Description & " : " & Str(Err.Number), , "File Rename Error"
            Exit Sub
    End Select
End Sub

Private Sub Form_Load()
'  to add more directories you need to change the DirList array in the general declaration section
    DirList(0) = "C:\John\"
    DirList(1) = "C:\Joe\"
    DirList(2) = "C:\Sam\"
End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now