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.
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 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?
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 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));
How to read image from Application folder in winjs
var item = groupedProducts.getAt(indx);
item.img = Windows.Storage.ApplicationData.current.localFolder.path + "\\" + "3766111.jpg";
groupedProducts.setAt(indx, item);
WinJS.UI.processAll();
You need to use the async APIs to access files in ApplicationData in WinJS, such as the getFileAsync function used below (this is a helper function I use in databinding for one of my apps):
function getLocalLargeMapTile(item) {
return new WinJS.Promise(
function (completed, error, progress) {
var filename;
var sourceFolder;
if (item.latlong) {
var latandlong = item.latlong.split(", ");
var lat = latandlong[0];
var lon = latandlong[1];
filename = lat + lon + ".png";
var appData = Windows.Storage.ApplicationData.current;
sourceFolder = appData.localFolder;
sourceFolder.getFileAsync(filename).then(function (file) {
var mapUrl = window.URL.createObjectURL(file, { oneTimeOnly: true });
completed(mapUrl);
},
function (error) {
handleError(error)
});
}
else {
filename = "ms-appx:///images/megaphone_256x256.png";
completed(filename);
}
}
);
}
What I'm doing in the helper function is checking whether my data includes a latitude and longitude, and if so, checking for a file with a matching filename, and since those files are in the Application Data folder, wrapping the file with an objectURL and returning a promise with the objectURL. Otherwise, I simply return an ms-appx url pointing to a static file in the app's images folder. Here's how I call this helper function, from a programmatic template (I don't think you can do this with a declarative template):
var image = document.createElement("img");
image.className = "item-image";
image.src = "ms-appx:///images/megaphone_256x256.png";
result.appendChild(image);
// additional code omitted
var promise = mapTileUtil.getLocalMapTile(currentItem);
promise.done(function (mapTileUrl) {
image.src = mapTileUrl;
});
For more info on templating functions, which provide greater control over the rendered markup than declarative templates, check out:
http://msdn.microsoft.com/en-us/library/windows/apps/jj585523.aspx
and
http://go.microsoft.com/fwlink/p/?linkid=231499
For more information on Windows Store app development in general, register for App Builder.