How to get date of file in script
How to get date of file in script
Hello,
I am writing a script and want to get the creation or lastmod date of a file, but i am having problems.
I am wanting to sort these 2 files by creation or lastmod date.
Here is what i have in my script:
var testDir = new Dir ( "D:\\Testing\\scripting" );
var theFiles = testDir.entryList( 'Testing*', Dir.Files );
var File1 = new File( theFiles[0] );
var File1Date = theFiles[0].created; // I also tried var File1Date = File1.lastModified // but nothing gets returned.
but when i look in the log i get an error message Trying to access undefined member 'created'
Can someone help me get this file info?
Thank you
I am writing a script and want to get the creation or lastmod date of a file, but i am having problems.
I am wanting to sort these 2 files by creation or lastmod date.
Here is what i have in my script:
var testDir = new Dir ( "D:\\Testing\\scripting" );
var theFiles = testDir.entryList( 'Testing*', Dir.Files );
var File1 = new File( theFiles[0] );
var File1Date = theFiles[0].created; // I also tried var File1Date = File1.lastModified // but nothing gets returned.
but when i look in the log i get an error message Trying to access undefined member 'created'
Can someone help me get this file info?
Thank you
Re: How to get date of file in script
i tried but not working:
const fs = require('node:fs');
var newVar = fs.stat("D:\\Testing\\scripting\\text.txt", stats);
job.log( -1, newVar);
Error in line 25 of script : 'require' undefined or not a function
const fs = require('node:fs');
var newVar = fs.stat("D:\\Testing\\scripting\\text.txt", stats);
job.log( -1, newVar);
Error in line 25 of script : 'require' undefined or not a function
- tdeschampsBluewest
- Member
- Posts: 147
- Joined: Tue Jun 01, 2021 11:57 am
Re: How to get date of file in script
Can you clarify in which part of Switch you’re trying to achieve this, and in which language?
There are 3 scripting “modes” within Switch:
- Legacy scripting
- NodeJS
- TypeScript
I’m assuming you’re working in legacy, which really shouldn’t be used anymore.
In TypeScript, it should look like this:
stat will return an object similar to this:
So birthtimeMs, birthtime, mtimeMs, and mtime will be what you are looking for
There are 3 scripting “modes” within Switch:
- Legacy scripting
- NodeJS
- TypeScript
I’m assuming you’re working in legacy, which really shouldn’t be used anymore.
In TypeScript, it should look like this:
Code: Select all
import fs from "fs" // or in nodeJS : const fs = require("fs")
const myPath = "D:\Testing\scripting\text.txt"
const stat = fs.statSync(myPath)
Code: Select all
{
dev: 2114,
ino: 48064969,
mode: 33188,
nlink: 1,
uid: 85,
gid: 100,
rdev: 0,
size: 527,
blksize: 4096,
blocks: 8,
atimeMs: 1318289051000.1,
mtimeMs: 1318289051000.1,
ctimeMs: 1318289051000.1,
birthtimeMs: 1318289051000.1,
atime: Mon, 10 Oct 2011 23:24:11 GMT,
mtime: Mon, 10 Oct 2011 23:24:11 GMT,
ctime: Mon, 10 Oct 2011 23:24:11 GMT,
birthtime: Mon, 10 Oct 2011 23:24:11 GMT
}
Do you like the Enfocus Apps developed by Bluewest?
Feel free to leave a comment on the Appstore!
Feel free to leave a comment on the Appstore!
Re: How to get date of file in script
Hello,
I am using legacy scripting. I am a novice when it comes to scripting, especially Node.js and Typescript.
I have done lots of scripts in Powershell.
If i use node.js, i cannot create/edit in the SwitchScripter, correct?
I am using legacy scripting. I am a novice when it comes to scripting, especially Node.js and Typescript.
I have done lots of scripts in Powershell.
If i use node.js, i cannot create/edit in the SwitchScripter, correct?
Re: How to get date of file in script
Here you find information about how to use NodeJS
https://enfocus.com/en/learn/switch#scriptingtrail
I think you need a maintenance contract to access it.
And you can also ask ChatGPT for help, just speicfy that you work with Enfocus Switch
https://enfocus.com/en/learn/switch#scriptingtrail
I think you need a maintenance contract to access it.
And you can also ask ChatGPT for help, just speicfy that you work with Enfocus Switch
Re: How to get date of file in script
Thanks for the suggestions. I will find another way 

Re: How to get date of file in script
Are you sure that theFiles contains the correct list of files? Add job.log(-1, theFiles.length) and/or job.log(-1, theFiles) to investigate.netaware wrote: ↑Thu Sep 25, 2025 7:54 pm Hello,
I am writing a script and want to get the creation or lastmod date of a file, but i am having problems.
I am wanting to sort these 2 files by creation or lastmod date.
Here is what i have in my script:
var testDir = new Dir ( "D:\\Testing\\scripting" );
var theFiles = testDir.entryList( 'Testing*', Dir.Files );
var File1 = new File( theFiles[0] );
var File1Date = theFiles[0].created; // I also tried var File1Date = File1.lastModified // but nothing gets returned.
but when i look in the log i get an error message Trying to access undefined member 'created'
Can someone help me get this file info?
Thank you
Re: How to get date of file in script
freddyp,
Yes it does have the files. I was thinking since it is an array, maybe theFiles variable would contain more info than just the name.
I was hoping for each file (2) I could maybe get the date last modified info for each file in variables.
Yes it does have the files. I was thinking since it is an array, maybe theFiles variable would contain more info than just the name.
I was hoping for each file (2) I could maybe get the date last modified info for each file in variables.
- tdeschampsBluewest
- Member
- Posts: 147
- Joined: Tue Jun 01, 2021 11:57 am
Re: How to get date of file in script
If you’re just starting out, sticking with Node.js is totally fine, TypeScript adds an extra layer with compiling, which might be more than you need right now.
And yes, SwitchScripter editing is only for legacy scripts. For Node.js, you can right-click a newly created script element in Switch and select "Create new script." That’ll generate a folder with everything set up, including a main.js you can edit in VS Code or any editor you like.
When you’re ready to level up, TypeScript is worth the hassle, it’s basically JavaScript with types and better structure, it will avoid you a TON of easy mistake.
Do you like the Enfocus Apps developed by Bluewest?
Feel free to leave a comment on the Appstore!
Feel free to leave a comment on the Appstore!
Re: How to get date of file in script
Thank you all for your insights!
I appreciate the feedback
I appreciate the feedback
