Solved

Replacing old file with new file

Posted on 2004-09-22
6
170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…

695 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