anumoses
asked on
FTP - Powershell
https://www.experts-exchange.com/questions/28684693/Batch-file-to-powershell-scripting.html ( closed)
upload.bat
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.
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
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.
ASKER
Log file
ftp> User (heart1.heartlandbc.org:(n one)): 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
ftp> User (heart1.heartlandbc.org:(n
ftp>
ftp> cd /home/hbc_data/data/purple
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
ASKER
_. %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
This existed in the folder f:purple_top
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect solution. Awesome job. Thanks
ASKER
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?
/home/hbc_data/data/purple
Can you tell me where to add the code after it is converted on the server?
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.
Open in new window