script for finding a file and renaming it then replacing it with a new file

chasmant
chasmant used Ask the Experts™
on
I have this script but what I would like it to do is go through all of the users profile on a pc and if it finds the file rename it and then replace it with a specified new file
any ideas how I can make this work?

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strAppData = objShell.ExpandEnvironmentStrings("%userprofile%\AppData\Local\")
strOldFile = strAppData & "\Microsoft\Microsoft Dynamics NAV\ClientUserSettings.config"
strNewFile = "\\server\share\ClientUserSettings.config"
If objFSO.FileExists(strOldFile) = True Then
      objFSO.CopyFile strOldFile, strOldFile & ".old", True
      objFSO.CopyFile strNewFile, strOldFile, True
End If
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
1/ the envionment string is actually just "APPDATA".   try the slightly mod version
strAppData = objShell.ExpandEnvironmentStrings("AppData")
strAppData = strAppData & "\Local\Microsoft\Microsoft Dynamics NAV\"

2/ do you want ALL of the profile to be searched, or just the APPDATA ...\MicroSoftDynamics folder.

3/ there may well be "ClientUserSettings.config" for a different application that you may not want to be replaced.


Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strAppData = objShell.ExpandEnvironmentStrings("%AppData%")
strAppData = strAppData & "\Microsoft\Microsoft Dynamics NAV\"

strOldFile = strAppData & "ClientUserSettings.config"

strNewFile = "\\server\share\ClientUserSettings.config"

'wscript.echo strOldFile & vbcrlf &  strNewfile

If objFSO.FileExists(strOldFile) = True Then
   'wscript.echo "found"
      objFSO.CopyFile strOldFile, strOldFile & ".old", True
      objFSO.CopyFile strNewFile, strOldFile, True
  else
    'wscript.echo "missing"
End If 

'wscript.echo "done"

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Your target is somewhat unclear. The above code should work if you want to replace a single file of a single user profile (the currently active one). That does not require any search. However, the word "all" in your questions puzzled me. If it isn't that simple, please elaborate more, and provide some examples.

Author

Commented:
all of the directories in each user would be great
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Author

Commented:
when I just used the appdata it would always go to the roaming folder for user so I changed it but if we searched all of the folders in each C:\users folder that would take care of the issue
so I guess what I am looking for is for it to search in each Users folder in C:\users
and If it finds the file rename it to .old and then copy the new file to that location.
There should be only one but if there is multiple then we should rename and copy new file to that location also.
thanks
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Only admins can do that, and only if they obtain the access rights. User profile folders are protected from being accessed by Joe Smith.

Regarding %AppData% - yes, that is the roaming profile path. %LocalAppData% however provides the location of the ...\Local\... branch.

Author

Commented:
I am admin so I would be running this script yea I think if it searches all of the folders in each users I would be covered if there is a copy somewhere else.
anyway to modify the script above to search through all of the folders for the file in the original script and rename it old and then copy the new file?
Commented:
robberbaron
I would like all of the folders in each user profile searched for the file "ClientUserSettings.config"
If found rename it old and then copy the new file from the server to that folder and then contijnue searching for any others in each profile in the C:\user folder.
Hope that clears it up thanks
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Doing it right is much more complex than it sounds on a first glance:
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strProfile = objShell.ExpandEnvironmentStrings("%UserProfile%")
strAppData = Replace(objShell.ExpandEnvironmentStrings("%AppData%"), strProfile, "")
strAppData = strAppData & "\Microsoft\Microsoft Dynamics NAV\"

strOldFile = strAppData & "ClientUserSettings.config"
strNewFile = "\\server\share\ClientUserSettings.config"

for each fld in objFSO.GetFolder(strProfile & "\..").SubFolders
  if objFSO.FileExists(fld.Path & strOldFile) then
      WScript.Echo "Found for for " & fld.Name
      objFSO.CopyFile strOldFile, strOldFile & ".old", True
      objFSO.CopyFile strNewFile, strOldFile, True
  end if
next

Open in new window


while ClientUserSettings.config is a reasonably long name, there is no guarantee that it is only for MS NAV. In fact is a name many MS apps may use.
Olemo's code will search each users folders for ANY file matching that name. Is that an acceptable risk ?  

And you will have to personally login to each machine and run the script.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
It will NOT search the complete profiles. Line 6 restricts it to Dynamics NAV, and I'm not parsing recursively.

Author

Commented:
So the 2 directories I need searched are %appdata% and %localappdata%
"\Microsoft\Microsoft Dynamics NAV\"
How can I make it look in both locations in the script above by Qlemo:
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
You really should be able to do that yourself, but here you go (not tested):
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strProfile = objShell.ExpandEnvironmentStrings("%UserProfile%")
strAppData = array(_
	Replace(objShell.ExpandEnvironmentStrings("%AppData%"), strProfile, ""), _
	Replace(objShell.ExpandEnvironmentStrings("%LocalAppData%"), strProfile, ""))

strNewFile = "\\server\share\ClientUserSettings.config"

for each usr in objFSO.GetFolder(strProfile & "\..").SubFolders
  for each appdata in strAppData
    strOldFile = usr.Path & appdata & "\Microsoft\Microsoft Dynamics NAV\ClientUserSettings.config"
    
    if objFSO.FileExists(strOldFile) then
        WScript.Echo "Found for for " & usr.Name
        objFSO.CopyFile strOldFile, strOldFile & ".old", True
        objFSO.CopyFile strNewFile, strOldFile, True
    end if
  next
next

Open in new window

Author

Commented:
Ok I am new at this but if it does not find the folder it gives error would like it to go to next folder if it has been through the folders go to next profile
last question I promise

Thanks in advance

Author

Commented:
I got it thanks

Author

Commented:
thanks saved me on this one

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial