Batch Script to Upload a File to Your Server via FTP

us_flag_40x24.png

MVPinsiderLogo.jpg


BATCH SCRIPT TO UPLOAD A FILE TO YOUR SERVER VIA FTP

The following batch script uploads a file to a client server via FTP.

Simply follow the instructions below. I have personally tested this batch script via Sysnative FTP and all works well.

It's been tested on Windows 10, Windows 8.1 and Windows 7. It probably works on Vista too. You can try it on Vista as if it doesn't work, no harm will come to your system or FTP site.


Open up a Notepad; copy/paste the following into it:
Rich (BB code):
@echo off

:: Obtain today's date in format YYYYMMDD
set x1=%date:~10,4%
set x2=%date:~4,2%
set x3=%date:~7,2%

:: The CSV file is presumed to be in your Documents directory
set userdir=%userprofile%\documents

:: Build CSV file path + filename
set x=%userdir%\%x1%%x2%%x3%.csv

:: Echo the FTP commands to a text file - done so that ENV variables carry through
echo open ftp.sysnative.com>%windir%\system32\ftpcmdtemp.txt
echo Server User Name>>%windir%\system32\ftpcmdtemp.txt
echo Server Password>>%windir%\system32\ftpcmdtemp.txt
echo binary>>%windir%\system32\ftpcmdtemp.txt
echo cd Enter server directory path - including "public_html" if Linux>>%windir%\system32\ftpcmdtemp.txt
echo put %x%>>%windir%\system32\ftpcmdtemp.txt
echo quit>>%windir%\system32\ftpcmdtemp.txt

:: FTP Execution Commands to FTP the file up to the server
ftp -s:%windir%\system32\ftpcmdtemp.txt %x%


Save it in \windows\system32; filename = FTPup.bat

Change the items in RED to your personal FTP site info.

To execute the newly saved FTPup.bat - enter the batch file name (FTPup.bat) into an ADMIN CMD prompt screen or go into Windows Explorer, RIGHT-click on FTPup.bat; select "Run as Administrator"

You'll know that the CMD Prompt screen is an Admin screen as it will say "Administrator" in the screen title and the path will be \windows\system32 - see screenshot in spoiler -

Read More:


This particular batch script was written for an OP at TSF Forums.

The TSF post - Batch script to upload csv to website mysql - Tech Support Forum

Any questions, modifications or other assistance, please post in this thread and I will try my best to answer them.

Regards. . .

jcgriff2

EDIT: Make sure the username or password has no special characters in it as the batch script thinks they are something else. Many of the symbols above the number keys have a special meaning in batch. Many are "reserved words/characters" in batch. So, use letters + numbers only. The "forbidden characters" also include the 3 keys (upper and lower case) to the right of the letter "P" - the brackets and slash mark ( ( ) { } [ ] \ |) ) as well as the 2 keys to the right of the zero (0) key ( _ - + =)

For example - the ampersand (&) tells batch that the next word/phrase following it is a new command. So, if you use an ampersand in your password, what follows the ampersand is expected to be a new batch command. If it is not a new command, the batch script will fail (ABEND - Abnormal Termination) at that point immediately and give off an error stating that whatever is there following the ampersand is not a valid batch command.

To make things easier, I have copied the content of the CODE box, placed it into a batch file and attached the zipped BAT file to this post. Use Notepad or other text editor to edit the batch script (FTPup.bat file) and make the changes as directed.
 

Attachments

Last edited:
I copy all the scrip that you show above and I also change the items on Red but can't.

Any other step that I missing?
 
Did you change ftp.sysnative.com to your server ftp?
That's something I suspect you didn't change, because it's not in red.
 
Did you change ftp.sysnative.com to your server ftp?
That's something I suspect you didn't change, because it's not in red.
You're right.

I missed that line!

I just fixed it - it's in RED now.

Regards. . .

jcgriff2
 
@thiriglory - yes, please share your fix. It would greatly be appreciated.

Thank you. . .

jcgriff2
@jcgriff2

This is my test in powershell.

$sourceFilePath = "C:\Upload\test.Zip"
$siteAddress = "Test.jp"
$MyUserName = "test.co.jp";
$MyPassword = "password";
$webClient = New-Object System.Net.WebClient;
$webClient.Credentials = New-Object System.Net.NetworkCredential("$MyUserName", "$MyPassword");
("*** Uploading {0} file to {1} ***" -f ($sourceFilePath, $siteAddress) ) | write-host -ForegroundColor white -BackgroundColor Black
$webClient.UploadFileAsync($siteAddress,"PUT", $sourceFilePath);


Hope to ok !
 
Could you share your fix please?


This is my test in powershell.

$sourceFilePath = "C:\Upload\test.Zip"
$siteAddress = "Test.jp"
$MyUserName = "test.co.jp";
$MyPassword = "password";
$webClient = New-Object System.Net.WebClient;
$webClient.Credentials = New-Object System.Net.NetworkCredential("$MyUserName", "$MyPassword");
("*** Uploading {0} file to {1} ***" -f ($sourceFilePath, $siteAddress) ) | write-host -ForegroundColor white -BackgroundColor Black
$webClient.UploadFileAsync($siteAddress,"PUT", $sourceFilePath);


Hope to ok !
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top