Link to home
Start Free TrialLog in
Avatar of anumoses
anumosesFlag for United States of America

asked on

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.
Avatar of anumoses
anumoses
Flag of United States of America image

ASKER

Thanks
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
My assumption is save the complete new script as  upload.bat. Is this correct?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ?
Avatar of oBdA
oBdA

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.
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
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).
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do you want me to post this in powershell topic with batch file and csv file? Please let me know.
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help and imparting knowledge.