FTP Script - Uses External directory from an oracle package to load data to oracle table.

File name

06_02_2015_15_45.csv

upload.bat

@echo off
@set logfile=upload.log
@ftp -i -v -s:"upload.ftp" > %logfile%
@del %logfile%



upload.ftp

open (hostname)
user_id
password
cd /home/mydir/data/purple_top_data
mput F:\Purple_Top\purple_top.csv
bye

I have written a batch file and script for ftp.

My file name is purple_top.csv. But the user gives the file as 06_02_2015_15_45.csv ( date and time)

IS there a way to add this file name to the ftp script instead of hardcoding? Can I have an example of file name that I can add to the ftp script?


NOT SURE IF I AM FORWARDING THIS TO THE CORRECT GROUP.
LVL 6
anumosesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anumosesAuthor Commented:
Thanks
oBdACommented:
Try the script below; it allows you to keep the actual ftp commands in the batch file (in the FTP section), and the ftp file will be built dynamically. In the [FTP] section, you can use any available environment variable (as in the "mput" line).
The script will use the latest (based on the Last Modified timestamp, not the file name) file in the F:\Purple_Top folder that matches the name format you specified.
The script is currently in test mode and will show the ftp file generated and the echo the ftp command.
Remove the uppercase ECHO in front of the ftp command to run it for real.
@echo off
setlocal enabledelayedexpansion
set FtpFile=%~dp0upload.ftp
set CsvFolder=F:\Purple_Top
set logfile=upload.log

set CsvFile=
for /f "delims=" %%a in ('dir /o:-d /b "%CsvFolder%\*.csv" ^| findstr.exe "^[0-9][0-9]_[0-9][0-9]_[0-9][0-9][0-9][0-9]_[0-9][0-9]_[0-9][0-9]\.csv$"') do (set CsvFile=%CsvFolder%\%%a)
if not defined CsvFile (
	echo No csv file found in '%CsvFolder%'!
	exit /b 1
)
echo Using '%CsvFile%' as file to upload.
call :ExportDataSection FTP "%FtpFile%"

echo Content of the ftp file:
type "%FtpFile%"

ECHO ftp.exe -i -v -s:"%FtpFile%" >"%logfile%"
del "%logfile%"


REM ================================================================================
REM Only functions after this line!
REM ================================================================================
goto :eof
:ExportDataSection
REM *** Reads all lines listed in %1 (section) and writes them to %2 (file name).
REM *** Environment variables will be expanded.
set Section=%~1
set FileName=%~2
if exist "%FileName%" del "%FileName%"
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| C:\Windows\system32\find.exe /n "[%Section%]"') do set DataStart=%%a
for /f "skip=%DataStart% delims=" %%a in ('type "%~f0"') do (
	call :Expand Line "%%a"
	if "!Line:~0,1!"=="[" (goto :eof) else (>>"%FileName%" echo !Line!)
)
goto :eof

:Expand
set %1=%~2
goto :eof

REM ================================================================================
REM Only data sections after this line!
REM ================================================================================
[FTP]
open (hostname)
user_id
password
cd /home/mydir/data/purple_top_data
mput %CsvFile%
bye

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anumosesAuthor Commented:
Thanks for the quick response. I have a question. Earlier I had 2 files.

1. upload.bat
2. upload.ftp

Your script above has everything combined into one. Is that correct? or as before I have to have 2 files but now the upload.ftp is changing.  Please confirm.

Thanks again.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

anumosesAuthor Commented:
My assumption is save the complete new script as  upload.bat. Is this correct?
oBdACommented:
Yes, the script above combines both files. The script will create the ftp on the run, so that variables form the batch file can be used. The separate upload.ftp isn't required anymore.
You can save it as whatever you want, including upload.bat.
You could add a del "%FtpFile%" after line 20, so that nobody will try to change it and then wonder why nothing is happening, but I left it out for the time being until you've tested it.
anumosesAuthor Commented:
From your script I only changed the last part

From

open (hostname)
user_id
password
cd /home/mydir/data/purple_top_data

To


[FTP]
open heart1
hbc_data
hbc_data
cd /home/hbc_data/data/purple_top_data
mput %CsvFile%
bye

But transfer did not work. File was not created.
oBdACommented:
In its current version, the script should generate a file in the script's folder, with the script's name and the extension ".ftp" (so "upload.ftp" if the script is called "upload.bat").
It should generate an output similar to this (the actual content of the ftp file generated); run it from an open command prompt, not with a double-click in Explorer:
Using 'C:\Temp\06_02_2015_15_45.csv' as file to upload.
Content of the ftp file:
open heart1
hbc_data
hbc_data
cd /home/mydir/data/purple_top_data
mput C:\Temp\06_02_2015_15_45.csv
bye

Open in new window


Note that the script above is in test mode, it will not ftp the file. You first have the remove the uppercase ECHO in line 19.
anumosesAuthor Commented:
Now upload.ftp is as below

open heart1
hbc_data
hbc_data
cd /home/hbc_data/data/purple_top_data
mput F:\Purple_Top\05_14_2015_11_17.csv
bye

So test complete as we wanted.
Now I want the 05_14_2015_11_17.csv to be loaded on the server side. As you mentioned if I remove ECHO in line 19 will the file be transferred? or I have to change your code ?
oBdACommented:
Yes, to enable the ftp transfer, all you have to do is remove the uppercase ECHO in line 19.
And a small correction, sorry: the ftp file name will not be created based on the script name, but will be upload.ftp in the script's folder.
And come to think of it: you should probably remove the del "%logfile%" (copy and paste from your original script) in line 20; a log file that is deleted before anyone can see it isn't very useful.
anumosesAuthor Commented:
This is my script now. If I run the .bat file, I should have the file on the server side correct? If so I do not have the file created. What is my mistake?

@echo off
setlocal enabledelayedexpansion
set FtpFile=%~dp0upload.ftp
set CsvFolder=F:\Purple_Top
set logfile=upload.log

set CsvFile=
for /f "delims=" %%a in ('dir /o:-d /b "%CsvFolder%\*.csv" ^| findstr.exe "^[0-9][0-9]_[0-9][0-9]_[0-9][0-9][0-9][0-9]_[0-9][0-9]_[0-9][0-9]\.csv$"') do (set CsvFile=%CsvFolder%\%%a)
if not defined CsvFile (
      echo No csv file found in '%CsvFolder%'!
      exit /b 1
)
echo Using '%CsvFile%' as file to upload.
call :ExportDataSection FTP "%FtpFile%"

echo Content of the ftp file:
type "%FtpFile%"





REM ================================================================================
REM Only functions after this line!
REM ================================================================================
goto :eof
:ExportDataSection
REM *** Reads all lines listed in %1 (section) and writes them to %2 (file name).
REM *** Environment variables will be expanded.
set Section=%~1
set FileName=%~2
if exist "%FileName%" del "%FileName%"
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| C:\Windows\system32\find.exe /n "[%Section%]"') do set DataStart=%%a
for /f "skip=%DataStart% delims=" %%a in ('type "%~f0"') do (
      call :Expand Line "%%a"
      if "!Line:~0,1!"=="[" (goto :eof) else (>>"%FileName%" echo !Line!)
)
goto :eof

:Expand
set %1=%~2
goto :eof

REM ================================================================================
REM Only data sections after this line!
REM ================================================================================
[FTP]
open heart1
hbc_data
hbc_data
cd /home/hbc_data/data/purple_top_data
mput %CsvFile%
bye
oBdACommented:
Well ... you removed the complete ftp command from line 19, not just the uppercase ECHO at the beginning.
When you post code, please enclose it in Code tags (from the tool bar at the top of the Edit box).
anumosesAuthor Commented:
Thanks.

@echo off
setlocal enabledelayedexpansion
set FtpFile=%~dp0upload.ftp
set CsvFolder=F:\Purple_Top
set logfile=upload.log

set CsvFile=
for /f "delims=" %%a in ('dir /o:-d /b "%CsvFolder%\*.csv" ^| findstr.exe "^[0-9][0-9]_[0-9][0-9]_[0-9][0-9][0-9][0-9]_[0-9][0-9]_[0-9][0-9]\.csv$"') do (set CsvFile=%CsvFolder%\%%a)
if not defined CsvFile (
	echo No csv file found in '%CsvFolder%'!
	exit /b 1
)
echo Using '%CsvFile%' as file to upload.
call :ExportDataSection FTP "%FtpFile%"

echo Content of the ftp file:
type "%FtpFile%"

ftp.exe -i -v -s:"%FtpFile%" >"%logfile%"



REM ================================================================================
REM Only functions after this line!
REM ================================================================================
goto :eof
:ExportDataSection
REM *** Reads all lines listed in %1 (section) and writes them to %2 (file name).
REM *** Environment variables will be expanded.
set Section=%~1
set FileName=%~2
if exist "%FileName%" del "%FileName%"
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| C:\Windows\system32\find.exe /n "[%Section%]"') do set DataStart=%%a
for /f "skip=%DataStart% delims=" %%a in ('type "%~f0"') do (
	call :Expand Line "%%a"
	if "!Line:~0,1!"=="[" (goto :eof) else (>>"%FileName%" echo !Line!)
)
goto :eof

:Expand
set %1=%~2
goto :eof

REM ================================================================================
REM Only data sections after this line!
REM ================================================================================
[FTP]
open heart1
hbc_data
hbc_data
cd /home/hbc_data/data/purple_top_data
mput %CsvFile%
bye

Open in new window


This is my code now. It worked. Thanks.  I am enclosing my csv file also. I have a question. in the script can we mention only the columns we need? csv file has lot of data. I need only 3 columns. If that is possible then I will close this and post a new question. Let me know.
05-14-2015-11-17.csv
oBdACommented:
You can certainly remove columns, but judging by the sample you provided, it shouldn't be done in Batch. Chances are too big that Batch will choke on the ^ or other special characters, not to mention that some fields are not filled, which prevents the use of "for /f".
Shouldn't be much of a problem in Powershell (http://www.experts-exchange.com/topics/powershell/)
anumosesAuthor Commented:
Do you want me to post this in powershell topic with batch file and csv file? Please let me know.
oBdACommented:
The batch file isn't of direct importance for the task at hand; just ask a new question that you want to extract certain (and which) columns from the original csv, and that you might need help with the syntax on calling the PS from a batch file (assuming you want to redo the csv on the fly inside this batch script).
I might be able to help, but I don't know if I'll have enough time.
Feel free to post a link to the new question here.
oBdACommented:
Sorry, just noticed an error when retrieving the csv file; it should have been "/o:d" instead of "/o:-d" in the "for /f" command.
So here's the complete script again:
@echo off
setlocal enabledelayedexpansion
set FtpFile=%~dp0upload.ftp
set CsvFolder=F:\Purple_Top
set CsvFolder=C:\Temp
set logfile=upload.log

set CsvFile=
for /f "delims=" %%a in ('dir /o:d /b "%CsvFolder%\*.csv" ^| findstr.exe "^[0-9][0-9]_[0-9][0-9]_[0-9][0-9][0-9][0-9]_[0-9][0-9]_[0-9][0-9]\.csv$"') do (set CsvFile=%CsvFolder%\%%a)
if not defined CsvFile (
	echo No csv file found in '%CsvFolder%'.
	exit /b 1
)

echo Using '%CsvFile%' as input file.
call :ExportDataSection FTP "%FtpFile%"

echo Content of the ftp file:
type "%FtpFile%"

ftp.exe -i -v -s:"%FtpFile%" >"%logfile%"



REM ================================================================================
REM Only functions after this line!
REM ================================================================================
goto :eof
:ExportDataSection
REM *** Reads all lines listed in %1 (section) and writes them to %2 (file name).
REM *** Environment variables will be expanded.
set Section=%~1
set FileName=%~2
if exist "%FileName%" del "%FileName%"
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| C:\Windows\system32\find.exe /n "[%Section%]"') do set DataStart=%%a
for /f "skip=%DataStart% delims=" %%a in ('type "%~f0"') do (
	call :Expand Line "%%a"
	if "!Line:~0,1!"=="[" (goto :eof) else (>>"%FileName%" echo !Line!)
)
goto :eof

:Expand
set %1=%~2
goto :eof

REM ================================================================================
REM Only data sections after this line!
REM ================================================================================
[FTP]
open heart1
hbc_data
hbc_data
cd /home/hbc_data/data/purple_top_data
mput %CsvFile%
bye

Open in new window

anumosesAuthor Commented:
Thanks for the help and imparting knowledge.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.