Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ftp script that accepts user input for password 1

Status
Not open for further replies.

JtheRipper

IS-IT--Management
Oct 4, 2002
274
GB
Hi there,

I need to write a script that has to ftp certain files from 1 server to another. I will hard code the directories, files and the username that will be used in this ftp file (lots of users will be using the same file). The script must only prompt the user for the password. The plan is to put the file on each user's pc and they must just run it and supply the password.

Is this possible either in DOS scripting or VB/Windows scripting ?

Any help will be much appreciated.

Thanks,
J.
 
Quite possible. Assuming you want to use the Windows FTP client that comes with Win2K and later you'll want to look into the use of the -s switch. This lets you feed [ignore]FTP.EXE[/ignore] a response file that includes the user/pw, directory change, and file upload/download commands you want to run.

You can build this file in VB, VBScript, or possibly a BAT/CMD file even though the latter are a bit "clunky" for accepting input and building a text file without some helper utilities.

Tips on automating [ignore]FTP.EXE[/ignore] have been posted here (VB 5&6) and in the VBScript forum on several occasions. Try a search.


Alternatives might include using other FTP client software or even an FTP component like the Microsoft Internet Transfer Control (INet control).
 
Oops, my mistake. I thought I was reading the VB forum.

Same thing applies however, you might want to look (search) in the VB 5&6 forum as well.
 
Hi,

I managed to get this going using VB script. I now have another problem which I hope someone will be able to assist with...

The process works like this:
1. Build up a fpt script to get the required files
2. Execute the ftp script
3. <delete the ftp script from the user's pc> -- this part does not work...

Code:
Option Explicit
Dim objFSO, objMyFile, objShell, strFTPScriptFileName 
Dim strLocalFolderName, strFTPServerName, strLoginID, strFTPServerDir
Dim strPassword, strFTPServerFolder, objPassword, objFolder,FolderContent, fso, CleanPath, file, Flag

' Change this, foldername = local dir (where files must be ftp'd TO)
strLocalFolderName = "c:\Temp\test"

' Server where you are ftp'ing TO
strFTPServerName = "<servername>"

' Username you use to ftp
strLoginID = "<username>"

' Initialize variables
strPassword = ""
strFTPServerDir = ""

' Change this to the folder where the files are on the source server
strFTPServerFolder = "/tmp"

'The follow lines of code generate the FTP script file on the fly,
'because the directory name changes every time its run

strFTPScriptFileName = strLocalFolderName & "\FTPScript.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If (objFSO.FileExists(strFTPScriptFileName)) Then
    objFSO.DeleteFile(strFTPScriptFileName)
End If

Set objMyFile = objFSO.CreateTextFile(strFTPScriptFileName, True)
'objMyFile.WriteLine ("ftp -s open " & strFTPServerName)
objMyFile.WriteLine ("open " & strFTPServerName)
objMyFile.WriteLine (strLoginID)

strPassword = InputBox("Please enter your password:")
objMyFile.WriteLine (strPassword)

'strFTPServerDir = InputBox("Enter directory from which to ftp:")

'objMyFile.WriteLine ("cd " & strFTPServerFolder & strFTPServerDir)
objMyFile.WriteLine ("cd " & strFTPServerFolder)
'objMyFile.WriteLine ("bin")
objMyFile.WriteLine ("lcd " & strLocalFolderName)
objMyFile.WriteLine ("get myfile.txt")
objMyFile.WriteLine ("bye")
objMyFile.Close

Set objFSO = Nothing
Set objMyFile = Nothing

'The following code executes the FTP script. It creates a Shell
'object and run FTP program on top of it.
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run ("ftp -s:" & chr(34) & strFTPScriptFileName & chr(34))
'objShell.Run (strFTPScriptFileName & chr(34))
Set objShell = Nothing


Set fso=CreateObject("Scripting.FileSystemObject")
CleanPath="c:\temp\test"

For Each file In fso.GetFolder(strLocalFolderName).Files
Flag = StrComp(file, strFTPScriptFileName ,1)
if Flag = 0 then
   file.delete
end if
Next


It looks like the script deletes the FTPScript before actually running it. Is there a way to tell the script to wait for the FTP to finish before going and deleting the FTPScript?


Thanks,
J.
 
objShell.Run ("ftp -s:" & chr(34) & strFTPScriptFileName & chr(34))[!], , True[/!]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
In the file deletion part, you have to note that the path and filename are under full control of the scripter. Hence, [1] there is no reason to use matching as if the scripter has no full control of what to do. Just go straight to delete the file. [2] You do not need another instance of scripting.filesystemobject. Hence you can simply delay dumping objFSO and use it to delete the file. [3] The line [tt]Flag = StrComp([red]file[/red], strFTPScriptFileName ,1)[/tt] can be misleading just because the default property of file is file.path. [4] The cleanpath servers no purpose.

I would consider rewrite the last part of the script like this.
[tt]
'...etc...
[red]'[/red]Set objFSO = Nothing 'comment it out and re-use objFSO
'...etc...
'The .run section here...
'[red]this to replace the whole set fso ... section [/red]
[blue]objFSO.deletefile strFTPScriptFileName,true
set objFSO=nothing 're-insert it here[/blue]
[/tt]
 
you might consider these

'Set objPassword = CreateObject("ScriptPW.Password")
'Wscript.StdOut.Write "Please enter your password:"
'strPassword = objPassword.GetPassword()

Set PassDlg = CreateObject("PassDlg.PasswordDialog")
PassDlg.ShowDialog "Administrator password required"
AdminPassWrd = PassDlg.Password
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top