Windows 7 Migrate User Profiles - windows

We are trying to create a script to migrate every users profile over in Windows 7 in the case of replacing the PC due to faults etc.
We have been using the following:
Dim fso
Dim oFolder1, objFolder, oFolder
Dim path
Dim colFolders
Dim sDocsAndSettings
Dim strDirectory
Set fso = createobject("Scripting.FileSystemObject")
'===========================================================
'CHANGE SDESTINATION FOLDER PATH HERE
sPath = "C:\Backup"
'===========================================================
Set proFolder = fso.GetFolder(sPath)
'COPY FILES FROM USER PROFILES
sDocsAndSettings = "C:\Users\"
Set colFolders = fso.GetFolder(sDocsAndSettings)
For Each oFolder In colFolders.SubFolders
Select Case LCase(oFolder.Name)
Case "admin", "administrator", "newuser", "all users", "default user", "default user.original", "localservice", "networkservice"
'LEAVE THE DEFAULT PROFILES ON THE MACHINE
Case Else
'MsgBox oFolder.Name
If fso.FolderExists(proFolder) Then
strDirectory = proFolder & "\" & oFolder.Name
If fso.FolderExists(strDirectory) Then
Else
Set objFolder = fso.CreateFolder(strDirectory)
End If
'COPY USER PROFILE FOLDERS to Destination Folder
fso.CopyFolder sDocsAndSettings & oFolder.Name & "\Favorites" , objFolder & "\", True
fso.CopyFolder sDocsAndSettings & oFolder.Name & "\Documents" , objFolder & "\", True
fso.CopyFolder sDocsAndSettings & oFolder.Name & "\Desktop" , objFolder & "\", True
End If
End Select
Next
MsgBox "Backup has been completed successfully!"
Set fso = Nothing
We seem to have hit an issue that aapears to be with the Junction Points (e.g My Documents) in each profile, which is stopping the script with permission errors, as taking the My Documents line out, the script works. Any ideas, or is there a simpler script to migrate profiles over (we need to be able to migrate specific sub-folders).

is the source folder suppose to be "\My Documents" ? it might be looking for a file whihc doesn't exists.
fso.CopyFolder sDocsAndSettings & oFolder.Name & "\Documents" , objFolder & "\", True
I don't know if this makes a differnce but you can alternatively use the object.Copy( destination[, overwrite] ); if you set the overwrite to TRUE, this may make sure that ALL files and folders will be overwritten. Can you also explain the permission issues as "is stopping the script with permission error" is a bit vague.

Related

Trying to code for the first time, but failed with file location

The problem is that I tried to make a file on somebody else's desktop but I don't know what to write in the users.
I tried "C:\Users\" & strUser & "\desktop\Test"
Set fso = CreateObject("Scripting.filesystemobject")
fso.Createfolder "C:\Users\" & strUser & "\desktop\Test"
error: Can't find the file location.
Use the special folder Shell function to return the path to the desktop.
set objShell = Wscript.CreateObject("Wscript.Shell")
strDesktopPath = objShell.SpecialFolders("Desktop")
Set fso = CreateObject("Scripting.filesystemobject")
fso.Createfolder strDesktopPath & "\Test"
I think your code will work if you simply add a \ at the end of your path:
Set fso = CreateObject("Scripting.filesystemobject")
fso.Createfolder "C:\Users\" & strUser & "\desktop\Test\"

How to get %username% in VBScript?

I am trying to hide network path of shared folders from domain users. (Windows Server 2012) I have found this script while searching for network drive labeling:
Option Explicit
Dim objNetwork, strDrive, objShell, objUNC
Dim strRemotePath, strDriveLetter, strNewName
strDriveLetter = "H:"
strRemotePath = "\\servername\sharedfoldername$\"
strNewName = "Save Your Files Here"
'Section to map the network drive
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
'Section which actually (re)names the Mapped Drive
Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(strDriveLetter).Self.Name = strNewName
WScript.Echo "Check : "& strDriveLetter & " for " & strNewName
WScript.Quit
My network path will be like below:
strRemotePath = "\\servername\sharedfoldername1$\%username%"
strRemotePath = "\\servername\sharedfoldername2$\%username%"
strRemotePath = "\\servername\sharedfoldername5$\%username%"
strRemotePath = "\\servername\sharedfoldernameNNN$\%username%"
When I insert %username%, the script does not run.
Kindly guide me how to modify this script that will run as per my requirements.
You can expand environment variables in your path string:
strRemotePath = "\\servername\sharedfoldername1$\%username%"
Set sh = CreateObject("WScript.Shell")
WScript.Echo sh.ExpandEnvironmentStrings(strRemotePath)
or you can build the path from the share and the UserName property of the WshNetwork that you already have:
share = "\\servername\sharedfoldername1$"
Set fso = CreateObject("Scripting.FileSystemObject")
WScript.Echo fso.BuildPath(share, objNetwork.UserName)

How to connect to Remote desktop to copy files

I am not able to copy files onto the remote machine. For manually copying files we log on to the remote desktop, go to that individual folder and then paste the files.
We connect to remote desktop by giving the information
Computer Name : targetserver.x.com
Username : xyz
password : xyz
I am able to copy files to my local machine
source folder = "\Sourceserver\Archive\folder1"
target folder = "C:\Users\TEMPPAHIR\LearnVB\folder1"
but not able to connect to the server for copying files onto it, like not able to copy from
source folder = "\Sourceserver\Archive\folder1"
target folder = "\Targetserver\F\folder1"
I have tried the below code
Option Explicit
'Get environment variables
Set objShell = CreateObject("WScript.Shell")
Set objSystemEnv = objShell.Environment("SYSTEM")
Set objNetwork = CreateObject("WScript.Network")
strEnvVarSinc = objSystemEnv("SINC_DATA")
strEnvVarPcData = objSystemEnv("PC_DATA")
strEnvVarArchive = objSystemEnv("ARCHIVE")
strComputerName = objNetwork.ComputerName
Set objSystemEnv = Nothing
Set objShell = Nothing
Set objNetwork = Nothing
'SINC_DATA_AREA
strSincPath = strEnvVarSinc & "\" & strArea
'PC_DATA_AREA
strPcDataPath = strEnvVarPcData & "\" & strArea
I am able to map the drive now finally on my local machine but getting an error while copying files on to it - error on (18,1) Microsoft VBScript runtime error: permission denied. Is it because of some firewall or some other issue ? I am manually able to connect the same RD by giving in my username and password and then copy files onto the RD.
Please help !
Option Explicit
Dim objNet, sFile, objFSO, Arg, var, strInfo, strComputer, strDomain, objSWbemLocator, objSWbemServices
Dim colSwbemObjectSet, strUser, strPassword, objNetwork, objProcess
Dim SourceFolder, TargetFolder, objWMIService, strMachine, strAltUsername, strAltPwd
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNet = CreateObject("WScript.NetWork")
set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive "T:" , "\\srv10219\F"
SourceFolder = "\\srv10218\Archive\BudgetPiecesStore\20151116_094107"
Wscript.echo "Source folder Name is : " & SourceFolder
TargetFolder = "\\srv10219\F\ICCdata\BudgetPiecesStore\"
Wscript.echo "Target folder Name is : " & TargetFolder
For Each sFile In objFSO.GetFolder(SourceFolder).Files
objFSO.GetFile(sFile).Copy TargetFolder & "\" & objFSO.GetFileName(sFile),True
WScript.Echo "Copying file : " & Chr(34) & objFSO.GetFileName(sFile) & Chr(34) & " to " & TargetFolder
Set objNet = Nothing
Next

Zip a folder up

I am trying to ZIP up a folder in VBScript and it doesn't seem to work. I'm certain I am creating the header file correctly.
It creates the actual file correctly, just doesn't zip the folder.
Anyone got any ideas:
Sub ArchiveFolder (folder)
Dim fso, wShell, sApp, zipFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set wShell = CreateObject("WScript.Shell")
Set sApp = CreateObject("Shell.Application")
Set zipFile = fso.CreateTextFile(folder & ".zip")
' Write zip file header.
zipFile.Write "PK" & Chr(5) & Chr(6) & String(18, 0)
zipFile.Close
sApp.NameSpace(folder & ".zip").CopyHere folder
End Sub
The answer I found here. The magic is in the last Do..Loop where the script wait the Shell to do it job.
ArchiveFolder "sub\foo.zip", "..\baz"
Sub ArchiveFolder (zipFile, sFolder)
With CreateObject("Scripting.FileSystemObject")
zipFile = .GetAbsolutePathName(zipFile)
sFolder = .GetAbsolutePathName(sFolder)
With .CreateTextFile(zipFile, True)
.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, chr(0))
End With
End With
With CreateObject("Shell.Application")
.NameSpace(zipFile).CopyHere .NameSpace(sFolder).Items
Do Until .NameSpace(zipFile).Items.Count = _
.NameSpace(sFolder).Items.Count
WScript.Sleep 1000
Loop
End With
End Sub
Check your argument. folder must be the path to the object you want to put into the zip file. If it's a folder object you have to use folder.Path, because the default method of folder objects is Name, and CopyHere can't find the object with just the name.
You could add some debugging statements to your function to check that:
WScript.Echo TypeName(folder)
If fso.FolderExists(folder) Then
WScript.Echo folder & " exists."
Else
WScript.Echo folder & " doesn't exist."
End If
you could call an external zip file via %comspec%
oShell.Run "%comspec% /c c:\windows\7za.exe a " & oFile &".zip " & oFile & " -tzip",,True
Source http://www.scriptlook.com/zip-large-files-in-a-directory-2/

SFTP transfer file and move file to folder

This is my first post so please excuse my ignorance. I am using a vbscript to zip all .csv type files in a particular folder. After some google searches, I have found a workable vbscript to do this and have enabled a scheduled task to automate this.
What I need to do next is to transfer the zip file via sftp and then "move" that zip file into another folder. I believe the former can be achieved using pscp.exe via command line but can someone show me how to do the latter?
Basically the zipping will be done twice a day and so it will have a timestamp similar to yyyymmdd0900.zip (for 9am schedule) and yyyymmdd1800.zip (for 6pm schedule). After the transfer, I want to move (not copy) the zip file generated into another folder.
Any pointers would be greatly appreciated. Thank you all in advance.
EDIT: Here is some code I slapped together based on some Google searches. It does what I want it to do. Please excuse the "pasting" as i couldn't figure out how to format it properly. Currently, it runs the bat file after copying but I just noticed that i need to send (using PuTTY Secure Copy) the "latest" zip file before moving it to the "completed" folder. Can someone please show me how to do this?
Zipping the file and rename the zip file
My Code :
On Error Resume Next
strFilepath = "c:\files"
strDestination = "c:\files\completed\"
strExtension = "csv"
strYear = Year(Now)
strMonth = Right("0" & Month(Now), 2)
strDay = Right("0" & Day(Now), 2)
strHour = Right ("0" & Hour(Now), 2)
strMinute = Right ("0" & Minute (Now), 2)
strZip = strFilepath & "\" & strYear & strMonth & strDay & strHour & strMinute & ".zip"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFilepath)
For Each objFile in objFolder.Files
strFileExt = objFSO.GetExtensionName(objFile.Path)
If LCase(strFileExt) = LCase(strExtension) Then
ZipFile objFile.Path, strZip
End If
Next
Sub ZipFile(strFileToZip, strArchive)
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FileExists(strArchive) Then
Set objTxt = objFSO.CreateTextFile(strArchive)
objTxt.Write "PK" & Chr(5) & Chr(6) & String(18, Chr(0))
objTxt.Close
End If
Set objApp = CreateObject( "Shell.Application" )
intCount = objApp.NameSpace(strArchive).Items.Count + 1
objApp.NameSpace(strArchive).CopyHere strFileToZip
Do
WScript.Sleep 200
set objNameSpace = objApp.NameSpace(strArchive)
If Not objNameSpace is nothing Then
If objNameSpace.Items.Count = intCount Then
Exit Do
End If
End If
Loop
End Sub
>Move file to a different folder and then run a bat file to secury copy file to a FTP location
'Vars
Dim objFSO, objFileCopy, objFileDelete, dot, files, file
Dim strDestination, folder, subfolder, fileCount, strFilePath
'Strings
strDestination = "C:\Files\Completed\"
strFilePath = "C:\Files"
set objFSO = CreateObject("Scripting.fileSystemObject")
set folder = objFSO.getFolder(strFilePath)
For Each file In folder.files
Set objFileCopy = objFSO.GetFile(file)
If objFSO.GetExtensionName(file) = "zip" Then
objFSO.MoveFile objFileCopy.Path, strDestination
End If
Next
Dim shell
Set shell=createobject("wscript.shell")
Shell.run "C:\testsend.bat"
Set shell=nothing
This will move a file to the specified location.
Sub Move_File(Source_File, Destination_Folder)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.MoveFile Source_File, Destination_Folder
Set fso = Nothing
End Sub
sftp client provides a means to change working directory on the host before performing any file transfers. It would be better to thus transfer the file directly to the target location.
NOTE: The above answer was a result of misunderstanding the question. I read it to mean the file had to be moved on the destination but the real operation was to move the file on the origin.
I found the following example code that moves a file after checking that it exists. Wildcards are allowed for the source parameter but then FileExists may not work. Requires vbscript 2.0 to work.
<%
dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
If filesys.FileExists("c:\sourcefolder\anyfile.html") Then
filesys.MoveFile "c:\sourcefolder\anyfile.html", "c:\destfolder\"
End If
%>

Resources