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

asked on

FTP - Powershell

https://www.experts-exchange.com/questions/28684693/Batch-file-to-powershell-scripting.html ( closed)


upload.bat

@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


It's currently going by "modified" date, because a time stamp in mm_dd_yyyy format doesn't really lend itself for alphabetic sort.

As you mentioned the file in purple_top directory

1. 06_03_2015_14_49.csv  Modified date - 6/4/2015 9:13 AM

2. 06_02_2015_15_45.csv  Modified date - 6/3/2015 7:37 AM

As per your code when I run the batch file should it not ftp No 1 file? Please confirm. I am asking this as it did not ftp based on modified date.


The user will put one file everyday in the morning. 1 File a day by date and time.

Need help in getting the most recent file to the server.
Avatar of oBdA
oBdA

That's the older version of which I mentioned that in line 8, the "/o:-d" should have been "/o:d".
Anyway, here's an updated version that will now pick the latest file in the source folder based on the time stamp in the file name, not on the modified date anymore.
It contains the PS code to re-process the csv file in it, and it will now strip off the header line.
As before, the ftp command is inactive, remove the ECHO in front of line 38 to run it for real.
@echo off
setlocal enabledelayedexpansion
set FtpFile=%~dp0upload.ftp
set CsvFolder=F:\Purple_Top
set logfile=upload.log

set TmpFile=%Temp%\%~n0.tmp
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 (
		for /f "tokens=1-5 delims=_." %%d in ("%%a") do (
			echo %%f%%d%%e%%g%%h	%%a
		)
	)
) >"%TmpFile%"
for /f "tokens=1*" %%a in ('type "%TmpFile%" ^| sort.exe') do (
	set CsvFile=%CsvFolder%\%%b
)
del "%TmpFile%"

if not defined CsvFile (
	echo No csv file found in '%CsvFolder%'.
	exit /b 1
)
echo Using '%CsvFile%' as input file.
for %%a in ("%CsvFile%") do (set UploadFile=%%~dpna-converted.csv)

echo Filtering unused columns, writing output to '%UploadFile%'.
powershell.exe -ExecutionPolicy RemoteSigned -Command "& {Import-Csv -Path '%CsvFile%' | Select-Object -Property 'Sample ID No', 'HGB(g/L)', 'PLT(10^3/uL)' | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | ForEach-Object {$_.Replace([char]34 + ',' + [char]34 , ',').Trim([char]34)} | Out-File -FilePath '%UploadFile%' -Encoding ASCII}"

call :ExportDataSection FTP "%FtpFile%"

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

ECHO 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 %UploadFile%
bye

Open in new window

Avatar of anumoses

ASKER

Log file

ftp> User (heart1.heartlandbc.org:(none)): open heart1
ftp>

ftp> cd /home/hbc_data/data/purple_top_data
ftp> mput F:\Purple_Top\_.
F:\Purple_Top\_.:File not found
bye

this is the file that is there

06_03_2015_14_49.csv
_. %d in (06_03_2015_14_49.csv) do (echo %f%d%e%g%h 06_03_2015_14_49-converted.csv

This existed in the folder f:purple_top
Is there a way that you can add another line in the code to move this into the archive folder at f:purple_top that I will create. This way the code will always detect one csv file. Let me know and I will add the folder. 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
Perfect solution. Awesome job. Thanks
Just one question. I know I have to use a mv command on the server to move the converted file to archive that I have already created.
/home/hbc_data/data/purple_top_date/archive
 Can you tell me where to add the code after it is converted on the server?