Link to home
Start Free TrialLog in
Avatar of NEXPERT-AG
NEXPERT-AG

asked on

Script to replace old file / shortcut with a new one

I got a migration of a software tonight.  The users start the software using a *.FP7 file, which is usually called mondo.fp7.  Now the problem is that I need to replace this mondo.fp7 file with a new one called mondo_11.fp7 file.  

The users placed this mondo.fp7 file on different locations, the majority on the desktop.  Now I need to replace this mondo.fp7 wherever it's located in the users profile with the new mondo_11.fp7 file.

As this migration takes place in a large environment I would like to script the "find mondo.fp7 and replace with mondo_11.fp7 wherever it's located" taks.

Help is appreciated.
Avatar of jawa29
jawa29
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi

This script will search for a file within the current users profile, it will then copy a new file from a given location and then delete the original if the copy was successful.

Hope it helps

Jawa29
Const USER_PROFILE = &H28&

On Error Resume Next

sFileToFind = "FileToFind.txt"
sReplacementFile = "Replacement.txt"
sServerLocation = "\\servername\sharename\"

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("Shell.Application")
Set oFolder = oShell.Namespace(USER_PROFILE)
Set oFolderItem = oFolder.Self
sSourceDir = oFolderItem.Path & "\"

Set oFolder = oFSO.GetFolder(sSourceDir)
Set colSubfolders = oFolder.Subfolders

For Each oSubfolder in colSubfolders
	If oSubFolder.Size > 0 Then
		fnCheckFiles(sSourceDir & oSubfolder.Name)
		ShowSubfolders oFSO.GetFolder(sSourceDir & oSubfolder.Name)
	End If
Next

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        fnCheckFiles(SubFolder.Path)
        If Subfolder.Size <> 0 Then
        	ShowSubFolders Subfolder
        End If
    Next
End Sub

Function fnCheckFiles(FolderToCheck)

	Set oFolder = oFSO.GetFolder(FolderToCheck)
	Set oFiles = oFolder.Files
	
	For Each File In oFiles
		Set oFile = oFSO.GetFile(File.Path)
		sFilename = File.Name
		If LCase(sFilename) = LCase(sFileToFind) Then
			Err.Clear
			oFSO.CopyFile sServerLocation & sReplacementFile, FolderToCheck & "\"
			If Err.Number = 0 Then
				oFSO.DeleteFile File.Path
			End If
		End If
	Next
	
End Function

Open in new window

Avatar of NEXPERT-AG
NEXPERT-AG

ASKER

Thanks jawa29, but I would like to run a script logged in as administrator and replace the files from a central location, rather than having a script ever user needs to run from his profile.
SOLUTION
Avatar of ReneGe
ReneGe
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gastone Canali
Probably adding a logon script should be the solution..

Bye Gastone Canali
@echo off
:: replacefile.cmd
:: Replace the file found inside the user profile
:: bye Gastone Canali
::
:: Add this script as "logon script"
::
:: Setting up a Logon Script
:: http://technet.microsoft.com/en-us/library/bb742376.aspx
setlocal
rem file to be searched
set searchedFile=mondo.fp7
rem complete file name with path (whe the new file is located)
set newFile=\\myDomainController\netlogon\mondo_11.fp7
rem Search based From userprofile Root  ex.: C:\Documents and Settings\user1
cd /d %USERPROFILE%
rem if already Done goto end 
if exist repalce.done goto :_END
echo on
for /f "tokens=*" %%F in ('dir "%searchedFile%" /s /w /b') do copy /y  %newFile% %searchedFile% 
if "%errorlevel%"=="1" goto :_END
echo %date%>repalce.done
:_END

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial