I am hacking together a node script in Puppeteer where I scrap a webpage and safe the content as a text file where I name the file after the date and want to add text from a text file at the end of the name.
const puppeteer = require('puppeteer');
const fs = require('fs');
let utc_date_string = new Date().toLocaleString("en-US", { timeZone: "UTC" });
let date_utc = new Date(utc_date_string);
let year = date_utc.getFullYear();
let month = ("0" + (date_utc.getMonth() + 1)).slice(-2);
let date = ("0" + date_utc.getDate()).slice(-2);
let hours = ("0" + date_utc.getHours()).slice(-2);
let minutes = ("0" + date_utc.getMinutes()).slice(-2);
let time_hh_mm = hours + + minutes;
let data = fs.readFileSync('insert.txt', 'utf8');
(async () => {
const browser = await puppeteer.launch(
{
headless: true,
args: ['--single-process', '--no-zygote', '--no-sandbox']
})
const page = await browser.newPage()
await page.goto('https://example.com/xxx.html')
const myproblem = await page.$eval('table', table => table.innerText)
fs.writeFile('/var/www/mydomain.com/xxx/'+year+'-'+month+'-'+date+'-'+time_hh_mm+'-'+data+'.txt', myproblem)
await browser.close()
})()
This works so far but the problem is that the file name comes out like this
'2022-02-13-1453-test_text_insert'$'\n''.txt'
and not like this
2022-02-13-1453-test_text_insert.txt
If I use only the date, the file name comes out as expected. I don't know from where the extra characters come from (they are not in the text file) or if there is something wrong with the code.
String error. You should check this line:
let time_hh_mm = hours + + minutes;
Maybe it should be like this: (?)
let time_hh_mm = hours + minutes;
And you tried to read the file which contain the breakline character \n, you can try to trim it.
fs.writeFile('/var/www/mydomain.com/xxx/'+year+'-'+month+'-'+date+'-'+time_hh_mm+'-'+data.trim()+'.txt', myproblem)