Solved

Replacing old file with new file

Posted on 2004-09-22
6
164 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

747 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

12 Experts available now in Live!

Get 1:1 Help Now