Using FTP.exe to get unknown contents from a directory [duplicate] - batch-file

I want to create a batch file in Windows Server, including the following functions:
Connection to a FTP server
Copying the files from there (directory called "out") to a local directory
if success, then deleting the files from the FTP server
repeating those steps every 15 minutes
I haven't done that much with batch files so far, so it would be great if you could help me. I know there is the ftp command, and I know how to connect (ftp open), but unfortunately I don't know how to copy those files from there every 15 minutes.
Thanks a lot for your help!

To program ftp from a batch file, see http://support.microsoft.com/kb/96269. You need to call ftp like this
ftp -i -s:ftpcommands.txt
where ftpcommands.txt looks something like this:
open ftp.myftpsite.com
username
password
bin
cd out
mget *
del *
bye
For running this every 15 minutes, see other replies (at or Command Scheduler).
(The -i parameter is to turn off interactive prompting - the other way to do this is to add a prompt off command to the commands text file before the mget. Without this, mget will stop and ask you to confirm before getting each file. [Thanks to Adriano for pointing this out!])

The accepted answer by #AAT suggests using Windows built-in ftp.exe command-line client. While that can work, more often it won't, because this client does support FTP active mode only, which does not play nicely with today's ubiquitous firewalls and NATs. It also does not support encrypted FTPS (FTP over TLS/SSL).
If you have a problem with the above, you need to use a 3rd party FTP client. Most of them do support both the passive mode and encryption.
For example with WinSCP FTP client, you can use the following batch file (.bat):
WinSCP.com /command ^
"open ftp://username:password#ftp.example.com/" ^
"get /out/* c:\local\path\" ^
"exit"
In case you already have an ftp.exe script, there's a guide for converting it to WinSCP script.
For the scheduling part, see the guide to scheduling transfers to FTP server.
(I'm the author of WinSCP)

Windows has the at utility as well as the Windows task scheduler. Either one can run your program at a specified interval.

Using only one (1) .bat file script. Create the FTP script in a temporary file, run it, then delete the temporary file.
SET "FTPFILE=%TEMP%\myftpscript_%RANDOM%.txt"
ECHO>>"%FTPFILE%" open ftp.myftpsite.com
ECHO>>"%FTPFILE%" username
ECHO>>"%FTPFILE%" password
ECHO>>"%FTPFILE%" bin
ECHO>>"%FTPFILE%" cd out
ECHO>>"%FTPFILE%" mget *
ECHO>>"%FTPFILE%" del *
ECHO>>"%FTPFILE%" bye
ftp -i -s:"%FTPFILE%"
IF EXIST "%FTPFILE%" (DEL "%FTPFILE%")
EXIT /B 0

Related

How to check for directory existence before creating it on remote server? [duplicate]

Trying to automate via Jenkins: Transfer files from Local Windows directory to FTP directory via WinSCP console scripting.
Problem statement: Need to check whether a folder exists on FTP server before initiating upload.
This is the code I'm trying to execute via winscp /script=abc.txt
abc.txt as follows:
open ftp://xyz
mkdir /Server_folder/test_folder/ABC
put FROM_LOCAL_PATH TO_SERVER_PATH
bye
Since you clarified you are using WinSCP scripting console
option batch continue
mkdir some_dir
option batch abort

CMD, connect to SFTP with cmd command and copy file from there [duplicate]

I have program in Unix that generates files 8 files at 8:30 pm everyday.
I need to download the files into my Windows machine after the files are generated using WinSCP.
I can drag and drop those but its time consuming, I need to automate this process.
I cannot use .Net assembly to download those.
I have tried to use the get command, but its throwing error: Not an SCCS file.
Let me know how can I solve this.
--Thanks
To automate a task using WinSCP, use its scripting interface from a batch file (e.g. download.bat).
I assume you want to use SFTP, as you are connecting to a *nix server.
The simplest download batch file is like:
winscp.com /log=c:\path\to\log\winscp.log /command ^
"open sftp://username:password#example.com/ -hostkey=""xxx""" ^
"get /path/to/file.ext c:\path\to\download\to\" ^
"exit"
Replace the username, password and example.com with the actual connection details. Get the value of -hostkey switch from your GUI session. Use real paths.
Though it's easier to have WinSCP generate the batch file for you.
For details see a guide to automating file transfers from SFTP server using WinSCP.
Once you have the batch file working, schedule the the batch file execution using Windows scheduler.
See a guide to scheduling file transfers from SFTP server.

Batch command to check if FTP connection is successful

I am writing a batch command to send data via FTP. Before sending the actual data I need to find if the FTP server is active/running. How do I check that in batch command?
The server responds with "220 server ready" message when it is connected.
Do something like this:
YourFTPCommand | find /i /v "220 server ready" && goto :ServerNotReady
Explanation:
Pipe the output of your FTP command to FIND
Do a case insensitive (/i) search for output that does not contain (/v) the string "220 server ready"
Go to someplace if such a string is found (&&)
I do not think there's a reliable way to do this with Windows ftp.exe.
It blindly keeps running the commands, no matter if connection or previous commands succeeded.
It won't even report the result via exit code.
All you can do is to parse the ftp.exe output.
You should better use a 3rd-party FTP client.
For example with WinSCP scripting you can use:
#echo off
winscp.com /log=ftp.log /command ^
"open ftp://user:password#example.com/" ^
"put c:\local\path\file.txt" ^
"exit"
If connection (the open command) fails, WinSCP won't execute the following commands (the put).
See also Converting Windows FTP script to WinSCP FTP script.
(I'm the author of WinSCP)

Need help writing .bat file which changes all file permissions in a folder to Everyone

I appreciate your time so I won't take much of it. Here's what I'm trying to do: I want to create a .bat file that, when executed, will open the Command Prompt program, change directory to a folder (located on the C:), then execute a line of code that will change the file permission status of all files in that folder to Everyone. I intend to create a Windows task that will run this .bat file everyday. I understand how to setup the Windows task, but I can't figure out exactly how the .bat file should be written. Can anyone help?
Here's what I've got so far:
ECHO OFF
[Tab]Start "" C:\Windows\system32\cmd.exe
ECHO OFF
[Tab]Prompt cd cd:\google drive
ECHO OFF
[Tab]Prompt cacls *.* /t /e /g everyone:f
If you're interested, here's why I'm trying to create this .bat file:
I use a single Google Drive account on two file server computers, one in each of two offices. Each office has this file server and about 10 client computers. The client computers access files on the shared Google Drive folder (located on the file server) and occasionally add/edit/delete files. Google Drive does a great job of keeping all files synced between the two offices, but one problem I have is that if office A adds a file, the only computer in office B that can see it is the file server. I have to change the file permission to "Everyone" using the file server in office B before any of the client computers in office B can see the file. Over time, it's become very annoying to manually change the file permission every day and I'm looking for a shortcut. Please let me know if you can think of a better one.
If you got the commands right then this is a batch script with them.
The && will run the following command only if the cd command was successful.
#echo off
cd /d "c:\google drive" && cacls *.* /t /e /g everyone:f

Batch file when using WinSCP and command prompt

I am currently writing a script that calls WinSCP, connects an SFTP session, transfers a group of files from a local server to a remote server, closes the connection, then moves the local file to an archive. An alternate acceptable solution would be to copy the file to archive and then delete it from the source directory.
The problem I am having is that I can get WinSCP to open, connect and transfer the files, but after that the commands are ignored and I am unable to automate the connection close, file move or copy and the deletion and closing of command prompt. What is the best way to do this?
My FTP.bat file that connects the session and calls the script -
"C:\Program Files\WinSCP\WinSCP.exe" /console /command "lcd d:\bofa_ftp\out" /script=script.txt savedsession#winscpname.com
My script file that puts the file and closes the connection -
option batch abort
option confirm off
option exclude script.txt
put *.txt
close
When I add any commands to either the bat or the script they are ignored.
Commands after close are definitely not ignored. Your problem might be that you are trying to put Windows commands to WinSCP script. Note that there are no commands in WinSCP to move local files.
Instead of trying using Winscp3, try with plink.exe.
Plink is the best option to transfer files from winscp3 to your local.By using scp command we can transfer file from winscp to local as well as one folder in to another folder in winscp.
scp username#atechguideserver1.com:/data/tmp/samplscp.txt username#atechguideserver2.com:/data/tmp/dir/tech
Please refer to: http://99students.com/move-files-in-unix/

Resources