Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Copy a file after renaming another file

Posted on 2013-11-06
3
Medium Priority
?
261 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
[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
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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

670 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