Some useful code I found or put together

Although this site is not about coding, I thought I would share some of the code I eventually figured out after looking online and having a hard time finding exactly what I was looking for.
Perhaps it was the way (terms) I searched Google, but I found it hard to find what I thought would be pretty common code.
I am not a coder / programmer by any means. So I use books and Google.
I usually have to edit the code I find to suite my needs because I might find something close to what I want but not exactly.
Obviously, there is no licensing since most of it is basic and available in many places, and I just put it together.

Some basic stuff first
I have learned that running a command in a .bat (batch) file can act differently than it does being run right from the CMD (command line) window. So if you test something in a CMD window and find it does not work right once put into a .bat file you may need to make some changes.
– I have had this happen to me, but I never would have figured that it made a difference… I was just “Why is this not working, it does in my test…”

You can use ./ to represent the current folder location instead of typing out the full path.
Ex. If you have a folder with several files you want to install or run, and the script file is in the same folder you can simply use:
start ./file.exe instead of start d:\folder\folder\file.exe

User Profile shortcut
You can use a shortcut to the users Profile or folders in it.
%USERPROFILE% can take the place of c:/users/username in CMD, VBscript, or .bat files. So to represent a users Desktop use: %USERPROFILE%\Desktop
This is great when you write something to be used on different computers and you have no idea what the user names are going to be.
PowerShell uses a similar $env:USERPROFILE.

Backup user data with prompts
This is a VBscript (Visual Basic script) with source and destination prompts to backup user data with Robocopy.
The default source is the users Profile directory. It will copy everything like Documents, Pictures, Favorites… except the AppData folder and other hidden folders.
*Oh, it seems hard to copy Chrome and Firefox bookmarks, So I have separate code to copy their profile folders. see next code for them. 
It will create the destination folder if needed or quit if the destination folder already exists to not over write other backups. I chose this option because I usually use last names as the destination folder, and you can easily have multiple people with the same last name – or other reasons…

You can include AppData  by removing the /XD AppData switch.

Dim objFSO
Set objShell = CreateObject(“Wscript.Shell”)
Set objFSO = CreateObject(“Scripting.FileSystemObject”) ‘Standard File System Object

Call ElevateThisScript()
Sub ElevateThisScript()

objSource = InputBox(“Add the SSO to complete source location”, “Backup”, “C:\users\”)

objDestination = InputBox(“Enter the Destination directory path. Backup will begin”, “Backup Destination”, “D:\”)
If objFSO.FolderExists(objDestination) Then
Wscript.Echo “That folder already exists. Quiting. Create/ use a new folder”

objFSO.CreateFolder(objDestination) ‘Create the backupdoc folder
End If

objShell.Run( “cmd.exe /k robocopy.exe ” & Chr(34) & objSource & Chr(34) &” ” & Chr(34) & objDestination & Chr(34) & ” /MIR /XA:SH /XD AppData /XJD /R:1 /W:1″)

End Sub

WScript.Echo “Backup in progress! – REMINDER get .PST files. Confirm BOX files. Ask user about other folders and bookmarks or what ever text you want to remind or say “

Restore user data
Here is the code to restore the files to the new computer. Also with prompts. you will want to run this while logged into the user account, or have the account created.

‘Restoere Files  Script
Dim BackupDriveLetter, WshNetwork, strUser, objShell, objFSO
Dim spath, dpath, s0
Set WshNetwork = WScript.CreateObject(“WScript.Network”)  ’Standard Network Object
Set objShell = CreateObject(“WScript.Shell”)
Set objFSO = CreateObject(“Scripting.FileSystemObject”) ’Standard File System Object

spath = InputBox(“Backup drive folder for source locatoin”, ”Restore to PC”, ”D:\”)’Source Path we will be copying

s0 = ”/MIR ” ’ Restore switches for Robocopy, mirror

‘Asks to start the backup
X=MsgBox(spath,36,”Confirm Source path”)
If X = 6 Then ’If MsgBox is answered YES

StartBackup() ’Start main Subroutine
X=MsgBox(“Choose correct source location and try again ”,48,”Error”)
WScript.Quit ’If MsgBox is answered NO
End If

‘Main Program ****************************************
Sub StartBackup()

‘ Check to see if the My Documents Path exists
dpath = InputBox(“Enter the Destination directory path. Restore will begin”, ”Restore Destination”, ”C:\Users\”)
If objFSO.FolderExists(dpath) Then
Wscript.Echo ”The user does exists.”
X=MsgBox(“No such user. Choose correct SSO or destination and try again ”,48,”Error”)
WScript.Quit ’If MsgBox is answered NO
End If

objShell.Run( ”cmd.exe /k robocopy.exe ””” & spath & ””” ””” & dpath & ””” ” & s0 )

End Sub

Wscript.Echo ”Restore in progress. REMINDER get .PST files. Confirm BOX files. and bookmarks or what ever text you want to remind or say”

Chrome and Firefox bookmarks location and backup.
Chrome and Firefox bookmarks are kept in the AppData folder, and it seems to be hard to copy them automatically with prompts for username’s. So for  now I manually copy the file/folder they are in. It’s actually faster then editing the code each time.
*If you can get into the users logon, it is best to export them.

Firefox:   C:\Users\username\AppData\Roaming\Mozilla\Firefox\Profiles
Then you need to go into the randomly created profile name \bookmarkbackups\
Then copy the most recent .jsonlz4  file.

Chrome:  C:\Users\username\AppData\Local\Google\Chrome\User Data\Default\bookmarks     file with no extention.
you can also get bookmarks.bak if you want.

Network drives and Printers export
It also seems hard to make a code to export printers and mapped drives, but if you
copy and paste these into a command line cmd box, you can get the registry file to then double click on and import into the new PC.

Network mapped drives:       reg export HKEY_CURRENT_USER\Network C:\netdrives.reg

Network Printers :                   reg export HKEY_CURRENT_USER\Printers C:\printers.reg

Erase temp files or other files. 
Robocopy can also be used to delete files with the /purge switch.
Where I work our software seems to save temp files like crazy. Gigs and Gigs and more Gigs. Enough to fill a hard drive to have Windows warn the user their C: drive is full.
*I have since learned that this is probably a Windows issue with the C:\Windows\Logs\CBS folder sending excess files to C:\Windows\Temp.
Read this from Computer World for more details.
Here are batch and the VBscript to delete the various temp files and clean the hard drive.
I think the .bat file can delete more files/folders if run as Admin.
You have to edit the .bat with the username (represented by SSO) before running it. Or try the shortcut mentioned above – I have not tried it here yet.
The VBscript prompts for username.

*With the new info about the CBS folder, You need to Stop the Windows Modules Installer service and delete the files in C:\Windows\Logs\CBS also.
You can do this manually or add a line to the script with this location.
After a reboot the Windows Modules Installer service will restart and the excess temp folder fill up should stop.

Bat file:
mkdir c:\empty
call robocopy c:\empty c:\temp /purge
robocopy c:\empty c:\tmp /purge
robocopy c:\empty c:\windows\temp /purge
robocopy c:\empty c:\users\SSO\appdata\local\temp /purge
echo “Ask user to empty or clean up Downlods folder? check other users and empty Recycle Bin”

Dim WshNetwork, strUser, objShell, objFSO
Dim spath, dpath, s0
Set WshNetwork = WScript.CreateObject(“WScript.Network”) ‘Standard Network Object
Set objShell = CreateObject(“WScript.Shell”)
Set objFSO = CreateObject(“Scripting.FileSystemObject”) ‘Standard File System Object

dpath = InputBox(“Enter the user SSO”, “Destination”, “SSO”)
objShell.Run( “cmd.exe /k mkdir c:\empty & robocopy c:\empty c:\temp /purge & robocopy c:\empty c:\tmp /purge & robocopy c:\empty c:\windows\temp /purge &robocopy c:\empty c:\users\”& dpath & “\appdata\local\temp /purge”)
Wscript.Echo “REMINDER empty Recycle Bin, Downloads folder? Check for other users”

I think I have to approve comments and I don’t always see them quickly, so they might not show up for a while…. If anyone leave any…..

Key words: Backup user data. Data backup. Computer backup. User profile backup. Backup my documents. Backup to another computer. Move data to another computer. Robocopy. cmd. command line. chrome bookmarks location export copy. firefox bookmarks location export copy. copy or export network mapped drives and printers. robocopy to delete files.

Edited 8/15/19 and 8/23/19 to add some new code and info.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s