I'm trying to write a script to download *.csv file and rename all to *.csv
to *.bak. How would I do this? Please see my script:
open 192.168.2.1
username
password
cd /temp
lcd x:\
mget *.csv
ren *.csv *.bak
quit
Of course mget and ren at this point wouldn't work. Please help.
Thanks in advance,
Sarah
As you've discovered, there is no easy way to accomplish the MGET and REN
using wildcards. MGET prompts for every file and you have to respond "y" to
start the download (and you probably cannot know how many .csv files there
will be). The REN command simply does not support wildcards.
You can create a script to accompish your task. The script would first
retrieve a listing of all .csv files. Then, based on the listing, the script
would issue GET and REN commands for every .csv file. I've put together 3
files for you to accomplish this. They're text based so simply copy and
paste their contents from this post to notepad and Save As to the appropriate
file name.
The FtpListScript.txt file is used first to retrieve a directory listing of
all the .csv files. It is later used to build the gen_FtpWorkScript.txt that
will be used to GET and REN the files (the first 5 lines from this file is
copied into gen_FtpWorkScript.txt because the connection information,
username, password and local and remote directories can then be only changed
in one location).
The FtpCSVFiles.bat is the main file you need to run to kick everything off.
It calls on the help of ParseList.vbs to take the directory listing from the
server and turn it into something we can use later on. A caveat with the way
ParseList.vbs works is that it assumes the file names do not contain spaces.
If this is a problem I can rework it for you, but I'll need a sample of your
directory listing output. Note that MSFTP server listing looks different
from most Unix or Mainframe based FTP server listings.
Example of listing from MSFTP Server:
04-27-05 10:33AM 7 csv1.csv
04-27-05 10:33AM 7 csv2.csv
04-27-05 10:33AM 7 csv3.csv
Example of listing from most other FTP Server types:
-rw-r--r-- 1 ftp ftp 7 Apr 27 10:33 csv1.csv
-rw-r--r-- 1 ftp ftp 7 Apr 27 10:33 csv2.csv
-rw-r--r-- 1 ftp ftp 7 Apr 27 10:33 csv3.csv
The FtpCSVFiles.bat will exit with error message if it was unable to
establish connection to the FTP server, or if there were not .csv files found
there. So here below are the 3 files. Let me know how it goes.
Best Regards,
Andre.
---- Start of file FtpListScript.txt ----
open 192.168.2.1
username
password
cd /temp
lcd x:\
ls *.csv
quit
---- End of file FtpListScript.txt ----
---- Start of file FtpCSVFiles.bat ----
@ECHO OFF
:: Retrieve directory listing of *.CSV...
ftp -s:FtpListScript.txt | find /i ".csv" | find /v "*" > gen_CSVFileList.txt
IF ERRORLEVEL 1 GOTO :ERROR
:: Parse the listing...
cscript //nologo ParseList.vbs < gen_CSVFileList.txt >
gen_ParsedCSVFileList.txt
:: Copy first 5 lines from FtpListScript.txt to gen_FtpWorkScript.txt...
IF EXIST gen_FtpWorkScript.txt DEL gen_FtpWorkScript.txt
SET iLineCount=1
FOR /F "tokens=*" %%I IN (FtpListScript.txt) DO CALL :FIRST_LINES %%I
:: Add GET and REN commands to gen_FtpWorkScript...
FOR /F %%I IN (gen_ParsedCSVFileList.txt) DO ECHO GET %%I>>
gen_FtpWorkScript.txt
FOR /F %%I IN (gen_ParsedCSVFileList.txt) DO ECHO REN %%I %%I.bak>>
gen_FtpWorkScript.txt
:: Finish gen_FtpWorkScript and execute it...
ECHO QUIT>> gen_FtpWorkScript.txt
ftp -s:gen_FtpWorkScript.txt
GOTO :EOF
:FIRST_LINES
IF %iLineCount% LEQ 5 (
ECHO %*>> gen_FtpWorkScript.txt
SET /A iLineCount=%iLineCount% + 1
)
GOTO :EOF
:ERROR
echo error occured.
GOTO :EOF
---- End of file FtpCSVFiles.bat ----
---- Start of file ParseList.vbs ----
'---
'- Reads every line from standard input.
'- For every line:
'- Locate the last space character in the line.
'- Skips that space and copies every character for the remainder of the
line.
'- Outputs this to standard output.
'- Goal: to parse a file list and output only the file names.
'- Caveat: file names cannot contain space in the file name.
'---
Option Explicit
Dim sInputLine
Dim sOutputLine
'-- Process from standard input...
Do While Not WScript.StdIn.AtEndOfStream
sInputLine = WScript.StdIn.ReadLine
'-- Chop off trailing CR and LF characters...
Do While Asc(Right(sInputLine,1)) < 32
sInputLine = Left(sInputLine,Len(sInputLine)-1)
Loop
'-- Parse line if it is not a blank line...
If Len(sInputLine) > 0 Then
sOutputLine = Trim(Mid(sInputLine,InStrRev(sInputLine," ")))
WScript.Echo sOutputLine
End If
Loop
---- End of file ParseList.vbs ----
Thanks so much for your response. I almost give up on this project. Your
solution gives me hope. I definitely will give a try and let you know.
Appreciate.
Sarah
"Andre Lombard" <AndreL...@discussions.microsoft.com> wrote in message
news:69E2A70E-D9BD-401D...@microsoft.com...
I've been trying your code. Everything is fine except I couldn't get through
cscript point. It won't generate file. The file gen_ParseCSVFileList.txt is
empty.
I got the error message saying "Expected Identifier".
Can you please help me with this?
Thanks,
Sarah
"Andre Lombard" <AndreL...@discussions.microsoft.com> wrote in message
news:69E2A70E-D9BD-401D...@microsoft.com...