I'm rushing to find a solution for my problem!
At the moment I'm connected to an FTP and I selected 5 files to download, but I can't get the correct diretory for both local and remote path.
On the remote ftp, I am inside a folder named OUT and this folder contains 5 files that I want to download.
On local, I want to download to my project folder > files.
Here is my code:
ftps.cd('./OUT')
.raw('ls')
.exec(function(err, res){
var arr = [];
var items = res.data.split('\n')//.map(String);
var item;
//console.log(items);
for (i in items){
item = items[i];
if(item.length > 0 ){
var linha = item.toString().slice(46);
var date = linha.toString().substr(0,12);
var date_timestamp = moment([2018, months[date.substr(0,3)], date.substr(7,2), date.substr(5,2), date.substr(10,2)]).format('x');
var nome_ficheiro = linha.toString().slice(13);
// console.log(nome_ficheiro)
// split files
//-rwxrwxrwx 1 owner group 79563 Jul 17 10:44 STKHYN_20180713170017.csv
var fl = [];
// fl['datea'] = moment([2018, months[date.substr(0,3)], date.substr(7,2), date.substr(5,2), date.substr(10,2)]);
fl['date'] = date_timestamp;
fl['filename'] = nome_ficheiro;
arr.push(fl);
}
}
// filter array
arr = _.sortBy(arr, "date").reverse();
// first 5 files
arr = arr.splice(0,5);
// get files
for (i in arr){
ftps.put("files/text.txt", "/");
ftps.get(arr[i]['filename']);
}
console.log(arr);
});
Could you guys please help me?
Related
Is there a way to write a script/automate copying over data from an xlsx file to a google sheet file in the same google drive?
I have the xlsx file to auto-sync in my google drive. I want to then connect this data to data studio report for visualization, but that is not compatible with an xlsx file. I need to transfer this data to a google sheet in order to connect, so I was wondering how I could automate the process of moving data from this xlsx file to a gsheet.
There is a project hosted on GitHub that has what you are looking for:
https://gist.github.com/azadisaryev/ab57e95096203edc2741
It creates the new GS file on GDrive from the existing xlsx file on your GDrive.
In your scenario you may want to read data from the newly created GS and put them into a "static" GS (connected to Data Studio). Then you may want to delete (send to trash) newly created GS file.
This code convert XLSX to G Spread Sheet in specific folder. Update if file name (GSS) already exist.
Sure this helps XLSX2GSS.
function XLSX2GSS() {
var sourceFolderId = ""; // Folder ID including source files.
var destinationFolderId = ""; // Folder ID that the converted files are put.
var getFileIds = function (folder, fileList, q) {
var files = folder.searchFiles(q);
while (files.hasNext()) {
var f = files.next();
fileList.push({id: f.getId(), fileName: f.getName().split(".")[0].trim()});
}
var folders = folder.getFolders();
while (folders.hasNext()) getFileIds(folders.next(), fileList, q);
return fileList;
};
var sourceFiles = getFileIds(DriveApp.getFolderById(sourceFolderId), [], "mimeType='" + MimeType.MICROSOFT_EXCEL + "' or mimeType='" + MimeType.MICROSOFT_EXCEL_LEGACY + "'");
var destinationFiles = getFileIds(DriveApp.getFolderById(destinationFolderId), [], "mimeType='" + MimeType.GOOGLE_SHEETS + "'");
var createFiles = sourceFiles.filter(function(e) {return destinationFiles.every(function(f) {return f.fileName !== e.fileName});});
var updateFiles = sourceFiles.reduce(function(ar, e) {
var dst = destinationFiles.filter(function(f) {return f.fileName === e.fileName});
if (dst.length > 0) {
e.to = dst[0].id;
ar.push(e);
}
return ar;
}, []);
if (createFiles.length > 0) createFiles.forEach(function(e) {Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS, parents: [{id: destinationFolderId}], title: e.fileName}, DriveApp.getFileById(e.id))});
if (updateFiles.length > 0) updateFiles.forEach(function(e) {Drive.Files.update({}, e.to, DriveApp.getFileById(e.id))});
}
I'm trying to make a simple program in node.js that will download the same file with some interval. If the downloaded file is newer than the previous, then it will be saved in a new filename with the help of a counter.
If it's a new file, then I want to save it in the last_unique.jpg name and use it to compare next time the file will be downloaded. But it doesn't seem to work. For test, I just have an empty last_unique.jpg that I would expect to be overwritten. But it never is, so every time the jpg file is downloaded, it is unique and saves it in file3.jpg, file3.jpg, etc.
However, the output also looks like maybe some async issues? It skips the first couple of times.
OUTPUT:
downloading 1
downloading 2
downloading 3
Unique file spotted!
downloading 4
Unique file spotted!
downloading 5
Unique file spotted!
downloading 6
Unique file spotted!
downloading 7
Unique file spotted!
downloading 8
Unique file spotted!
Here is the code:
const http = require('http');
const fs = require('fs');
const md5File = require('md5-file');
const fileToDownload = "http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg";
var counter = 0;
function request() {
counter = counter + 1
console.log("downloading " + counter);
const save = fs.createWriteStream("last_download.jpg");
http.get(fileToDownload, function(response) {
response.pipe(save)
});
const hash1 = md5File.sync('last_download.jpg');
const hash2 = md5File.sync('last_unique.jpg');
// it is a new file
if (hash1.localeCompare(hash2) != 0) {
console.log('Unique file spotted!');
fs.copyFileSync('last_download.jpg','last_unique.jpg');
fs.copyFileSync('last_unique.jpg','file' + counter + '.jpg');
}
}
setInterval(request, 3000);
const http = require('http');
const fs = require('fs');
const md5File = require('md5-file');
const fileToDownload = "http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg";
var counter = 0;
function request() {
counter = counter + 1;
console.log("downloading " + counter);
const save = fs.createWriteStream("last_download.jpg");
http.get(fileToDownload, function(response) {
response.pipe(save);
response.on('end',function () {
save.end();
})
});
save.on('finish',function () {
const hash1 = md5File.sync('last_download.jpg');
const hash2 = md5File.sync('last_unique.jpg');
console.log(hash1,hash2);
// it is a new file
if (hash1.localeCompare(hash2) != 0) {
console.log('Unique file spotted!');
fs.copyFileSync('last_download.jpg','last_unique.jpg');
fs.copyFileSync('last_unique.jpg','file' + counter + '.jpg');
}
});
}
setInterval(request, 3000);
You need to listen for the finish event on the stream otherwise it maybe the case that you call the copy function before the stream has completely been written. Hence a partial image is copied from the last_download.jpg to last_unique.jpg which means the hashes would be different. This is due the asynchronous nature of copying and http request.
I need to access per-machine configuration data in my Node application running on Windows. I've found this documentation for how to find the location:
Where Should I Store my Data and Configuration Files if I Target Multiple OS Versions?
So, in my case, I would like to get the path for CSIDL_COMMON_APPDATA (or FOLDERID_ProgramData). However, the examples are all in C, and I would prefer to not have to write a C extension for this.
Is there any other way to access these paths from Node, or should I just hardcode them?
After doing a bit of research, I've found that it's possible to call the relevant Windows API proc. (SHGetKnownFolderPath) to get these folder locations, see docs at: https://msdn.microsoft.com/en-us/library/windows/desktop/bb762188(v=vs.85).aspx.
We call the APi using the FFI npm module: https://www.npmjs.com/package/ffi.
It is possible to find the GUIDs for any known folder here:
https://msdn.microsoft.com/en-us/library/windows/desktop/dd378457(v=vs.85).aspx
Here is a script that finds the location of several common folders,
some of the code is a little hacky, but is easily cleaned up.
const ffi = require('ffi');
const ref = require('ref');
const shell32 = new ffi.Library('Shell32', {
SHGetKnownFolderPath: ['int', [ ref.refType('void'), 'int', ref.refType('void'), ref.refType(ref.refType("char"))]]
});
function parseGUID(guidStr) {
var fields = guidStr.split('-');
var a1 = [];
for(var i = 0; i < fields.length; i++) {
var a2 = [...Buffer.from(fields[i], 'hex')];
if (i < 3) a2 = a2.reverse();
a1 = a1.concat(a2);
}
return new Buffer(a1);
}
function getWindowsKnownFolderPath(pathGUID) {
let guidPtr = parseGUID(pathGUID);
guidPtr.type = ref.types.void;
let pathPtr = ref.alloc(ref.refType(ref.refType("void")));
let status = shell32.SHGetKnownFolderPath(guidPtr, 0, ref.NULL, pathPtr);
if (status !== 0) {
return "Error occurred getting path: " + status;
}
let pathStr = ref.readPointer(pathPtr, 0, 200);
return pathStr.toString('ucs2').substring(0, (pathStr.indexOf('\0\0') + 1)/2);
}
// See this link for a complete list: https://msdn.microsoft.com/en-us/library/windows/desktop/dd378457(v=vs.85).aspx
const WindowsKnownFolders = {
ProgramData: "62AB5D82-FDC1-4DC3-A9DD-070D1D495D97",
Windows: "F38BF404-1D43-42F2-9305-67DE0B28FC23",
ProgramFiles: "905E63B6-C1BF-494E-B29C-65B732D3D21A",
Documents: "FDD39AD0-238F-46AF-ADB4-6C85480369C7"
}
// Enumerate common folders.
for(let [k,v] of Object.entries(WindowsKnownFolders)) {
console.log(`${k}: `, getWindowsKnownFolderPath(v));
}
I am new in WinRT ,
Is it possible to count number of images in Assest Folder . So that we can do few operation over it .
Presently i am making a small app.
Thanks in advance
try,
var folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
var files = await folder.GetFilesAsync();
and get the files count as
var filesCount = files.Count;
and you can get the files count of specific extension as
var pngFileCount = files.Where(file => file.FileType == ".png").Select(f => f).ToList().Count;
Hope this will helps you :)
Here you go.
var folder = await Package.Current.InstalledLocation.GetFolderAsync("Assets");
var options = new QueryOptions { FileTypeFilter = { ".png", ".jpg" } };
var query = folder.CreateFileQueryWithOptions(options);
var files = await query.GetFilesAsync();
foreach (var file in files)
{
// TODO
}
I want to point out that this works in Windows but not Windows Phone. Not yet.
Best is luck.
i have a folder stucture like
var/
testfolder1/
myfile.rb
data.rb
testfolder2/
home.rb
sub.rb
sample.rb
rute.rb
inside var folder contains subfolders(testfolder1,testfolder2) and some files(sample.rb,rute.rb)
in the following code returing a josn object that contains folders and files inside the var folder
like
{
'0': ['sample.rb', 'rute.rb'],
testfolder1: ['myfile.rb',
'data.rb',
],
testfolder2: ['home.rb',
'sub.rb',
]
}
code
var scriptsWithGroup = {};
fs.readdir('/home/var/', function(err, subfolder) {
if(err) return context.sendJson({}, 200);
var scripts = [];
for (var j = 0; j < subfolder.length; j++) {
var scriptsInFolder = [];
if(fs.lstatSync(scriptPath + subfolder[j]).isDirectory()) {
fs.readdirSync(scriptPath + subfolder[j]).forEach(function(file) {
if (file.substr(file.length - 3) == '.rb')
scriptsInFolder.push(file);
});
scriptsWithGroup[subfolder[j]] = scriptsInFolder;
} else {
if (subfolder[j].substr(subfolder[j].length - 3) == '.rb')
scripts.push(subfolder[j]);
}
}
scriptsWithGroup["0"] = scripts;
console.log(scriptsWithGroup)
context.sendJson(scriptsWithGroup, 200);
});
What i need is i want to return the latest modified or created files.here i only use 2 files inside folders it contains lots of files.so i want to return latest created ones
I'm going to assume here that you want only the most recent two files. If you actually want them all, just sorted, just remove the slice portion of this:
scriptsInFolder = scriptsInFolder.sort(function(a, b) {
// or mtime, if you're only wanting file changes and not file attribute changes
var time1 = fs.statSync(a).ctime;
var time2 = fs.statSync(b).ctime;
if (time1 < time2) return -1;
if (time1 > time2) return 1;
return 0;
}).slice(0, 2);
I'll add, however, that it's typically considered best practice not to use to the synchronize fs methods (e.g. fs.statSync). If you're able to install async, that would be a good alternative approach.
const fs = require('fs');
const path = require('path');
const getMostRecentFile = (dir) => {
const files = orderReccentFiles(dir);
return files.length ? files[0] : undefined;
};
const orderReccentFiles = (dir) => {
return fs.readdirSync(dir)
.filter(file => fs.lstatSync(path.join(dir, file)).isFile())
.map(file => ({ file, mtime: fs.lstatSync(path.join(dir, file)).mtime }))
.sort((a, b) => b.mtime.getTime() - a.mtime.getTime());
};
const dirPath = '<PATH>';
console.log(getMostRecentFile(dirPath));