Solved

Copy a file after renaming another file

Posted on 2013-11-06
3
253 Views
Last Modified: 2013-11-18
I need to replace a file named file.xml for all users on the computer remotely
The file is located at this location and needs to be replaced by finenew.xml

C:\Users\abcuser\AppData\Local\App01\file.xml

however i do not want to delete the filr.xml instead need to rename it to fileold.xml and keep it at the same location

So if the file exists the scripts should run and replace it with mu new file and rename the old file
0
Comment
Question by:MOQINFRA
3 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 39629648
Hi, this VBS implemented as a login script should work for you.

Regards,

Rob.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strFileToReplace = objShell.ExpandEnvironmentStrings("%LOCALAPPDATA%") & "\App01\File.xml"
strBackupFile = objShell.ExpandEnvironmentStrings("%LOCALAPPDATA%") & "\App01\File.xml.old"
strNewFile = "\\server\share\newfile.xml"
If objFSO.FileExists(strBackupFile) = False Then
	If objFSO.FileExists(strFileToReplace) = True Then
		objFSO.CopyFile strFileToReplace, strBackupFile, True
		objFSO.CopyFile strNewFile, strFileToReplace, True
	End If
End If

Open in new window

0
 
LVL 14

Accepted Solution

by:
Faustulus earned 500 total points
ID: 39629677
Please paste the code below into a standard code module in any blank workbook. Then run the procedure 'CopyAndRename'.
Option Explicit
Option Base 0

Sub CopyAndRename()

    ' modify path as required
    Const PathName As String = "C:\Users\abcuser\AppData\Local\App01"
    
    Dim SelFiles() As String
    Dim NewFn As String                     ' Fn = file name
    Dim OldFn As String
    
    If GetSelectedFiles(PathName, SelFiles) Then
        NewFn = NewFileName(SelFiles(0), OldFn)
        If Len(NewFn) Then
            FileCopy SelFiles(0), NewFn
            Name SelFiles(0) As OldFn
        Else
            MsgBox "The selected file has no exension.", _
                   vbCritical, "Invalid file name"
        End If
    End If
End Sub

Private Function GetSelectedFiles(Pn As String, _
                                  SelFiles() As String) _
                                  As Boolean
    ' 0086 V 1.0
    
    Dim FoD As FileDialog                           ' File Open Dialog
    Dim SelItem As Variant                          ' Selected Item
    Dim i As Long                                   ' Index for SelFiles
    
    Set FoD = Application.FileDialog(msoFileDialogFilePicker)
    With FoD
        .Title = "Choose the file to copy"
        .ButtonName = "Create Copy"
        .Filters.Clear
        .Filters.Add "XML files", "*.xml", 1
        .InitialFileName = WithSeparator(Pn)
        .AllowMultiSelect = False
        If .Show Then
            For Each SelItem In .SelectedItems
               ReDim Preserve SelFiles(i)
               SelFiles(i) = SelItem
               i = i + 1
            Next SelItem
            GetSelectedFiles = True
        End If
    End With
    Set FoD = Nothing
End Function

Private Function NewFileName(Ffn As String, _
                             OldFn As String) As String

    Dim Sp() As String
    Dim Fn As String                ' File name (original)
    Dim Ext As String
    
    Sp = Split(Ffn, ".")
    If UBound(Sp) Then
        Ext = Sp(UBound(Sp))
        Fn = Left(Ffn, Len(Ffn) - (Len(Ext) + 1))
        OldFn = Fn & "Old." & Ext
        NewFileName = Fn & "New." & Ext
    End If
End Function

Private Function WithSeparator(ByVal PathName As String, _
                               Optional ByVal RemoveExisting As Boolean) _
                               As String
                       
    Do While Right(PathName, 1) = Application.PathSeparator
        PathName = Left(PathName, Len(PathName) - 1)
    Loop
    WithSeparator = PathName & IIf(RemoveExisting, "", _
                                   Application.PathSeparator)
End Function

Open in new window

The program will allow you to select a file, starting in the directory you mentioned (you can choose another default by assigning another string to the constant 'PathName' at the top of the code). When you press the "Create Copy" button in the file_open dialog box the selected file will be renamed as [file]New.[Ext] and a copy will be created as [file]Old.[Ext]
I wonder if this is your intention because no file of the original name will remain. However, if I misunderstood your instruction this is a small thing to correct in the above code which I shall be glad to do.
0
 

Author Comment

by:MOQINFRA
ID: 39638781
Thanks , will test it and update you guys
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

706 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

23 Experts available now in Live!

Get 1:1 Help Now