how to automatically download files from unix to windows?

Posted on 2010-08-15
Medium Priority
Last Modified: 2012-08-13
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  
                 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

Question by:SayYou_SayMe
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 32

Expert Comment

ID: 33441492
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("cd backups")
	stream.WriteLine("GET " & rFileName)
	stream.WriteLine("GET md5.txt")
	logstream.Write("Downloading today's backup...")
'Log in with FTP and download stuff
	WshShell.Run "C:\Windows\System32\ftp.exe -s:ftpscript.txt",,True
'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
'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"
		'They don't match!
		logstream.WriteLine("MATCH FAILURE!")
	End If


	'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.")

	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
		formatday = strDay
	End If
End Function
Function formatmonth(strMonth)
	If Int(strMonth) < 10 Then
		formatmonth = "0" & strMonth
		formatmonth = strMonth
	End If
End Function

Open in new window

LVL 32

Accepted Solution

DrDamnit earned 1000 total points
ID: 33441511
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.
'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
'                        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

        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

        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

        wscript.echo "Installation Complete."

Open in new window

LVL 32

Assisted Solution

DrDamnit earned 1000 total points
ID: 33441518
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.
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!


Author Comment

ID: 33441541
thanks DrDammnit,

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


Assisted Solution

Frozenice earned 500 total points
ID: 33441547
you can try this..

create.. from notepad

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

and there you go.
LVL 11

Assisted Solution

by:Pieter Jordaan
Pieter Jordaan earned 500 total points
ID: 33441558
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
lcd c:\
mget *

Open in new window


Author Comment

ID: 33441578
thanks guys


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

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

I am really confuse  

Author Comment

ID: 33441891
thanks guys

it works now

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Sometimes people don't understand why download speed shows differently for Windows than Linux.Specially, this article covers and shows the solution for throughput difference for Windows than a Linux machine. For this, I arranged a test scenario.I…
If you have done a reformat of your hard drive and proceeded to do a successful Windows XP installation, you may notice that a choice between two operating systems when you start up the machine. Here is how to get rid of this: Click Start Clic…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

719 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