Command line FTP help

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have written a batch program which FTPs a file to a remote location. I have
specified the -s parameter in the command syntax. The command file contains
the FTP commands including the file to be transferred.
However the file name changes each day. The batch program is run specifying
input parameters as some encryption is done on the source file. %1 being the
source file name and %2 being the filename after encryption.
As a result of this I need to use %2 in the FTP command file. I have
attempted this and it does not work. Is there anyway I can get the filename
into the command file? Or is there a better way of doing this?
 
Jake said:
I have written a batch program which FTPs a file to a remote location. I have
specified the -s parameter in the command syntax. The command file contains
the FTP commands including the file to be transferred.
However the file name changes each day. The batch program is run specifying
input parameters as some encryption is done on the source file. %1 being the
source file name and %2 being the filename after encryption.
As a result of this I need to use %2 in the FTP command file. I have
attempted this and it does not work. Is there anyway I can get the filename
into the command file? Or is there a better way of doing this?

This should be fairly straightforward. Let's have a look
at your current batch file so that someone can suggest
how to implement your requirement.
 
OK here is the batch program:


@echo off
rem Version 1.0
rem
if (%1)==() GOTO NOPARAM1
if (%2)==() GOTO NOPARAM2

rem This part of the program encrypts the input file and produces an
encrypted output file

gpg --armor --output %2 --recipient XXXXXXXX --encrypt %1
if not errorlevel 1 goto FTP
echo ------------------------------------------------------------------- >>
C:\Gnupg\SecureFTP.log
date /t >> C:\Gnupg\SecureFTP.log
time /t >> C:\Gnupg\SecureFTP.log
echo An error occured whilst encrypting the file >> C:\Gnupg\SecureFTP.log
echo ------------------------------------------------------------------- >>
C:\Gnupg\SecureFTP.log

GOTO END

rem This part of the program transfers the encrypted file using FTP

:FTP
echo ------------------------------------------------------------------- >>
C:\Gnupg\SecureFTP.log
date /t >> C:\Gnupg\SecureFTP.log
time /t >> C:\Gnupg\SecureFTP.log
C:\Windows\system32\FTP.EXE -i -n -d -s:C:\Gnupg\FTP_Steps.txt >>
C:\Gnupg\SecureFTP.log
if not errorlevel 1 goto END
echo ------------------------------------------------------------------- >>
C:\Gnupg\SecureFTP.log
date /t >> C:\Gnupg\SecureFTP.log
time /t >> C:\Gnupg\SecureFTP.log
echo An error occured during the FTP transfer >> C:\Gnupg\SecureFTP.log
echo ------------------------------------------------------------------- >>
C:\Gnupg\SecureFTP.log

GOTO END

:NOPARAM1
echo ------------------------------------------------------------------- >>
C:\Gnupg\SecureFTP.log
date /t >> C:\Gnupg\SecureFTP.log
time /t >> C:\Gnupg\SecureFTP.log
echo The input (unencrypted) filename did not get parsed to the program >>
C:\Gnupg\SecureFTP.log
echo ------------------------------------------------------------------- >>
C:\Gnupg\SecureFTP.log

:NOPARAM2
echo ------------------------------------------------------------------- >>
C:\Gnupg\SecureFTP.log
date /t >> C:\Gnupg\SecureFTP.log
time /t >> C:\Gnupg\SecureFTP.log
echo The output (encrypted) filename did not get parsed to the program >>
C:\Gnupg\SecureFTP.log
echo ------------------------------------------------------------------- >>
C:\Gnupg\SecureFTP.log

:END


Here is the FTP_Steps file:

open xxx.yyyyyyy.co.uk
user username password
put %2
quit
 
You already had most of the important elements - nice job!
In the version below I added a script generator that will
process your input parameters. I also streamlined things
a little bit to make the code shorter and more robust.

@echo off
rem Version 1.0

if (%1)==() GOTO NOPARAM1
if (%2)==() GOTO NOPARAM2

set site=xxx.yyyyyyy.co.uk
set user=username
set pass=password
set source=%1
set target=%2
set script=C:\Gnupg\FTP_Steps.txt
set log=C:\Gnupg\SecureFTP.log

echo>%script% %user%
echo>>%script% %pass%
echo>>%script% binary
echo>>%script% put %target%
echo>>%script% quit

rem This part of the program encrypts the input file and produces an
encrypted output file

gpg --armor --output %target% --recipient XXXXXXXX --encrypt %source% &&
goto FTP
echo ------------------------------------------------------------------- >>
%log%
echo %date% %time:~0,5% >> %log%
echo An error occured whilst encrypting the file >> %log%
echo ------------------------------------------------------------------- >>
%log%

GOTO :eof

rem This part of the program transfers the encrypted file using FTP

:FTP
echo ------------------------------------------------------------------- >>
%log%
echo %date% %time:~0,5% >> %log%
FTP.EXE -i -n -d -s:%script% %site% >> %log% && goto :eof
echo ------------------------------------------------------------------- >>
%log%
echo %date% %time:~0,5% >> %log%
echo An error occured during the FTP transfer >> %log%
echo ------------------------------------------------------------------- >>
%log%

GOTO END

:NOPARAM1
echo ------------------------------------------------------------------- >>
%log%
echo %date% %time:~0,5% >> %log%
echo The input (unencrypted) filename did not get passed to the program >>
%log%
echo ------------------------------------------------------------------- >>
%log%

:NOPARAM2
echo ------------------------------------------------------------------- >>
%log%
echo %date% %time:~0,5% >> %log%
echo The output (encrypted) filename did not get passed to the program >>
%log%
echo ------------------------------------------------------------------- >>
%log%
 
Awesome. I was thinking along the lines of writing stuff to the FTP_Steps
file but just didn't know how to do it. I made one small change to due to
problems logging on to the remote FTP server:

echo>%script% user %user% %pass%

I have tested this and it works fine. Thank you very much, good job.
 
OK here is the batch program:
[62 lines snipped]

I do this regularly using my preferred CLI, 4NT. Applied to your
situation, the following few lines should give you an idea how:

@ECHO OFF
SET logfile=C:\Gnupg\SecureFTP.log
LOG /W %logfile
IFF %# NE 2 THEN
ECHO You must enter 2 parameters - input and output file names.
QUIT 8
ENDIFF
gpg --armor --output %2 --recipient XXXXXXXX --encrypt %1 | TEE /A %logfile
IFF ERRORLEVEL NE 0 THEN
ECHO An error (%ERRORLEVEL) occurred whilst encrypting the file %1
QUIT 4
ENDIFF
COPY %2 "ftp://username:p[email protected]" | TEE /A %logfile
IFF ERRORLEVEL NE 0 THEN
ECHO An error (%ERRORLEVEL) occurred during the FTP transfer of %2
QUIT 2
ENDIFF
EXIT

4NT's transparent treatment of FTP is documented at
<http://jpsoft.com/help/ftpservers.htm>; its LOG command at
<http://jpsoft.com/help/log.htm>. 4NT is a commercial product; 4DOS is
free, but doesn't support FTP.
 
Back
Top