I want to find if a file exists at FTP using if-exist filename -else statement using FTP batch script which is as follows:
ftp.txt open ftp.mysite.com
ftp.txt username
ftp.txt password
ftp.txt if exist filename (echo file exists) else (echo file doesn't exist)
ftp.txt quit
ftp -s:ftp.txt
the if-exist line above does not work.
Is there any other way to search?
Don't do the logic in the FTP script.
Call the ftp.txt script from a batch file. Within your ftp.txt script, just do a GET on your file. If the file is there, it'll be downloaded to the local directory. Otherwise, it won't. After calling the FTP script, check the file's existence in your local directory using standard DOS batch commands, i.e.:
#echo off
:FETCHFILE
ftp -s:ftp.txt
IF EXIST filetocheckfor.txt (
REM the file was there, so do something
) ELSE
echo Trying again...
REM ping "sleep" would go here
GOTO FETCHFILE
)
If you want to build a delay into your retries, perform a "sleep" by pinging a bogus IP address, as described in this post:
http://www.dullsharpness.com/2010/06/14/elapsed-timer-using-pure-ms-dos/
Related
I'm trying to upload latest files generated in one hour to a remote server. But the script I wrote is intelligent enough to gather only the latest file in directory and upload it to FTP. Any chance of set the variable type as array and upload the files in the array?
My FTP batch file:
FOR /F %%I IN ('DIR "abcdef*.bac" /B /O:D') DO SET latest_file=%%I
echo user domain/username> ftp.txt
echo password>> ftp.txt
echo cd remotepath>> ftp.txt
echo put %latest_file%>>ftp.txt
echo quit>> ftp.txt
ftp -n -s ftp.txt Servername>ftp_logs.txt
del ftp.txt
It would be difficult to write this in a pure batch file code.
You better use some more powerful tool.
For example with WinSCP FTP client, it's trivial:
winscp.com /ini=nul /log=upload.log /command ^
"open ftp://username:password#ftp.example.com/" ^
"put -filemask=>1H C:\local\path\abcdef*.bac /remote/path/" ^
"exit"
References:
Automate file transfers to FTP server with WinSCP;
Converting Windows FTP script to WinSCP script;
File masks with time constraints.
(I'm the author of WinSCP)
I've a batch file that is opening a FTP connection to a server and putting a file on a specified location.
Here is how my ftpConnection.bat file look like..
open HOST
FTP_USER_NAME
FTP_USER_PASSWD
cd TO_DIR
lcd TO_FILE_LOCATION
put THE_FILE
quit
and from command prompt if i run it like this ftp -i -s:ftpConnection.bat it works fine.
My requirement is to pass HOST, USER_NAME and PASSWORD as argument
so i tried to use %1 %2 and %3 but this is not working for me.
Passing the argument like this
C:\Users\xxx\Desktop>ftp -i -s:ftpConnection.bat "HOST" "USER_NAME" "PASSWORD"
also tried without the quotes but result is same, it'S showing
Transfers files to and from a computer running an FTP server service
(sometimes called a daemon). Ftp can be used interactively.
FTP [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-A] [-x:sendbuffer] [-r:recvbuf
fer] [-b:asyncbuffers] [-w:windowsize] [host]
Followed and tried few document like How to pass multiple parameters in CMD to batch file and Batch file command line arguments
they suggested to use set and i tried it like below but result is same.
set host=%1
set uname=%2
set passwd=%3
open %1
%2
%3
Can anyone suggest me what i am doing wrong or any pointer to achieve this.
Thanks in advance.
Sorry, but you are using a text file with input and commands expected and interpreted by ftp command. This is not a windows batch file and can not use its syntax.
To use the required data as arguments in the call, you need a batch file that will generate the ftp script from the passed arguments
#echo off
setlocal enableextensions disabledelayedexpansion
if "%~3"=="" goto :eof
set "TO_DIR=......"
set "TO_FILE_LOCATION=......"
set "THE_FILE=........"
> ftpScript (
echo open %~1
echo %~2
echo %~3
echo cd %TO_DIR%
echo lcd %TO_FILE_LOCATION%
echo put %THE_FILE%
echo quit
)
ftp -i -s:ftpScript
Now, you have a batch file (let's call it doFTP.cmd), that you can call as
doFTP.cmd ftpserver userName pa$$word
The arguments in the command line are stored in the %1, %2 and %3 variables (%~1 is first argument without surrounding quotes if present) and used inside the batch file to generate (the block of echo commands are redirected to the ftpScript file) the text file that ftp will handle.
based on your use it is like
set host=%1
set uname=%2
set passwd=%3
open %host%
%username%
%passwd%
you can use the set variable back by enclosing between two %var-name% as i shown above
please give it a try
I am trying to ftp a text file from a Unix machine to a Windows machine. In the process, the data is getting mangled. I want to try transfering the file in binary instead of ASCII - hopefully that will make the data pass thru without getting corrupted.
How can I modify the following script to make the .txt file transfer as a binary file, instead of ASCII? I am not familiar with the syntax.
#echo off
setlocal
set uname=john
set passw=password
set hostname=johncomputer
set filespec=SampleSPEC
echo %uname%> name.ftp
echo %passw%>> name.ftp
echo cd CRMD>> name.ftp
echo get %filespec%>> name.ftp
echo bye>> name.ftp
ftp -s:name.ftp %hostname%
if errorlevel 1 pause
endlocal
Try: echo TYPE I >> name.ftp before the line that adds the get command.
I need to write a single dos script which will first FTP from a remote server to my local directory and then will copy these files to another folder within the local machine. The reason behind the second step is that the destination folder path is based on whether the SYSTEM Processor of 32-bit or 64-bit.
I have two separate scripts which are working fine now. But when I am putting them together in a single script, it is failing with the ftp command prompt. Here is the script which I am using -
#echo off
:MAIN
SETLOCAL ENABLEDELAYEDEXPANSION
set winver=%PROCESSOR_ARCHITECTURE%
rmdir c:\batch\temp
mkdir c:\batch\temp
goto :ftpbegin
:ftpbegin
#ftp -i -s:"%~f0"&GOTO:EOF
open x.y.z.a
<userid>
<password>
!:-----FTP commands here -----
lcd C:\batch\temp
cd CGServices\Uploads
binary
mget "*.psl"
mget "*.PST"
mget "*.psy"
get abc.ini
get def.pmd
disconnect
bye
:eof
exit /b
:findwin
if %winver% ==x86 (goto :copywin32) else (goto :copywin64)
:copywin32
echo "inside copywin32"
<do the copy files here>
:copywin64
<do copy files here>
exit /b 0
However, the ftp script seems to cause a break while executing the second part of my program since it is calling the program in a loop in ftp prompt. So, none of the dos commands are translated on FTP prompt.
Any help on how to achieve this in a single script for this is highly appreciated.
thanks,
Sanders
The FTP command AFAIK cannot process the commands from the command line without reading the ftp commands from an external file. I usually download a windows version of wget, and it works well for us, perhaps you would like to have a look at that.
Here is the manual for wget http://www.editcorp.com/Personal/Lars_Appel/wget/v1/wget_7.html
for example
wget ftp://<user>:<password>#server.com/upload/file.ext
After invoking ftp command you tell your script to jump to ':EOF'. You can not use :EOF label for jumping inside a batch file. Goto :EOF basically means "jump to the end of the file", which is generally the same as calling exit /b. You can also call goto :EOF from inside a subroutine. In this case it mean "this is the end of the subroutine". For more info, see goto /? and call /?
Because in ftp.exe you are using your .bat file as a ftp command script, all the lines before open x.y.z.a will give you invalid command messages, so it would be good to minimize the number of lines before FTP command block.
Your script should look like this:
#echo off
goto main
:ftpbegin
#ftp -i -s:"%~f0" & goto ftpend
open x.y.z.a
<userid>
<password>
!:-----FTP commands here -----
lcd C:\batch\temp
cd CGServices\Uploads
binary
mget "*.psl"
mget "*.PST"
mget "*.psy"
get abc.ini
get def.pmd
disconnect
bye
:main
SETLOCAL ENABLEDELAYEDEXPANSION
set winver=%PROCESSOR_ARCHITECTURE%
rmdir c:\batch\temp
mkdir c:\batch\temp
goto ftpbegin
:ftpend
if not "%winver%"=="x86" goto copywin64
REM did not jump, so this is a 32-bit system
echo "inside copywin32"
<do copy files here>
goto finish
:copywin64
echo "inside copywin64"
<do copy files here>
:finish
endlocal
I have a somewhat related, but different questions here.
I have a batch script (*.bat file) such as this:
#ftp -i -s:"%~f0"&GOTO:EOF
open ftp.myhost.com
myuser
mypassword
!:--- FTP commands below here ---
lcd "C:\myfolder"
cd /testdir
binary
put "myfile.zip"
disconnect
bye
Basically this is a script that uploads a zip file to a ftp site. My question is that, the upload operation can fail from time to time ( the remote ftp is not available, "myfile.zip" is non-existent, upload operation interrupted and whatnot), and when such unfortunate things happen, I want my bat file return 1 ( exit 1).
It would be great if my upload wasn't successful, the ftp would throw an exception ( yes, like exception in C++), and I would have a catch-all exception that catches it and then exit 1, but I don' think this is available in batch script.
What is the best way to do what I need here?
You can redirect the output to a log file and when the ftp session is finished the file can be parsed.
#ftp -i -s:"%~f0" > log.txt & GOTO :parse
open ftp.myhost.com
myuser
mypassword
!:--- FTP commands below here ---
lcd "C:\myfolder"
cd /testdir
binary
put "myfile.zip"
disconnect
bye
:parse
for /F "delims=" %%L in (log.txt) Do (
... parse each line
)
Windows FTP does not return any codes.
I suggest running a batch file that echo your ftp commands to a input response file, then use this file as input to the ftp command, redirecting stderr to a file and verifying the file size. something like this
echo open ftp.myhost.com >ftpscript.txt
echo myuser >>ftpscript.txt
echo mypassword >>ftpscript.txt
echo lcd "C:\myfolder" >>ftpscript.txt
echo cd /testdir >>ftpscript.txt
echo binary >>ftpscript.txt
echo put "myfile.zip" >>ftpscript.txt
echo disconnect >>ftpscript.txt
echo bye >>ftpscript.txt
ftp -i -s:ftpscript.txt >ftpstdout.txt 2>ftpstderr.txt
rem check the ftp error file size, if 0 bytes in length then there was no erros
forfiles /p . /m ftpstderr.txt /c "cmd /c if #fsize EQU 0 del /q ftpstderr.txt"
if EXIST ftpstderr.txt (
exit 1
)
Your only option in batch files that I know of is to use the "IF ERRORLEVEL" syntax, which requires your ftp client to return a non-zero error code.
http://www.robvanderwoude.com/errorlevel.php is a good reference guide.
Unfortunately I do not if the standard Windows ftp client returns non-zero error codes, so you may have to code your own if this is a requirement. This link suggests that it does not return an error code, but provides a work around albeit clunky, by redirecting the output to a file and using the FIND command to return an error code.