• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2586
  • Last Modified:

Dynamic ftp batch file

I am trying to write a dynamic batch file to get files from an FTP server.

The files on the server are updated sporadically and I need to be able to get the files by name, so mget is out of the question.
After the files are received, I will need to remove them from the ftp server.

Process something like this

ftp server name
dir
get files dir listed
delete files dir listed
bye
0
jkratzer
Asked:
jkratzer
  • 3
  • 3
  • 3
  • +1
1 Solution
 
pbarretteCommented:
Hi jkratzer,

Why can't you use MGET? It will download all the files on the FTP server.

If you know what files you need, then why not specify them manually in the script?

If the files you need change, but have a specific filename pattern, then can you give an example of the filenames?

pb
0
 
jkratzerAuthor Commented:
Each file gets a unique file name.  I do not know what the file names are until I do a dir on the ftp directory.
There are files continuously being put there, and I need to be able to delete them by name after download as so not to affect any that are new since download started.

0
 
koolmikeCommented:
try creating anouther directory on the FTP server, then you can move all of the files to the new directory, then download all files, then it will be safe to delete all files as any new files will be added to the old directory, am i making sense? nope, didn't think so...
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
pbarretteCommented:
Hi Jratzer,

This is possible, but I need more information:
What OS are you using?
Are you GETting all files in the remote directory, then deleting the files you have received?
If you are not GETting all the files, then how do you know which ones to GET?

pb
0
 
jkratzerAuthor Commented:
This is for a Win 2K box getting the files from a Sun FTP server.

Yes we are getting all the files, but need to ensure that none are recieved twice and that all are recieved.

If this was for a unix box, I would just do an dir/ls and output to a file, then read it in in a while loop and use that to do a get $FILENAME.

I know this same thing can be done in dos.
0
 
pbarretteCommented:
Hi Jratzer,

This batchfile grabs a list of the files on the FTP server, then compares that list against files in the local current directory. If the files do not exist on the local machine, the script will GET then DEL the file on the FTP server.

:: ---------GETFILES.BAT-----------
@ECHO OFF
:: --------
:: Set up FTP script header
:: --------
ECHO open 192.168.1.1>FTPFILE.SCR
ECHO username>>FTPFILE.SCR
ECHO password>>FTPFILE.SCR
ECHO CD "/MyDir/">>FTPFILE.SCR
ECHO BIN>>FTPFILE.SCR
:: --------
:: Get FTP dir list
:: --------
ECHO DIR>>FTPDIR.SCR
ECHO BYE>>FTPDIR.SCR
TYPE FTPFILE.SCR FTPDIR.SCR>FTPDIR.FTP 2>NUL
FTP -s:FTPDIR.FTP>FTPDIR.DAT
:: --------
:: Find filesizes and filenames on FTP server.
:: There's a little extra here because FTP
:: returns unix formatted data, which must
:: be stripped before we can use it.
:: --------
FOR /F "TOKENS=5,9" %%F IN ('TYPE FTPDIR.DAT^|FIND "-r"^|FIND /V "dr"') DO (
set FILE=#%%G#
CALL :PROCREM
)
GOTO LOCAL
:PROCREM
ECHO %FILE%>>RFILES.DAT
GOTO END
:: --------
:: Find filesizes and filenames for local files.
:: --------
:LOCAL
FOR /F "TOKENS=3,4" %%F IN ('DIR /A-D /-C^|FIND "."') DO (
ECHO %%G>>LFILES.DAT
)
:: --------
:: Compare to see what we have and create
:: the script to GET the missing or
:: different files
:: --------
FOR /F "TOKENS=1 DELIMS=#" %%F IN (RFILES.DAT) DO (
TYPE LFILES.DAT|FIND "%%F">NUL
 IF ERRORLEVEL 1 (
  ECHO GET "%%F">>GETFILES.SCR
  ECHO DEL "%%F">>GETFILES.SCR)
)
ECHO BYE>>GETFILES.SCR

:: --------
:: Set up the Date for logging:
:: --------
FOR /F "TOKENS=2-4 DELIMS=/ " %%F IN ('DATE /T') DO (
SET MM=%%F
SET DD=%%G
SET YYYY=%%H
)
:: --------
:: GET the files and log it.
:: --------
TYPE FTPFILE.SCR GETFILES.SCR>FTPGET.FTP 2>NUL
FTP -s:FTPGET.FTP>FTPLOG-%MM%-%DD%-%YYYY%.LOG
:: --------
:: Clean up the mess
:: --------
DEL FTPFILE.SCR
DEL FTPDIR.SCR
DEL FTPDIR.FTP
DEL FTPDIR.DAT
:: DEL RFILES.DAT
:: DEL LFILES.DAT
DEL GETFILES.SCR
DEL FTPGET.FTP
:END
:: ---------GETFILES.BAT-----------

pb
0
 
jkratzerAuthor Commented:
Works like a champ.

Thanks
0
 
asasamyCommented:
Hi pb,

i need to get the report file by checking the remote and local files, if dose not match...,
but my local file list generated based on the date, which should be matched with the FTP server's files, if not then i need to report in a file.

How to do this???


Here is code.. for your reference....


:: ---------GETFILES.BAT-----------
@ECHO OFF
:: --------
:: Set up FTP script header
:: --------
FOR /F "TOKENS=2-4 DELIMS=/ " %%F IN ('DATE /T') DO (SET CDATE=%%H%%F%%G)

ECHO OPEN 144.46.239.93 23 > FTPFILE.SCR
ECHO conoco/samyas>> FTPFILE.SCR
ECHO Rteacher12>> FTPFILE.SCR
ECHO CD "/SEMBLOG/" >> FTPFILE.SCR
ECHO LCD C:\Arul >> FTPFILE.SCR
ECHO BIN>>FTPFILE.SCR

:: ***************** Pls, Note *****************
::-------------i am generating file (Local), based on the date.... and this file should be matched with the FTP
:: ------------Server files.... if not, then i need to generate a report file thaqt this file is not in the FTP Server.......
:: ******************************************
ECHO get CNCSCLCUSTM%CDATE%.txt >> FTPSCRSembLog.TXT
ECHO get CNCSCLINBOUND%CDATE%.txt >> FTPSCRSembLog.TXT
ECHO get CNCSCLINVOICE%CDATE%.txt >> FTPSCRSembLog.TXT
ECHO get CNCSCLPRODINV%CDATE%.txt >> FTPSCRSembLog.TXT
ECHO get CNCSCLPRODM%CDATE%.txt >> FTPSCRSembLog.TXT
ECHO get CNCSCLUOM%CDATE%.txt >> FTPSCRSembLog.TXT

:: **********************************

:: --------
:: Get FTP dir list
:: --------
ECHO DIR>>FTPDIR.SCR
ECHO BYE>>FTPDIR.SCR
TYPE FTPFILE.SCR FTPDIR.SCR>FTPDIR.FTP 2>NUL
FTP -s:FTPDIR.FTP>FTPDIR.DAT

:: --------
:: Find filesizes and filenames on FTP server.
:: There's a little extra here because FTP
:: returns unix formatted data, which must
:: be stripped before we can use it.
:: --------
FOR /F "TOKENS=5,9" %%F IN ('TYPE FTPDIR.DAT^|FIND "-r"^|FIND /V "dr"') DO (
set FILE=#%%F %%G#
CALL :PROCREM
)
GOTO LOCAL
:PROCREM
ECHO %FILE%>>RFILES.DAT
GOTO END

:: --------
:: Find filesizes and filenames for local files.
:: --------
:LOCAL
FOR /F "TOKENS=3,4" %%F IN ('DIR /A-D /-C^|FIND "."') DO (
ECHO %%F %%G>>LFILES.DAT
)

:: --------
:: Compare to see what we have and create
:: the script to GET the missing or
:: different files
:: --------
FOR /F "TOKENS=1,2 DELIMS=# " %%F IN (RFILES.DAT) DO (
TYPE LFILES.DAT|FIND "%%F %%G">NUL
IF ERRORLEVEL 1 ECHO GET %%G>>GETFILES.SCR
)
ECHO BYE>>GETFILES.SCR

:: --------
:: Set up the Date for logging:
:: --------
FOR /F "TOKENS=2-4 DELIMS=/ " %%F IN ('DATE /T') DO (
SET MM=%%F
SET DD=%%G
SET YYYY=%%H
)

:: --------
:: GET the files and log it.
:: --------
TYPE FTPFILE.SCR GETFILES.SCR>FTPGET.FTP 2>NUL
FTP -s:FTPGET.FTP>FTPLOG-%DD%-%MM%-%YYYY%.LOG


:: --------------for Server Report
::FOR /F "TOKENS=1,2" %%F IN (LFILES.DAT) DO (
::TYPE RFILES.DAT|FIND "%%F %%G">NUL
::IF ERRORLEVEL 1 ECHO PUT %%G>>GETFILES.SCR
::)
::ECHO BYE>>GETFILES.SCR
:: --------------




:: --------
:: Clean up the mess
:: --------
DEL FTPFILE.SCR
DEL FTPDIR.SCR
DEL FTPDIR.FTP
DEL FTPDIR.DAT
DEL RFILES.DAT
DEL LFILES.DAT
DEL GETFILES.SCR
DEL FTPGET.FTP

:END
:: ---------GETFILES.BAT-----------
0
 
asasamyCommented:
Please Delete my queries above !...

Tks
AS
0
 
asasamyCommented:
Hi there,
Some one on the admin side need your help,
to delete all my quries applier on under the Topic "Title: Dynamic ftp batch file  "


Thanks
As
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now