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
Solved

Replacing old file with new file

Posted on 2004-09-22
6
168 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

792 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