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


SayYou_SayMeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DrDamnitCommented:
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

0
DrDamnitCommented:
This vbs script creates an FTP script on the fly, then saves it then uses ftp to download a specific file (or list if you so desire), then downloads the md5 hashes, compares them, and emails you to let you know that it was done sucessfully or not.

Here's how it works.

1. I dim all the vars.
2. I create several scripting objects (filesystem object, shell object, etc...)
3. I create the download script. This just creates the commands, in order, for the FTP program to process. You can change, add, remove commands here to suit your needs).
4. It logs into the ftp server to download stuff.
5. It runs an MD5 hash to compare the downloaded one to the original. This is where you can start deleting lines if you so please. It is more than you're asking for.
6. Next it moves the downloaded files to a backup directory. For you, this could be your distribution directory.
7. Last, it uses blat to send a log file (that it has been creating all along) to tell me what went on.

The functions at the bottom need to stay there or you might break the script.

To use the email function, you need to have a directory called C:\Util where you store blat.exe (See: http://www.blat.net/)

If you want to do the MD5 deal, you need to get gnucore utils and put it in C:\Util (at this point you might as well add C:\Util to the path).

I have attached a zip with a script to download and install core utils for you. It requires wget (Download from here: http://sourceforge.net/projects/gnuwin32/files/wget/1.11.4-1/wget-1.11.4-1-bin.zip/download).

I am pretty sure you feel like you just drank from the firehose by now, but let me know if you're having any issues, and I'll walk you through it.
'Dim vars
        dim x,t,m,filename
        dim wsh
        dim fso
        dim d
        dim args
        dim oFolder
        dim oFile
'Create objects
        set fso = WScript.CreateObject("Scripting.fileSystemObject")
        Set t = WScript.CreateObject("WScript.Shell")
        Set args = WScript.Arguments
        set wsh = WScript.CreateObject("WScript.Shell")
'Make sure we're in util.
        t.CurrentDirectory="C:\Util\"
'Get the file
        wscript.echo "Downloading Core Utils ..."
        t.run "wget http://gnuwin32.sourceforge.net/downlinks/coreutils-bin-zip.php -O coreutils.zip",1,true
        t.run "unzip -o coreutils.zip",1,true

        wscript.echo "Downloading Dependencies..."
        t.run "wget http://gnuwin32.sourceforge.net/downlinks/coreutils-dep-zip.php -O coredeps.zip",1,true
        t.run "unzip -o coredeps.zip",1,true

        wscript.echo "Downloading Grep..."
        t.run "wget http://downloads.sourceforge.net/gnuwin32/grep-2.5.4-bin.zip -O grep.zip",1,true
        t.run "unzip -o grep.zip",1,true

        wscript.echo "Downloading Grep Dependencies..."
        t.run "wget http://downloads.sourceforge.net/gnuwin32/grep-2.5.4-dep.zip -O grepdeps.zip",1,true
        t.run "unzip -o grepdeps.zip",1,true

        'Fix the su.exe read only bullshit
        t.run "attrib -r .\bin\su.exe"

        wscript.echo "Installing binaries and dependencies..."
        set oFolder = fso.GetFolder(".\bin")
        for each oFile in oFolder.Files
                dim target
                target = t.CurrentDirectory & "\" &oFile.Name
                wscript.echo "Checking to see if " & target & " exists..."
                if fso.FileExists(target) then
                        'stuff
'                        wscript.echo "File Exists. Removing in favor of the new file."
'                        fso.DeleteFile target
                end if
                        wscript.echo "Installing: " & oFile.Name
                        fso.CopyFile oFile.Path,target,1
        next

        wscript.echo "Cleaning UP..."
        dim FolderNames
        FolderNames = "bin,contrib,man,manifest,share"
        dim DelFolders
        DelFolders = split(FolderNames,",")
        for each x in DelFolders
                wscript.echo "Removing folder: " & x
                fso.DeleteFolder(x),1
        next

        dim FileNames
        FileNames = "coreutils.zip,coredeps.zip,grep.zip,grepdeps.zip"
        dim killList
        killList = split(FileNames,",")
        for each x in killList
                wscript.echo "Removing downloaded file: " & x
                fso.DeleteFile(x),1
        next

        wscript.echo "Installation Complete."

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DrDamnitCommented:
Oh... one last thing... once you get this working to solve the #1 part of your question, I'll post how to do the #2 part. It is SUPER easy once we get the #1 part setup.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

SayYou_SayMeAuthor Commented:
thanks DrDammnit,

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

0
FrozeniceCommented:
you can try this..

create.. from notepad
autoftp.bat

with the following codes.
@ftp -i -s:"%~f0"&GOTO:EOF
open example.com
username
password
!:--- FTP commands below here ---
lcd c:\MyLocalDirectory
cd  public_html/MyRemoteDirectory
binary
mput "*.*"
disconnect
bye




and there you go.
0
Pieter JordaanSenior Systems Administrator - Web hosting and Network Security.Commented:
That is serious overkill

Create a file called ftpdownload.scr
and add the attached source code to it, changing the lines where needed.


Then call it using
ftp -s:ftpdownload.scr

more info: http://support.microsoft.com/kb/96269
ftp ftp.myserver.com
user
username
password
lcd c:\
bin
mget *

Open in new window

0
SayYou_SayMeAuthor Commented:
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  
0
SayYou_SayMeAuthor Commented:
thanks guys

it works now
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.