FTP upload with ANT

2015/03/26

Problem: After a build, you may want to upload the deploy-able files to an FTP server.
Here’s how to upload, let’s say, some SQL update scripts to the FTP server, with ANT:

Ant Ftp Upload

We test to see if the property ftp.upload.sqlUpdates.folder is set; the value for it will contain the folder from where to do the upload of the SQL scripts; this property can be set in any property file that should be updated per each build.

Also, for this to work, you’ll need to add to your ANT library or MAVEN dependencies 2 new JAR files:
Jakarta Oro
Commons Net

Ant Ftp settings

As for the rest, the properties for the FTP (server, user, password, upload folder) can be declared in a simple property file, which should not be included in the final build.

Because I’m lazy and/or I don’t have time to format the code, above is a screenshot for clarity, below is the code for whoever needs it.

    <target name=”ftp-sql-upload.test.if.set”>
        <condition property=”ftp-sql-upload.is.set” value=”true” else=”false”>
            <not>
                <equals arg1=”${ftp.upload.sqlUpdates.folder}” arg2=””
                    forcestring=”true” />
            </not>
        </condition>
    </target>
    
    <target name=”ftp-sql-upload”
            depends=”ftp-sql-upload.test.if.set”
            if=”${ftp-sql-upload.is.set}”>
        <ftp
            server=”${ftp.upload.server}” remotedir=”${ftp.upload.remote.dir}”
            userid=”${ftp.upload.user}” password=”${ftp.upload.password}”
            action=”send” verbose=”yes” depends=”yes”>
            <fileset dir=”${ftp.upload.sqlUpdates.folder}”>
                <include name=”**/*.sql” />
            </fileset>
        </ftp>
    </target>

Note 1: Credits go to StackOverflow, IT blogs and alike, sites that helped me with the code when searching for solutions.
Note 2: Be aware that when copy-pasting, the quotes and possibly other characters get messed up by WordPress, you’ll have to replace them.


PowerShell – Get FTP folder file list

2015/03/18

PowerShell - Get FTP folder list

Because I’m lazy and/or I don’t have time to format the code, above is a screenshot for clarity, below is the code for whoever needs it.

function Get-FtpFolderFileList([string]$ftpSourceFolder, [string]$ftpUser, [string]$ftpPassword)
{
    [System.Net.FtpWebRequest]$ftp = [System.Net.WebRequest]::Create($ftpSourceFolder)
    $ftp.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
    $ftp.Credentials = New-Object System.Net.NetworkCredential($ftpUser, $ftpPassword)
    
    $response = $ftp.GetResponse()
    $stream = $response.GetResponseStream()
    
    $buffer = new-object System.Byte[] 1024
    $encoding = new-object System.Text.UTF8Encoding
    $outputBuffer = “”
    $foundMore = $false
    do
    {
        start-sleep -m 1000
        $foundmore = $false
        $stream.ReadTimeout = 1000
        do
        {
            try
            {
                $read = $stream.Read($buffer, 0, 1024)
                if ($read -gt 0)
                {
                    $foundmore = $true
                    $outputBuffer += ($encoding.GetString($buffer, 0, $read))
                }
            } catch { $foundMore = $false; $read = 0 }
        } while ($read -gt 0)
    } while ($foundmore)
    $fileList = $outputBuffer.Replace(“`r`n”, “`n”).Replace(“`r”, “`n”).Split(“`n”)
    return $fileList
}

In my case, the $ftpSourceFolder variable had to be like

ftp://my-ftp-server.com/%2f/some-inner-folder-on-ftp/some-sub-folder-maybe

The symbol %2f it’s an escape slash, and it’s needed when separating the FTP name from the folder with the content.

The files will be retrieved in the short format, only name and extension, but that should not be the problem if you already have the FTP subfolder name


Note 1: Credits go to StackOverflow, IT blogs and alike, sites that helped me with the code when searching for solutions.
Note 2: Be aware that when copy-pasting, the quotes and possibly other characters get messed up by WordPress, you’ll have to replace them.


%d bloggers like this: