How to connect to Remote desktop to copy files - vbscript

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

Related

VB SCRIPT The system can not find the file specified ON SOME COMPUTERS/USERS

Good day! I have this script that opens up an access application.
This script works on several users but one. This user is getting this error
"Error 80070002: The system can not find the file specified".
I'm quite sure there is nothing wrong with my script as only one person is encountering this issue.
Could there be a computer setting or update that is causing this problem?
Everything works except for the Open File part.
And this is for some computers/user only. Most of the Computer/users can execute this without any problem.
Thanks in advance!
Here's the script
'*******************************************************************************
'Find user name
'*******************************************************************************
Set WshNetwork = CreateObject("WScript.Network")
userName = WshNetwork.UserName
Set WshNetwork = Nothing
'*******************************************************************************
'Find version of master file
'*******************************************************************************
Set folder = objfso.GetFolder(folderPath)
For Each file In folder.Files
If InStr(file.Name, "AMSDshbd_M") = 1 Then
masterVersion = Mid(file.Name, 11, (InStrRev(file.Name, ".") - 11))
Exit For
End If
Next
'*******************************************************************************
'Find version of user file, if it exists
'*******************************************************************************
isUserFile = 0
For each file In folder.Files
If InStr(file.Name, "AMSDshbd_" & userName) = 1 Then
isUserFile = 1
userVersion = Mid(file.Name, (Len(userName) + 10), (InStrRev(file.Name, ".") - (Len(userName) + 10)))
Exit For
End If
Next
'*******************************************************************************
'Copy the file if no user file exists or if the user version is not current
'*******************************************************************************
sourceFile = folderPath & "AMSDshbd_M" & masterVersion & ".accde"
targetFile = folderPath & "AMSDshbd_" & userName & "_M" & masterVersion & ".accde"
isCopyNeeded = 1
if isUserFile = 1 then
if userVersion = masterVersion then
isCopyNeeded = 0
end if
end if
if isCopyNeeded = 1 then
objFSO.CopyFile sourceFile, targetFile, True
end if
'*******************************************************************************
'Open the file
'*******************************************************************************
sComTxt = Chr(34) & microsoftAccessFile & Chr(34) & " " & Chr(34) & targetFile & Chr(34)
'objShell.Run sComTxt
objShell.Run sComTxt,,true
Set objFSO = Nothing
Set objShell = Nothing
I figured out what happened. the variable "microsoftAccessFile" is the path to the MS Access EXE, some of the users have a different path to this Access EXE that's why it doesn't work for them. I identified the path where their Access EXE is stored and changed the script for them and it works now. Thanks for pointing out the variable

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 delete FTP files base from file list produced by mget?

I have a vbscript that basically downloads files base on a filename. I want to get the file list and delete the remote files based from that list.
List of remote files that were downloaded, redirect to downloads.txt_[systemdate]
List of remote files base on item 1. that were deleted (after successful download), redirect to deleted.txt_[systemdate]
Capture any errors in the process of download/delete and
redirect to a errors.txt__[systemdate] in local
Below is my existing vbscript. I'm currently new in that program. Any help is greatly appreciated.
' VBScript Client - Inbound Get FTP Files and Move to 'backup' folder
Option Explicit
Dim objFSO, objMyFile, objShell, strFTPScriptFileName, strFilePut
Dim strLocalFolderName, strFTPServerName, strLoginID
Dim strPassword, strFTPServerFolder
'Set FTP options
strLocalFolderName = "E:\TEMP\VENDOR"
strFTPServerName = "HOSTNAME"
strLoginID = "USERNAME"
strPassword = "PASSWORD"
strFTPServerFolder = "/SGAII/CCDATA/DEPOSITS"
'Set File name
strFilePut = "./S*"
'Generate FTP command
strFTPScriptFileName = strLocalFolderName & "\FTP_Inbound_Files_from_Client.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If (objFSO.FileExists(strFTPScriptFileName)) Then
objFSO.DeleteFile (strFTPScriptFileName)
End If
Set objMyFile = objFSO.CreateTextFile(strFTPScriptFileName, True)
objMyFile.WriteLine ("open " & strFTPServerName)
objMyFile.WriteLine (strLoginID)
objMyFile.WriteLine (strPassword)
objMyFile.WriteLine ("cd " & strFTPServerFolder)
objMyFile.WriteLine ("bin")
objMyFile.WriteLine ("lcd " & strLocalFolderName)
objMyFile.WriteLine ("prompt off")
objMyFile.WriteLine ("mget " & strFilePut)
objMyFile.WriteLine ("bye")
'Execute the FTP script.
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run "ftp -s:" & Chr(34) & strFTPScriptFileName & Chr(34), , True

Windows 7 Migrate User Profiles

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.

Creating a Zip then copying folders to it

I'm trying to create a zip file, then copy three folders into it. I get the error on line 33 char 1, error state object required, I have searched and googled but just can't seem to either understand what I'm reading or understand what I really need to search for. Anyhow, here is my code.
Option Explicit
Dim objFSO, objFolder1, objFolder2, objFolder3, FolderToZip, ziptoFile, FolderGroup
Dim ShellApp, eFile, oNewZip, strZipHeader
Dim ZipName, Folder, i, Zip, Item
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder1 = objFSO.GetFolder("C:\Windows\Temp\SMSTSLog")
Set objFolder2 = objFSO.GetFolder ("C:\Windows\System32\CCM\Logs")
Set objFolder3 = objFSO.GetFolder ("C:\Windows\SysWOW64\CCM\Logs")
'For Each efile In objFolder.Files
' If DateDiff("d",eFile.DateLastModified,Now) >= 2 Then
' objFSO.MoveFile eFile, "C:\Documents and Settings\User\Desktop\Test2\"
' End If
'Next
Wscript.Sleep 2000
Set oNewZip = objFSO.OpenTextFile("C:\win7tools\testing script.zip", 8, True)
strZipHeader = "PK" & Chr(5) & Chr(6)
For i = 0 To 17
strZipHeader = strZipHeader & Chr(0)
Next
oNewZip.Write strZipHeader
oNewZip.Close
Set oNewZip = Nothing
WScript.Sleep 5000
FolderGroup = Array(objFolder1,objFolder2,objFolder3)
FolderToZip = "FolderGroup"
ZipToFile = "C:\Win7tools\Test Script.zip"
Set ShellApp = CreateObject("Shell.Application")
Set Zip = ShellApp.NameSpace(ZipToFile)
'Set Folder = ShellApp.NameSpace(FolderToZip)
ShellApp.NameSpace(FolderGroup).CopyHere Zip.NameSpace(ZipToFile)
WScript.Sleep 10000
set ShellApp = Nothing
set FolderToZip = Nothing
set ZipToFile = Nothing
When in doubt, read the documentation:
retVal = Shell.NameSpace(
vDir
)
Parameters
vDir [in]
Type: Variant
The folder for which to create the Folder object. This can be a string that specifies the path of the folder or one of the ShellSpecialFolderConstants values. Note that the constant names found in ShellSpecialFolderConstants are available in Visual Basic, but not in VBScript or JScript. In those cases, the numeric values must be used in their place.
The NameSpace method expects either a string with a path or the integer value of one of the ShellSpecialFolderConstants, not an array of Folder objects. Also you got the order wrong. The object on which you call the copyHere method is the zip file. The argument is what you want to copy to the zip file (a path string should do just fine here). Plus, the name of the zip file you create is different from the name of the zip file you try to add the folders to.
Change your code to this:
folder1 = "C:\Windows\Temp\SMSTSLog"
folder2 = "C:\Windows\System32\CCM\Logs"
folder3 = "C:\Windows\SysWOW64\CCM\Logs"
zipfile = "C:\Win7tools\Test Script.zip"
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.OpenTextFile(zipfile, 2, True).Write "PK" & Chr(5) & Chr(6) _
& String(18, Chr(0))
Set ShellApp = CreateObject("Shell.Application")
Set zip = ShellApp.NameSpace(zipfile)
zip.CopyHere folder1
zip.CopyHere folder2
zip.CopyHere folder3
WScript.Sleep 10000
WinZip has a Command Line Interface. You might have to download and install it depending on your version: http://www.winzip.com/prodpagecl.htm
The below is a test script that works for WinZip version 9.0 if it helps.
Const WinZip = "C:\Program Files\WinZip9.0\wzzip.exe" 'WinZip Version 9.0
BasePath = "C:\Path\To\Folders\"
strZipFilePath = BasePath & "Test.zip"
strArchiveMe = BasePath & "Folder_A"
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FileExists(WinZip) Then
MsgBox "WinZip (wzzip.exe) Does Not Exist"
WScript.Quit
End If
'''// For Below Command - Change "-a" TO "-mu" To Auto Delete The file After Zip Is Created
'''// For Below Command - Change "-yb" TO "-ybc" To Answer YES To all Promps and not Terminate Operation
strcommand = Chr(34) & WinZip & Chr(34) & " -a -yb " & Chr(34) & strZipFilePath & Chr(34) & " " & Chr(34) & strArchiveMe & Chr(34)
objShell.Run strcommand, 1, True
The command format is:
winzip [action] [options] [Zip Path] [Path to file/folder to zip]

Resources