VBscript , create directory in FTP - vbscript

I would like to create a directory in FTP, the name of the directory must be as my computer name,
here is my code,
Dim FoldertoCreate, filesys, newfolder, Ob
Set Ob = Wscript.CreateObject("Wscript.Network" )
FoldertoCreate = "ftp://user:password#ftpserver/url-path/" & ob.ComputerName
Set filesys = CreateObject("Scripting.FileSystemObject")
If Not filesys.FolderExists(FoldertoCreate) Then
Set newfolder = filesys.CreateFolder(FoldertoCreate)
End If
This code doesn't work however when i replace ftp://user:password#ftpserver/url-path with any local directory like D:/ , it works :S
how to make it work for my ftp too

The FileSystemObject does not support FTP. The Shell Automation Object does, but it doesn't seem to like the NewFolder method. That leaves us with automating the FTP.exe command using an unattended FTP session. It might look something like this.
strUser = "myusername"
strPass = "mypassword"
strHost = "ftp.myhost.com"
Const ForWriting = 2
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.OpenTextFile("session.txt", ForWriting, vbTrue)
With objFile
.WriteLine "OPEN " & strHost
.WriteLine "USER " & strUser
.WriteLine strPass
.WriteLine "mkdir sometestdirectory"
.Close
End With
strFTP = "%systemroot%\System32\ftp.exe -s:session.txt"
Set WshShell = CreateObject("WScript.Shell")
strFTP = WshShell.ExpandEnvironmentStrings(strFTP)
WshShell.Run strFTP,, vbTrue
objFso.DeleteFile "session.txt", vbTrue

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

VBScript Using Computername to Name a File

New to VBScript and having a problem grasping this concept.
This is the code:
Set WshNetwork = WScript.CreateObject("WScript.Network")
strCompName = WshNetwork.Computername
Wscript.Echo WshNetwork.Username >j:\strCompName.txt
WScript.Quit()
Basically I want to the username dumped to a text file and the text file should be named with the name of the computer. I've tried putting the strCompName in quotes, single quotes, parenthesis with no success.
Here is the code that you can use. You need to use FileSystemObject. The FileSystemObject is used to gain access to a computer's file system. It can create new files and access existing ones.
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strCompName = WshNetwork.Computername
'writing to file
outFile="c:\TEMP\" & strCompName & ".txt"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write WshNetwork.Username & vbCrLf
objFile.Close
Set objFile = Nothing
Set objFSO = Nothing
Set WshNetwork = Nothing
WScript.Quit()
Save this in .vbs file and run and you will get a text file with computer name in TEMP folder (Change the path if you like).
This code should work. This code opens the file and appends it if the file exists or creates a file and writed to it if it does not exist.
'constants
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
'Load domain, username, & computer variables
Set oShell = CreateObject( "WScript.Shell" )
sDomain = oShell.ExpandEnvironmentStrings( "%USERDOMAIN%" )
sUserName = oShell.ExpandEnvironmentStrings( "%USERNAME%" )
sComputer = oShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
'Setup filesystemobject
Set oFSO=CreateObject("Scripting.FileSystemObject")
'Check to see if file exists. If exists open it forAppending
'else create file and write to it.
outFile="c:\export\" & sComputer & ".txt"
If oFSO.FileExists(outFile) Then
Set objFile = oFSO.OpenTextFile(outFile, ForAppending, True, TristateTrue)
Else
Set objFile = oFSO.CreateTextFile(outFile,True)
End If
'write to file
objFile.WriteLine sDomain & "\" & sUsername & " - " & Now
'clean up objects
objFile.Close
Set objFile = Nothing
Set oFSO = Nothing
Set oShell = Nothing

Programmatically add 'My Network Place' for FTP site?

Is there anyway I can create a small exe or batch file to setup a new 'My Network Place' in Windows? Its for an ftp site if that makes any difference.
XP will primarily be the target machine but If I can find something that will work on Vista too thats great.
I wrote this script to connect to a FTP using a proxy server. You could adapt it for your needs. It prompts for the filename and folder you are trying to access. Just cut the code you don't need, and you should be good to go.
You will need to change the FTP Server Name variable as well. Happy coding:
Option Explicit
Dim objShell, strFTPScriptFileName, strFile2Get
Dim strLocalFolderName, strFTPServerName, strLoginID
Dim strPassword, strFTPServerFolder, strFileToGet, returnCode
'Customize code here to fit your needs
strFTPServerName = "proxy.prv"
strLocalFolderName = ""
strLoginID = ""
strPassword = ""
strFTPServerFolder = ""
strFileToGet = ""
strLocalFolderName = GetLocalFolder()
strLoginID = InputBox("Enter FTP Username: ", "Enter FTP Username", "Authentication_Method#Destination_FTP_Host")
strPassword = InputBox("Enter FTP Password: ", "Enter FTP Password", "Authentication_Method#Destination_FTP_Host")
strFTPServerFolder = InputBox("Enter FTP folder that you want to access: ", "Enter FTP Folder", "/")
strFileToGet = InputBox("Enter the filename located on the FTP that you want to retrieve: ", "Enter FTP file", "*.*")
if strLoginID = "" then
WScript.Echo "You must specify a Login ID for this script to work"
WScript.Quit()
end if
if strPassword = "" then
WScript.Echo "You must specify a Password for this script to work"
WScript.Quit()
end if
if strFTPServerFolder = "" then
WScript.Echo "You must specify a FTP Folder to access for this script to work"
WScript.Quit()
end if
if strFileToGet = "" then
WScript.Echo "You must specify a Filename to download for this script to work"
WScript.Quit()
end if
Call WriteFTPScript()
Set objShell = WScript.CreateObject( "WScript.Shell" )
returnCode = objShell.Run( "cmd.exe /c ftp -s:" & chr(34) & strFTPScriptFileName & chr(34), 1, true)
if (returnCode = 0) then
Wscript.echo("Your file has been downloaded.")
else
Wscript.echo("An error has occured while attempting to download your file.")
End if
objShell.Run (strLocalFolderName)
Set objShell = Nothing
' **************************************************************************
' Creates the FTP script text file
Function WriteFTPScript()
Dim objFSO, objMyFile
strFTPScriptFileName = strLocalFolderName & "\FTPScript.txt" 'File to be created to hold ftp script data
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 ("lcd " & strLocalFolderName)
objMyFile.WriteLine ("get " & strFileToGet)
objMyFile.WriteLine ("bye")
objMyFile.Close
Set objFSO = Nothing
Set objMyFile = Nothing
End Function
' **************************************************************************
' Dialog box to select folder to download to
Function GetLocalFolder()
Const BIF_returnonlyfsdirs = &H0001
Const BIF_editbox = &H0010
Dim wsh, objDlg, objF
Set objDlg = WScript.CreateObject("Shell.Application")
Set objF = objDlg.BrowseForFolder (&H0, "Select the destination folder to download FTP files to:", BIF_editbox + BIF_returnonlyfsdirs)
If IsValue(objF) Then
GetLocalFolder = objF.ParentFolder.ParseName(objF.Title).Path
Else
WScript.Echo "You MUST specify a folder to download files to. Application will now exit."
WScript.Quit
End If
end function
' **************************************************************************
' Verifies if the the object contains a value
Function IsValue(obj)
Dim tmp
On Error Resume Next
tmp = " " & obj
If Err <> 0 Then
IsValue = False
Else
IsValue = True
End If
On Error GoTo 0
End Function
' **************************************************************************
' Verifies if the the object is a folder
Function IsFolder(obj)
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
if objFSO.IsFolder(obj) then
IsFolder = True
end if
End Function
Yes, there is. The NetHood folder can be manipulated with vbScript. Refer to this forum thread for more information. The following works for me on XP Pro:
Option Explicit
On Error Goto 0
'ShellSpecialFolderConstants
Const ssfNETHOOD = 19 '(&H13) Special Folder NETHOOD
Dim objWSHShell, objShell, objFolder, objFolderItem, strNetHood
Dim strShortcutName, strShortcutPath, objShortcut
Set objWSHShell = CreateObject("Wscript.Shell")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(ssfNETHOOD)
Set objFolderItem = objFolder.Self
strNetHood = objFolderItem.Path
strShortcutName = "FTP to localhost"
strShortcutPath = "ftp://username#localhost/"
Set objShortcut = objWSHShell.CreateShortcut(strNetHood & "\" & strShortcutName & ".lnk")
objShortcut.TargetPath = strShortcutPath
objShortcut.Save

Resources