Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamic ftp batch file

Posted on 2003-02-25
10
Medium Priority
?
2,576 Views
Last Modified: 2008-06-05
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
Comment
Question by:jkratzer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 10

Expert Comment

by:pbarrette
ID: 8018046
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
 
LVL 6

Author Comment

by:jkratzer
ID: 8018437
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
 

Expert Comment

by:koolmike
ID: 8018570
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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 10

Expert Comment

by:pbarrette
ID: 8018710
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
 
LVL 6

Author Comment

by:jkratzer
ID: 8018989
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
 
LVL 10

Accepted Solution

by:
pbarrette earned 400 total points
ID: 8019460
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
 
LVL 6

Author Comment

by:jkratzer
ID: 8019800
Works like a champ.

Thanks
0
 

Expert Comment

by:asasamy
ID: 9229548
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
 

Expert Comment

by:asasamy
ID: 9327226
Please Delete my queries above !...

Tks
AS
0
 

Expert Comment

by:asasamy
ID: 9327236
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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question