Logic Machine Forum
Append line to existing file on FTP - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Append line to existing file on FTP (/showthread.php?tid=3637)



Append line to existing file on FTP - Trond Hoyem - 21.10.2021

Hi

I am struggling with appending a line at the end of an existing CSV located on a FTP-server.
I have looked as this example for guidance; https://openrb.com/example-export-last-hour-csv-object-log-file-to-external-ftp-server-from-lm2/

My problem is that when I try to append the extra line, I always get "Access denied", even though I am sure that the user and password is OK. I have tried to modify the fila path to see the result, and then I get "not found, as is logical since the path is wrong. To me is seems that I am able to find the file, just not access it.

The file is created with another script, using the same user and password (I have copied it from the creation script). The creation of the script works perfect. 

Any ideas?

The code I have is;
Code:
require('socket.ftp')

--FTP-fil med fullstendig bane. Bruker og passord må være satt opp rett i FTP på mottakersiden
ftpfile = storage.get('ftpFileName', ftpfile)

log(ftpfile)
--Formatterer tid og dato for å legge inn i logger senere.
now = os.date('*t')
time = {
day = wday,
hour = now.hour,
minute = now.min,
second = now.sec,
}

if time.hour < 10 then time.hour = '0' .. time.hour end
if time.minute < 10 then time.minute = '0' .. time.minute end
if time.second < 10 then time.second = '0' .. time.second end

klokke = time.hour .. ':' .. time.minute .. ':' .. time.second


--Lise er sortert for å få stigende rekkefølge i rapport
liste = {'M-017-018 Matavfall +16=564.001-RT657 HVAC ACTUAL_TEMP', 'M-026 Disp.kjøl +16=564.001-RT656 HVAC ACTUAL_TEMP',
  'M-027 Disp.kjøl +16=564.001-RT655 HVAC ACTUAL_TEMP', 'M-031 Meierikjøl +16=564.001-RT659 HVAC ACTUAL_TEMP',
  'M-033 Pakkerom +16=564.001-RT658 HVAC ACTUAL_TEMP', 'M-034 Kjøl vaktmat +16=564.001-RT660 HVAC ACTUAL_TEMP',
  'M-035 Frys rester +16=564.001-RT662 HVAC ACTUAL_TEMP', 'M-039 Varemottak +16=564.001-RT644 HVAC ACTUAL_TEMP',
  'M-040 Kjøl frukt/grønt +16=564.001-RT650 HVAC ACTUAL_TEMP', 'M-041 Kjøl kjøtt +16=564.001-RT651 HVAC ACTUAL_TEMP',
  'M-042 Kjøl fisk +16=564.001-RT652 HVAC ACTUAL_TEMP', 'M-043 Frys +16=564.001-RT653 HVAC ACTUAL_TEMP',
  'M-044 Forbredelse FR/GR +16=564.001-RT661 HVAC ACTUAL_TEMP', 'M-045 Forbredelse FR/GR +16=564.001-RT663 HVAC ACTUAL_TEMP',
  'M-046 Passasje +16=564.001-RT603 HVAC ACTUAL_TEMP', 'M-047 Kaldtkjøkken +16=564.001-RT610 HVAC ACTUAL_TEMP', 'M-059 Tinerom +16=564.001-RT654 HVAC ACTUAL_TEMP'}


logLine = os.date('%d/%m-%Y') .. ';' .. klokke

--Henter alle temperaturer og lager tekststreng som skal skrives inn i eksisterende fil.
for _, addr in ipairs(liste) do
  value = tostring(grp.getvalue(addr))
  value = value:gsub('%.', ',')
  logLine = logLine .. ';' .. value
  --log(value)
end

buffer = {logLine}
log(buffer)

if #buffer > 0 then
  data = table.concat(buffer, '\r\n')
  res, err = socket.ftp.put({
    host = '192.168.0.188',
    user = 'user',
    password = 'password',
    command = 'appe',
    argument = ftpfile,
    source = ltn12.source.string(data)
  })
end

if err then
  alert('FTP upload error: %s', tostring(err))
end

log(res, err)

(21.10.2021, 15:22)Trond Hoyem Wrote: Hi

I am struggling with appending a line at the end of an existing CSV located on a FTP-server.
I have looked as this example for guidance; https://openrb.com/example-export-last-hour-csv-object-log-file-to-external-ftp-server-from-lm2/

My problem is that when I try to append the extra line, I always get "Access denied", even though I am sure that the user and password is OK. I have tried to modify the fila path to see the result, and then I get "not found, as is logical since the path is wrong. To me is seems that I am able to find the file, just not access it.

The file is created with another script, using the same user and password (I have copied it from the creation script). The creation of the script works perfect. 

Any ideas?

The code I have is;
Code:
require('socket.ftp')

--FTP-fil med fullstendig bane. Bruker og passord må være satt opp rett i FTP på mottakersiden
ftpfile = storage.get('ftpFileName', ftpfile)

log(ftpfile)
--Formatterer tid og dato for å legge inn i logger senere.
now = os.date('*t')
time = {
day = wday,
hour = now.hour,
minute = now.min,
second = now.sec,
}

if time.hour < 10 then time.hour = '0' .. time.hour end
if time.minute < 10 then time.minute = '0' .. time.minute end
if time.second < 10 then time.second = '0' .. time.second end

klokke = time.hour .. ':' .. time.minute .. ':' .. time.second


--Lise er sortert for å få stigende rekkefølge i rapport
liste = {'M-017-018 Matavfall +16=564.001-RT657 HVAC ACTUAL_TEMP', 'M-026 Disp.kjøl +16=564.001-RT656 HVAC ACTUAL_TEMP',
  'M-027 Disp.kjøl +16=564.001-RT655 HVAC ACTUAL_TEMP', 'M-031 Meierikjøl +16=564.001-RT659 HVAC ACTUAL_TEMP',
  'M-033 Pakkerom +16=564.001-RT658 HVAC ACTUAL_TEMP', 'M-034 Kjøl vaktmat +16=564.001-RT660 HVAC ACTUAL_TEMP',
  'M-035 Frys rester +16=564.001-RT662 HVAC ACTUAL_TEMP', 'M-039 Varemottak +16=564.001-RT644 HVAC ACTUAL_TEMP',
  'M-040 Kjøl frukt/grønt +16=564.001-RT650 HVAC ACTUAL_TEMP', 'M-041 Kjøl kjøtt +16=564.001-RT651 HVAC ACTUAL_TEMP',
  'M-042 Kjøl fisk +16=564.001-RT652 HVAC ACTUAL_TEMP', 'M-043 Frys +16=564.001-RT653 HVAC ACTUAL_TEMP',
  'M-044 Forbredelse FR/GR +16=564.001-RT661 HVAC ACTUAL_TEMP', 'M-045 Forbredelse FR/GR +16=564.001-RT663 HVAC ACTUAL_TEMP',
  'M-046 Passasje +16=564.001-RT603 HVAC ACTUAL_TEMP', 'M-047 Kaldtkjøkken +16=564.001-RT610 HVAC ACTUAL_TEMP', 'M-059 Tinerom +16=564.001-RT654 HVAC ACTUAL_TEMP'}


logLine = os.date('%d/%m-%Y') .. ';' .. klokke

--Henter alle temperaturer og lager tekststreng som skal skrives inn i eksisterende fil.
for _, addr in ipairs(liste) do
  value = tostring(grp.getvalue(addr))
  value = value:gsub('%.', ',')
  logLine = logLine .. ';' .. value
  --log(value)
end

buffer = {logLine}
log(buffer)

if #buffer > 0 then
  data = table.concat(buffer, '\r\n')
  res, err = socket.ftp.put({
    host = '192.168.0.188',
    user = 'user',
    password = 'password',
    command = 'appe',
    argument = ftpfile,
    source = ltn12.source.string(data)
  })
end

if err then
  alert('FTP upload error: %s', tostring(err))
end

log(res, err)

Nevermind. I found it. There was a mistake in the file name after all....