[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 650
  • Last Modified:

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.
0
NEXPERT-AG
Asked:
NEXPERT-AG
3 Solutions
 
jawa29Commented:
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

0
 
NEXPERT-AGAuthor Commented:
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.
0
 
ReneGeCommented:
This script will search in a the PC for replacing your file name.

The FOR %%C command line may beed to be reviewed, but you can get the idea.

@jawa29:
-This is VBScript. Right?
-What if there are multiple USERS using a PC?
-Would it be difficult to add the posibility to scan all profiles in XP and W7?
-This way, NEXPERT-AG will be able to scan his whole network by running my script and by using PSEXEC, to run yours on each PCs.
-Or maybe incorporate the functions of mine in yours? (who knows)

Cheers,
Rene

 
@ECHO OFF

REM This will replace the OldFileName by the NewFileName
REM on all PCs that the name starts with PC
REM CUSTOMIZE IT AS NEEDED

SETLOCAL enabledelayedexpansion

SET OldFileName=mondo.fp7
SET NewFileName=mondo_11.fp7 

FOR %%A IN (PCR) DO (
	FOR /F "tokens=1 delims= " %%B IN ('NET VIEW ^| FINDSTR -i \\%%A') DO (
			ECHO SCANNING:"%%B"
			FOR /F "delims=" %%C IN ('dir /s /b %%B\C$\%OldFileName%') DO (
				ECHO [%%~fC]
				ECHO [%%~fC]>>"%~n0.log" 
				REN "%%~fC" "%NewFileName%"
			)
	)
)

ECHO.
ECHO DONE
PAUSE

Open in new window

0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
canaliCommented:
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

0
 
canaliCommented:
If you want serch inside  all  c:\
Bye Gastone

@echo off
setlocal
set searchedFile=mondo.fp7
set newFile=\\myDomainController\netlogon\mondo_11.fp7
rem search from c:\
cd /d c:\
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

0
 
jawa29Commented:
Hi NEXPERT-AG

If you replace this line in the code:

sSourceDir = oFolderItem.Path & "\"

With this then it will search all users on the PC

Set oParentFolder = oFSO.GetFolder(oFolderItem.Path)
sSourceDir = oParentFolder.ParentFolder & "\"

Changes outlined in bold

Jawa29
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now