Solved

Sample Windows 2008 batch script : test status of files transfer & retransfer if status fails

Posted on 2013-01-13
6
868 Views
Last Modified: 2013-01-27
In one new project, our Win 2008 (client) ftp scripts had been failing
intermittently & the network chaps can't isolate where on the
network is the 'disconnect' happening.  Sometimes, files at the
remote ftp server end are locked or not there yet (ie apps at remote
end delayed by a couple of minutes in  creating the data file).

In ncftp, there are various status codes that we can test for:
These are the errorlevel values returned by ncftpget:

    ncftpget returns the following exit values:

       0. Success.
       1. Could not connect to remote host.
       2. Could not connect to remote host - timed out.
       3. Transfer failed.
       4. Transfer failed - timed out.
       5. Directory change failed.
       6. Directory change failed - timed out.
       7. Malformed URL.
       8. Usage error.
       9. Error in login configuration file.
      10. Library initialization failed.
      11. Session initialization failed.

My customer does not allow us to use opensource tools but
only Windows native ftp client, thus I'll a way to test if a
file get (or mget)'s status & if it fails, to pause for 10 mins
(doing 'ping localhost -n 600 > nul' would do as 1 ping is
abt 1 sec) & attempt retransfer again & if it fails attempt
again a 3rd time.

Will need some sample Windows batch script as I don't
quite know the syntax to check the %errorlevel% for
ftp.
0
Comment
Question by:sunhux
[X]
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
  • 2
  • 2
  • 2
6 Comments
 
LVL 78

Assisted Solution

by:arnold
arnold earned 120 total points
ID: 38772690
You may want to explore using vbscript or c# and maybe powershell to script your FTP session which will provide you th control you are looking for.
Vbscript ref.
http://community.spiceworks.com/scripts/show/99-an-ftp-vbscript-class

A search will provide you with many options depending on which scripting/programming approach you are comfortable with.
0
 
LVL 81

Accepted Solution

by:
David Johnson, CD, MVP earned 380 total points
ID: 38773341
You may want to elaborate on this approach using powershell


function Get-FtpFile {
	<#
		.SYNOPSIS
			Retrieves A file from an FTP Server

		.DESCRIPTION
			Using a Powershell Module this script retrieves a file from an ftp server

		.PARAMETER  Username
			The Username required to login to the ftp server
	
		.PARAMETER  Password
			The Password associated with the Username
		
		.PARAMETER  FtpServer
			The url for the Ftp server i.e. ftp:\\ftp.example.com
		
		.PARAMETER  Serverpath
			The path to the file on the ftp server
		
		.PARAMETER  LocalPath
			Where you want the file saved
		.PARAMETER  Password
			The Password associated with the Username
		.PARAMETER  Password
			The Password associated with the Username

		.EXAMPLE
			PS C:\> Get-FtpFile -username USERNAME -password PASSWORD -ftpserver ftp:\\ftp.example.com -serverpath /abc/ -localpath c:\temp -filename fileame.txt

		.EXAMPLE
			PS C:\> Get-FtpFile -username USERNAME -password PASSWORD -ftpserver ftp:\\ftp.example.com -filename fileame.txt

		.NOTES
			Additional information about the function go here.

		
#>	
	param(
	[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] 	[System.String] $username,
	[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] 	[System.String] $password,
	[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] 	[System.String] $ftpserver,
	[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]  [System.String] $filename,
	[System.String] $serverpath,
	[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]  [System.String] $localpath
	)

Import-Module psftp
# get the module from http://gallery.technet.microsoft.com/scriptcenter/PowerShell-FTP-Client-db6fe0cb
#
# initiaize a few variables
#
$fileSize = "0"
$remotefileSize = "0"

$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
#connect to ftp server

Set-FTPConnection -Credentials $credentials -Server $FtpServer -UseBinary

#
#  Test to see if the file exists by getting the file size by name.
#  If a -1 is returned, the file does not exist.

$remotefileSize = Get-FTPItemSize -Path $filename
if ($remotefileSize -lt 0) { $("file does not exist") }
else {     $("The file exists and is " + $remotefileSize + " bytes in size")   }
#
# transfer the file
#
Get-FTPChildItem -path $filename -Recurse | Get-FTPItem -localpath $localpath -RecreateFolders 
#
# check recieved file is the same size as the remote filesize
#
$filesize = Get-childItem  -Path $localpath -Filter $filename
$localfilesize = $fileSize.length
}

if ($localfilesize -ne $remotefileSize) {
Write-Output("The Remote and Local Files don't have the same length")
return 3
}

}

Open in new window

0
 

Author Comment

by:sunhux
ID: 38774641
Bear with me as I'm totally new to PowerShell:

"Import-Module psftp" : does this mean this uses psftp or it's ftp?

Does Win 2003 Enterprise (32bit) comes with PowerShell?
0
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

by:sunhux
ID: 38774784
Btw, how do I execute the Powershell script?

Can it do the equivalent of ftp 'mget' ?
0
 
LVL 78

Expert Comment

by:arnold
ID: 38775089
To get the control you want in determining what is going on, relying on a script provides you with a larger level of control.

powershell is available depending on whether you deployed it using WSUS or installed it as part of a windows update.

powershell get-help

Are you familiar with vbscript or c#?
0
 
LVL 81

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 380 total points
ID: 38776570
1. Yes it uses the module psftp
2. you would get a listing of files and then use a for/each loop to retrieve each file individually
3. you would need powershell installed. it is part of winrm http://support.microsoft.com/kb/968929?wa=wsignin1.0
4. c:\windows\system32\windowspowershell\1.0\powershell -noprofile <path>\scriptname.ps1
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
BATCH to EXE Converter 2 99
Exchange Server 2013 Message Tracking log Powershell ? 11 114
Remote login in windows 7 8 73
powershell script error 2 36
The password reset disk is often mentioned as the best solution to deal with the lost Windows password problem. In Windows 2008, 7, Vista and XP, a password reset disk can be easily created. But besides Windows 7/Vista/XP, Windows Server 2008 and ot…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

751 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