how to automatically download files from unix to windows?

Posted on 2010-08-15
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
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("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\\ " & 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 -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 250 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:

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:

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 ..." "wget -O",1,true "unzip -o",1,true

        wscript.echo "Downloading Dependencies..." "wget -O",1,true "unzip -o",1,true

        wscript.echo "Downloading Grep..." "wget -O",1,true "unzip -o",1,true

        wscript.echo "Downloading Grep Dependencies..." "wget -O",1,true "unzip -o",1,true

        'Fix the su.exe read only bullshit "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 = ",,,"
        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 250 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.
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)


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 125 total points
ID: 33441547
you can try this..

create.. from notepad

with the following codes.
@ftp -i -s:"%~f0"&GOTO:EOF
!:--- 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 125 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:
lcd c:\
mget *

Open in new window


Author Comment

ID: 33441578
thanks guys


is it correct? based on your solution
open , 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XP driver for Dell Color MFP H625cdw printer needed 7 191
Reading registry key from HKCU and not hklm 10 129
shortcuts on desktops 14 78
aix unix tar error 3 82
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

730 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