1.

Solve : FTP in a Batch file?

Answer»

HI Guys,

Here is my question:

I need a .bat that ftp to a Pc, and UPLOAD a file.
Actually i don't know which is the command for use ftp automatically inside a batch file.
Also the "get" command, for PUT files is a mistery for me.

I already checked the old Topics, but i couldn't find something appropriate.

Thanks for help,
Have fun,
Riccardo if you type ftp -h on the cmd line , you can see an option -s:filename
you can create a file with all the ftp commands you need, save it, then CALL ftp -s:filename from inside your batch file.Hi gosthdog74,

what's the syntax for write the command inside the .txt file?

when i lunch my .bat i would connect via Ftp, how should i write the ip address, username & password?

i tried this but it doesn't work:

(i.e. username= user pwd=hello)

ftp 10.20.30.40 user hello
but the batch file returned this error: invalid command.


i also tried :

ftp 10.20.30.40
user
hello

but i had again "invalid error" message.

What i'm doing wrong?

Ciao
Riccardohave not done ftp using batch for some time
think its like this:
create a file with ftp commands and save it, say ftpcmds.txt:

open
user
pass
cd
get file
close
by

then create the batch job , save it and run will do..

ftp -s:ftpcmds.txt




Below an example that has the FTP script embedded at the end of the batch file.
More sophisticated examples are available here:

http://dostips.cmdtips.com/DtCodeBatchFiles.php

Hope this helps.

Code: [Select]@ECHO OFF
REM.-- Prepare the Command Processor
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION


rem --build the FTP script as <ThisFileName>.txt
set FtpScript=%~dpn0+.ftp
type nul>"%FtpScript%"
call:extractFromFile ##FtpScript##>>"%FtpScript%"


rem --Execute the FTP script
ftp -i -s:"%FtpScript%"
set title=Done


REM.-- End of APPLICATION
FOR /l %%a in (5,-1,1) do (TITLE %title% -- closing in %%as&ping -n 2 -w 1 127.0.0.1>NUL)
TITLE Press any key to close the application&ECHO.&GOTO:EOF


::------------------------------------------------------------------
::-- functions start below here
::------------------------------------------------------------------


:extractFromFile - extract lines from a file between begin and end mark
:: - %~1: begin mark, use '...$' mark to allow variable substitution
:: - %~2: optional end mark, default is end of file
:: - %~3: optional source file, default is THIS file
SETLOCAL
set bmk=%~1
set emk=%~2
set src=%~3
set /a b=-1
set /a e=-1
if "%src%"=="" set src=%~f0& ::- if no source file then assume THIS file
for /f "tokens=1,* delims=:" %%a in ('"findstr /n /b /c:"%bmk%" "%~f0""') do (
set b=%%a
set bmk=%%b
)
if /i %b%==-1 echo.ERROR: begin mark '%bmk%' not found in '%src%'&GOTO:EOF
if "%emk%"=="" (set /a e=2000000000) ELSE (
for /f "delims=:" %%a in ('"findstr /n /b /c:"%emk%" "%~f0""') do (
if /i %b% LSS %%a if /i !e!==-1 set e=%%a& rem -- find only the first one after b
)
)
if /i %e%==-1 echo.ERROR: end mark '%emk%' missing in '%src%'&GOTO:EOF
if /i %b% GEQ %e% echo.ERROR: end mark '%emk%' detected before begin mark '%bmk%' in '%src%'&GOTO:EOF
for /f "delims=: tokens=1,*" %%a in ('"findstr /v /n /b /c:"#$*ReturnAll*$#" "%src%""') do (
if /i %b% LSS %%a if /i %%a LSS %e% (
if "%bmk:~-1%"=="$" (
rem --substitution variables within %%b
call echo.%%b
) ELSE (
rem --no variable substitution
echo.%%b
)
)
)
GOTO:EOF


::------------------------------------------------------------------
::-- ftp script starts below here
::------------------------------------------------------------------


##FtpScript##$
open example.com
anonymous
[emailprotected]
^!: THIS IS A COMMENT
^!: Put your FTP script below here, e.g.:
^!: change the local directory to be the location of the batch file
lcd %~dp0.
^!: set the target directory
cd public_html/cgi-bin
^!: upload all xml files
mput *.xml
^!: end of FTP session
bye
Hi GUYS,

It work, here is my error, in the *.txt files i used this syntax

open <10.20.30.40>
user
pass

But the correct syntax is :

open 10.20.30.40
Myuser
Mypass

now it works SWEETLY, thanks so muck for help.
have fun GUYS.

Ciao
Riccardo



Discussion

No Comment Found