Page 1 of 1
How to get date of file in script
Posted: Thu Sep 25, 2025 7:54 pm
by netaware
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
Posted: Thu Sep 25, 2025 8:35 pm
by jan_suhr
Re: How to get date of file in script
Posted: Thu Sep 25, 2025 8:50 pm
by netaware
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
Re: How to get date of file in script
Posted: Thu Sep 25, 2025 9:13 pm
by tdeschampsBluewest
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:
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)
stat will return an object similar to
this:
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
}
So birthtimeMs, birthtime, mtimeMs, and mtime will be what you are looking for
Re: How to get date of file in script
Posted: Thu Sep 25, 2025 9:18 pm
by netaware
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?
Re: How to get date of file in script
Posted: Thu Sep 25, 2025 10:53 pm
by jan_suhr
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
Re: How to get date of file in script
Posted: Fri Sep 26, 2025 2:33 pm
by netaware
Thanks for the suggestions. I will find another way

Re: How to get date of file in script
Posted: Fri Sep 26, 2025 2:56 pm
by freddyp
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
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.
Re: How to get date of file in script
Posted: Fri Sep 26, 2025 3:28 pm
by netaware
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.
Re: How to get date of file in script
Posted: Fri Sep 26, 2025 9:12 pm
by tdeschampsBluewest
netaware wrote: ↑Thu Sep 25, 2025 9:18 pm
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?
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.
Re: How to get date of file in script
Posted: Mon Sep 29, 2025 1:24 pm
by netaware
Thank you all for your insights!
I appreciate the feedback
