Friend called me: We have a website, we need to upload our price list to our website ftp hourly.
How do I upload files to the ftp site using a windows command line?
Manage and Report Active Directory, Exchange and Microsoft 365 with ManageEngine ADManager Plus. Download Free Trial!
His requirements were simple. He is using a windows server.
Hourly batch exports products and prices to xml with random name.
XML file has to be uploaded to the ftp site on a hourly basis.
While powershell is very popular and can be used for a lot of things, there is still option to use “old” method with ftp command integrated in Windows server.
How to automate upload to a FTP site with command line from Windows
To use ftp in a batch file, you have to create two files. First file is a batch file ending with .bat, you can run it manually or you can add it to a scheduled tasks. First file calls the scond file using a ftp command. The second file is used to tell what ftp operations we would like to do.
Because first file is a batch file, we can also use it to do other file operations like renaming the file, moving the file and also deleting the file, we can use it in cunjuction with FTP.
In my case, I would like to rename xml file first, then I would like to transfer xml file via FTP to the remote server and in the end, I would like to delete XML file. I need to repeat this every hour, so I will use scheduled tasks in a Windows for automation.
Create first file to rename xml and call a file with FTP commands
Create file 1.bat (you can use notepad)
copy content below in the file:
ren *.* nameofthefile.xml
ftp -v -i -s:c:\script\ftp.bat
Lets take a look what each line means:
cd c:\dirnameofxml\ (go to the folder where .xml file is being exported to)
ren *.* nameofthefile.xml (rename random filename.xml to pricelist.xml)
ftp -v -i -s:c:\script\ftp.bat (call second file with ftp commands to put file to FTP)
del pricelist.xml (delete file pricelist.xml, since this command is the last, it will delete xml file after it will place it to ftp)
Parameters that can be used with a FTP command in the first file:
ftp -s command is used in combination with a file. You have to list a filename with FTP commands like on example above
To use it properly you would type ftp -s:filename to point it to the file with commands.
ftp -n -v are both used to suppress the message.
copy content below into the file
Lets take a look at the second script
open “mywebsite.com” (command open will connect to your website)
myusername (this is your username)
mypassword (this is your password)
lcd C:\dirnameofxml\ (go to folder where you have xml file)
put “pricelist.xml” (place pricelist.xml to ftp)
bye (disconnect session)
Run the script, if all works like it should, go to System tools, Scheduled jobs and create new Scheduled task, set it up to run every 20 min. and you will be fine.
Leave a Reply