Link to home
Start Free TrialLog in
Avatar of SayYou_SayMe
SayYou_SayMe

asked on

how to automatically download files from unix to windows?

we have 2 files A.txt and B.txt at /unix/folder,every week we have to load them to \windows\folder, and send them to users.

usually, I   ftp unixserver
                 cd  /unix/folder  
                 prompt
                 mget *.txt "\windows\folder\*.txt"

question :
1. how to code a bat file ,when  i want to download the A.txt and B.txt , just double click the bat file?
2. how to code a bat file to upload the a specific file to unix environment or download a specific file to windows? it means give the file name as parameter.

loginID: unixuser
password: unixpass


Avatar of DrDamnit
DrDamnit
Flag of United States of America image

Please see the attached script. It may be a little more heavyweight than you need (this also compares md5 hashes), but we can remove that if you wish. I will post a second commend explaining how it works.
'Remote tools installation script

'Dim Vars
    dim WshShell
    Dim fso
    Dim hFile
    Dim WshEnv
    Dim args
    Dim rFileName 'Holds the remote file name.
    Dim ftpfile
    Dim stream
    Dim command
    Dim logstream
    Dim emailSubject
'Initialize Vars
    Set args = WScript.Arguments
    Set fso = CreateObject("Scripting.fileSystemObject")
	Set WshShell = WScript.CreateObject("Wscript.Shell")
	Set WshEnv = WshShell.Environment("SYSTEM")
	'Set ftpfile = fso.GetFile("ftpscript.txt")
	'Set stream = ftpfile.OpenAsTextStream(2) '1=Read, 2=Write, 8=Append
	Set stream = fso.CreateTextFile("ftpscript.txt",True)
	Set logstream = fso.CreateTextFile("log.txt",True)
'Create download ftp script.
	logstream.WriteLine("Backup download process beginning...")
	logstream.Write("Creating FTP Download script...")
	rFileName = "backup-" & formatmonth(Month(Now)) & "-" & formatday(Day(Now)) & "-" & Right(Year(Now),2) & ".tgz"
	stream.WriteLine("open www.yourdomain.com")
	stream.WriteLine("user")
	stream.WriteLine("pass")
	stream.WriteLine("cd backups")
	stream.WriteLine("BINARY")
	stream.WriteLine("GET " & rFileName)
	stream.WriteLine("GET md5.txt")
	stream.WriteLine("Quit")
	stream.Close
	logstream.WriteLine("[OK]")
	logstream.Write("Downloading today's backup...")
'Log in with FTP and download stuff
	WshShell.Run "C:\Windows\System32\ftp.exe -s:ftpscript.txt",,True
	logstream.WriteLine("[OK]")
'Run MD5 hash on downloaded file
	logstream.Write("Hashing downloaded file...")
	command = "%comspec% /c C:\Util\md5sum.exe " & rFileName & " > mymd5.txt"
	WshShell.Run command,0,True
	logstream.WriteLine("[OK]")
'Compare Hashes.
	logstream.Write("Comparing hashes...")
	Dim h1 'stream for hash1 file
	Dim h2 'stream for hash2 file
	Set h1 = fso.OpenTextFile("md5.txt")
	Set h2 = fso.OpenTextFile("mymd5.txt")

	'Get the original hash from hash1 file
	Dim buffer
	buffer = h1.ReadLine()
	Dim ary
	ary = Split(buffer," ")
	Dim hash1
	hash1 = ary(0)

	'Get the new hash from hash2 file
	buffer = h2.ReadLine()
	ary = Split(buffer," ")
	Dim hash2
	hash2 = ary(0)

	If hash1 = hash2 Then
		'they match!
		logstream.WriteLine("THEY MATCH")
		emailSubject = "DevServer SUCCESSFUL backup"
	Else
		'They don't match!
		logstream.WriteLine("MATCH FAILURE!")
		emailSubject = "BACKUP FAILED FOR DEVSERVER"
	End If

	logstream.WriteLine(hash1)
	logstream.WriteLine(hash2)

	'Move the files to the E drive.
	command = "%comspec% /c robocopy . F:\Backups\dev.yourdomain.com\ " & rFileName & " /move"
	WshShell.Run command,0,True

	logstream.WriteLine("Sending you this log file. Over and out.")
	logstream.Close

	command = "%comspec% /c C:\Util\blat.exe log.txt -to michael@yourdomain.com -subject """ & emailSubject & ""
	WshShell.Run command,0,True




Function formatday(strDay)
	If Int(strDay) < 10 Then
		formatday = "0" & strDay
	Else
		formatday = strDay
	End If
End Function
Function formatmonth(strMonth)
	If Int(strMonth) < 10 Then
		formatmonth = "0" & strMonth
	Else
		formatmonth = strMonth
	End If
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of DrDamnit
DrDamnit
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SayYou_SayMe
SayYou_SayMe

ASKER

thanks DrDammnit,

but it is a little bit complicated for me,
is there any other easy way to handle this?

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks guys

Frozenice-

is it correct? based on your solution
open example.com , what does it mean?

autoftp.bat
@ftp -i -s:"%~f0"&GOTO:EOF
open(what is it?)
unixuser
unixpass
lcd c:\windows\folder
cd /unix/folder
binary
mput "*.*"
disconnect
bye



I am really confuse  
thanks guys

it works now