user982201
user982201

Reputation: 71

DOS script tp FTP from a remote server and copy files to local folder

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

Upvotes: 0

Views: 10955

Answers (2)

MBu
MBu

Reputation: 2950

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

Upvotes: 0

Graham
Graham

Reputation: 338

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

Upvotes: 1

Related Questions