How to get date of file in script

Post Reply
netaware
Member
Posts: 31
Joined: Tue Jul 30, 2024 7:11 pm

How to get date of file in script

Post 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
jan_suhr
Advanced member
Posts: 692
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: How to get date of file in script

Post by jan_suhr »

Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
netaware
Member
Posts: 31
Joined: Tue Jul 30, 2024 7:11 pm

Re: How to get date of file in script

Post 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
User avatar
tdeschampsBluewest
Member
Posts: 147
Joined: Tue Jun 01, 2021 11:57 am

Re: How to get date of file in script

Post 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
Do you like the Enfocus Apps developed by Bluewest?
Feel free to leave a comment on the Appstore!
netaware
Member
Posts: 31
Joined: Tue Jul 30, 2024 7:11 pm

Re: How to get date of file in script

Post 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?
jan_suhr
Advanced member
Posts: 692
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: How to get date of file in script

Post 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
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
netaware
Member
Posts: 31
Joined: Tue Jul 30, 2024 7:11 pm

Re: How to get date of file in script

Post by netaware »

Thanks for the suggestions. I will find another way :)
freddyp
Advanced member
Posts: 1151
Joined: Thu Feb 09, 2012 3:53 pm

Re: How to get date of file in script

Post 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.
netaware
Member
Posts: 31
Joined: Tue Jul 30, 2024 7:11 pm

Re: How to get date of file in script

Post 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.
User avatar
tdeschampsBluewest
Member
Posts: 147
Joined: Tue Jun 01, 2021 11:57 am

Re: How to get date of file in script

Post 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.
Do you like the Enfocus Apps developed by Bluewest?
Feel free to leave a comment on the Appstore!
netaware
Member
Posts: 31
Joined: Tue Jul 30, 2024 7:11 pm

Re: How to get date of file in script

Post by netaware »

Thank you all for your insights!

I appreciate the feedback :)
Post Reply