[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to read a text file through VBS

Posted on 2011-10-23
6
Medium Priority
?
545 Views
Last Modified: 2012-05-12
Hi All i have a script which is uploading a single file on to ftp, but i want to put multiple files on to ftp.

how can i read the text file in which i can add item which i need to upload to ftp server?

i have attach  script which i am using

thank for your help
strFTPServer = "IPADDRESS"
strUsername = "USERNAME"
strPassword = "PASSWORD"
strFileToUpload = "C:\Users\Administrator\Desktop"
Set objNetwork = CreateObject("WScript.Network")
strFolderToCreate = Year(Date) & Right("0" & Month(Date), 2) & Right("0" & Day(Date), 2) & "_" & objNetwork.ComputerName

Set objFSO = CreateObject("Scripting.FileSystemObject")
strFileToUpload = objFSO.GetFile(strFileToUpload).ShortPath
strFTPScript = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "FTPCommands.txt"
Set objFTP = objFSO.CreateTextFile(strFTPScript, True)
objFTP.WriteLine "open " & strFTPServer
objFTP.WriteLine strUsername
objFTP.WriteLine strPassword
objFTP.WriteLine "cd DIRECTORY"
objFTP.WriteLine "mkdir " & strFolderToCreate
objFTP.WriteLine "cd " & strFolderToCreate
objFTP.WriteLine "put " & strFileToUpload
objFTP.WriteLine "bye"
objFTP.Close
strFTPScript = objFSO.GetFile(strFTPScript).ShortPath

Set objShell = CreateObject("WScript.Shell")
objShell.Run "cmd /k ftp -s:" & strFTPScript, 1, True

objFSO.DeleteFile strFTPScript, True

Open in new window

0
Comment
Question by:ammartahir1978
  • 3
  • 2
6 Comments
 
LVL 6

Accepted Solution

by:
rushtoshankar earned 1000 total points
ID: 37014279
Hope this helps.

 
strFTPServer = "IPADDRESS"
strUsername = "USERNAME"
strPassword = "PASSWORD"
strFileToUpload = "C:\Users\Administrator\Desktop"
Set objNetwork = CreateObject("WScript.Network")
strFolderToCreate = Year(Date) & Right("0" & Month(Date), 2) & Right("0" & Day(Date), 2) & "_" & objNetwork.ComputerName

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("D:\ListOfFiles.txt", ForReading)

'strFileToUpload = objFSO.GetFile(strFileToUpload).ShortPath
strFTPScript = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "FTPCommands.txt"
Set objFTP = objFSO.CreateTextFile(strFTPScript, True)
objFTP.WriteLine "open " & strFTPServer
objFTP.WriteLine strUsername
objFTP.WriteLine strPassword
objFTP.WriteLine "cd DIRECTORY"
objFTP.WriteLine "mkdir " & strFolderToCreate
objFTP.WriteLine "cd " & strFolderToCreate

Do Until objFile.AtEndOfStream
objFTP.WriteLine "put " & objFile.ReadLine
Loop
objFile.Close

objFTP.WriteLine "bye"
objFTP.Close
strFTPScript = objFSO.GetFile(strFTPScript).ShortPath

Set objShell = CreateObject("WScript.Shell")
objShell.Run "cmd /k ftp -s:" & strFTPScript, 1, True

objFSO.DeleteFile strFTPScript, True

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37014324
Or even use the batch script version I gave you in your last Q:

Have added "prompt" command to stop it asking overwrite if needed too.

This new line just say to run down the lines in the file shown and add the line "put filename" for each:
for /f "delims=" %%a in ('Type %filelist%') do echo put %%a

@echo off
set FTPServer=IPADDRESS
set Username=USERNAME
set Password=PASSWORD
set LocalDir=C:\Users\Administrator\Desktop
set RemoteDir=street/Windows
set FileList="filelist.txt"

REM Get date in form yyyymmdd_computername
echo wscript.echo Year(Date) ^& Right("0" ^& Month(Date), 2) ^& Right("0" ^& Day(Date), 2) ^& "_%computername%" > "%temp%\getdate.vbs
for /f "tokens=*" %%a in ('cscript //nologo "%temp%\getdate.vbs"') do set yyyymmdd=%%a

(echo open %FTPServer%
echo user %UserName%
echo %Password%
echo prompt
echo lcd %LocalDir%
echo cd %RemoteDir%
md %yyyymmdd%
cd %yyyymmdd%
for /f "delims=" %%a in ('Type %filelist%') do echo put %%a
echo quit
) | ftp -n -i
 

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37014327
It just seems silly to me like I say running a VBScript to make a text file to call ftp.exe rather than just doing it all with one ftp command in batch file?

Steve
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ammartahir1978
ID: 37051124
hi Dragon-it,

how can i your script run?

can you please give step by step instructions?

Thank you so much

thank you for your reply rushtoshankar i can see you added listoffile.txt in the vbs, so can i just put different files in that txt file on each line and also the files which are on differnet location then current computer?
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 1000 total points
ID: 37051246
OK.  Save the code below as MyFTP.cmd or something in notepad.  Save As, select All files otherwise it adds .TXT

Amend the set lines for ip, username, password, dir to copy from and dir to copy to if needed.
Amend the filelist line to point to a file containing the filenames to copy, one per line.
Start cmd.exe prompt and run the myftp.cmd
or double click in explorer, or schedule it etc.

Amend the files in "fileslist.txt" and it will send those files instead.

Your VBScript at the moment goes away and creates a text file which is then sent to ftp.exe to execute.  My Cmd file does away with that, it just needs the one .cmd file and your list of files.

hth

Steve
@echo off
set FTPServer=IPADDRESS
set Username=USERNAME
set Password=PASSWORD
set LocalDir=C:\Users\Administrator\Desktop
set RemoteDir=street/Windows
set FileList="filelist.txt"

REM Get date in form yyyymmdd_computername
echo wscript.echo Year(Date) ^& Right("0" ^& Month(Date), 2) ^& Right("0" ^& Day(Date), 2) ^& "_%computername%" > "%temp%\getdate.vbs
for /f "tokens=*" %%a in ('cscript //nologo "%temp%\getdate.vbs"') do set yyyymmdd=%%a

(echo open %FTPServer%
echo user %UserName% 
echo %Password%
echo prompt
echo lcd %LocalDir%
echo cd %RemoteDir%
md %yyyymmdd%
cd %yyyymmdd%
for /f "delims=" %%a in ('Type %filelist%') do echo put %%a
echo quit
) | ftp -n -i

Open in new window

0
 

Author Closing Comment

by:ammartahir1978
ID: 37100834
excellent
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question