Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Replacing old file with new file

Posted on 2004-09-22
6
Medium Priority
?
172 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…
Suggested Courses

610 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