listing files from working directory
Hello
Context: I collect files from my banks and rename them to the following format : isosstartdate_ Bank_account_iosenddate. The issue I have is that one bank is providing the statement closure date in the name but not the the initial date of the statement. One way I wanted to overturn that was by fetching the end date within the filename of the previous statement file in order to use it as the start date of the new one.
A sample of javascript code I planned to use includes:
import * as fs from 'node:fs';
var files = fs.readdirSync(working_directory);
The working directory being the directory the AR application uses.
Tried to use the instruction : import * as fs from 'node:fs'; but I get an error Invalid input pre script: uncaught: 'parse error (line 1)'
Any workaround to suggest ?
Thank you
Context: I collect files from my banks and rename them to the following format : isosstartdate_ Bank_account_iosenddate. The issue I have is that one bank is providing the statement closure date in the name but not the the initial date of the statement. One way I wanted to overturn that was by fetching the end date within the filename of the previous statement file in order to use it as the start date of the new one.
A sample of javascript code I planned to use includes:
import * as fs from 'node:fs';
var files = fs.readdirSync(working_directory);
The working directory being the directory the AR application uses.
Tried to use the instruction : import * as fs from 'node:fs'; but I get an error Invalid input pre script: uncaught: 'parse error (line 1)'
Any workaround to suggest ?
Thank you
Reply to #1:
Hi Laurent,
Advanced Renamer doesn't allow the use of external modules like node.js; so importing modules just doesn't work. That is for security, and I totally agree with Kim Jensen's decision to do that (even though I'd like to have the those capabilities :).
If you have all your files in the files list you can interrogate the filenames using something like this:
ind = item.index ;
lim = (app.parseTags('<Num Files>') * 1)-1 ;
app.log( "lim = " + lim ) ;
test = 0 ;
while ( test < lim ) {
if ( ind == 0 ) {
app.log( app.getItem( test ).name ) ;
}
test++
}
If you run that code on a set of files, then check the log, you'll see the names of your files. You would need to do something like capture the names in an array or other iterable and then do whatever you need against that iterable.
If I can help let me know, my friend.
Best,
DF
Hi Laurent,
Advanced Renamer doesn't allow the use of external modules like node.js; so importing modules just doesn't work. That is for security, and I totally agree with Kim Jensen's decision to do that (even though I'd like to have the those capabilities :).
If you have all your files in the files list you can interrogate the filenames using something like this:
ind = item.index ;
lim = (app.parseTags('<Num Files>') * 1)-1 ;
app.log( "lim = " + lim ) ;
test = 0 ;
while ( test < lim ) {
if ( ind == 0 ) {
app.log( app.getItem( test ).name ) ;
}
test++
}
If you run that code on a set of files, then check the log, you'll see the names of your files. You would need to do something like capture the names in an array or other iterable and then do whatever you need against that iterable.
If I can help let me know, my friend.
Best,
DF
Reply to #2:
So I've been going back and looking at old posts to see how I can make them better. It took a month, but here's a simple script to do what Laurent was asking. He hopefully already solved the problem, but it might be interesting to someone:
My bank statements are like 20110120_MyBank.pdf; this script takes that format, moves the date to the back end, adds the opening date at the front (from the previous file), and makes adds dashes between Year/Month/Day to make them ISO dates.
EDIT: The opening date will not be completely accurate; on the first record it's just the same day the previous month, and on the rest it should actually be the day following what I used. The latter change wouldn't be hard to make, and you could always change the first filename to use an actual opening date and change the script to just use that instead. END EDIT
PRE-BATCH:
//----------------------------------
// ---------------- Add from-date to bank statement filenames ----------------
// PRE-BATCH:
// Constants:
curDateStr = "" ;
preDateStr = "" ;
curYearStr = "" ;
curMonthStr = "" ;
curDayStr = "" ;
preYearStr = "" ;
preMonthStr = "" ;
preDayStr = "" ;
const lim = ( app.itemCount ) ;
const filesArr = [] ;
// Fill array with filenames:
for ( j = 0 ; j < lim ; j++ ) {
filesArr[j] = app.getItem( j ).newBasename ;
app.log( "PRE - filesArr["+j+"] = " + filesArr[j] ) ;
}
// Functions
function f_splitDate( date ) {
curYearStr = date.slice( 0, 4 ).toString() ;
curMonthStr = date.slice( 4, 6 ).toString() ;
curDayStr = date.slice( 6, 8 ).toString() ;
}
function f_splitPreDate( date ) {
preYearStr = date.slice( 0, 4 ).toString() ;
preMonthStr = date.slice( 4, 6 ).toString() ;
preDayStr = date.slice( 6, 8 ).toString() ;
}
//----------------------------------
MAIN SCRIPT:
//----------------------------------
// ---------------- Add from-date to bank statement filenames ----------------
// ****** SEE PRE-BATCH SCRIPT ******
// capture current basename:
curFN = item.newBasename ;
// capture previous basename from filesArr[]:
preFN = filesArr[index - 1] ;
// capture current date string from curFN:
curDateStr = curFN.substring( 0, 8 ) ;
// capture the rest of the filename without end date:
fileRest = curFN.substring( 8 ) ;
// split end date into Y,M,D strings:
f_splitDate( curDateStr ) ;
// Deal with first record - no previous date to capture:
if ( index == 0 ) {
// capture current date into Y,M,D strings:
preYearStr = curYearStr ;
preMonthStr = curMonthStr ;
preDayStr = curDayStr ;
// Check if end date is first month of year;
// if yes, roll back to previous month/year date:
if ( preMonthStr = "01" ) {
preYearStr = ( preYearStr - 1 ).toString() ;
preMonthStr = "12" ;
} else {
preMonthStr = ( curMonthStr - 1 ).toString() ;
}
} else {
// if not first file, capture previous date vars with function f_splitPreDate():
f_splitPreDate( preFN.substring( 0, 8 ) ) ;
}
// return start date + filename body + end date:
return preYearStr + "-" + preMonthStr + "-" + preDayStr +
fileRest + "_" + curYearStr + "-" + curMonthStr + "-" + curDayStr ;
//----------------------------------
Screenshot: https://drive.google.com/file/d/1JGVFPhfJC7QpvmEMOAGgz0yyBCT OFDIo/view?usp=sharing
If your end date is elsewhere in the filename, the easiest way to deal would be to add a replace method above to move it to the front (and format it if necessary).
I'm not sure if anyone cares, but it's been slow around here and I'm bored. :) If anyone tries this and has problems (or not...) let me know.
Best,
DF
So I've been going back and looking at old posts to see how I can make them better. It took a month, but here's a simple script to do what Laurent was asking. He hopefully already solved the problem, but it might be interesting to someone:
My bank statements are like 20110120_MyBank.pdf; this script takes that format, moves the date to the back end, adds the opening date at the front (from the previous file), and makes adds dashes between Year/Month/Day to make them ISO dates.
EDIT: The opening date will not be completely accurate; on the first record it's just the same day the previous month, and on the rest it should actually be the day following what I used. The latter change wouldn't be hard to make, and you could always change the first filename to use an actual opening date and change the script to just use that instead. END EDIT
PRE-BATCH:
//----------------------------------
// ---------------- Add from-date to bank statement filenames ----------------
// PRE-BATCH:
// Constants:
curDateStr = "" ;
preDateStr = "" ;
curYearStr = "" ;
curMonthStr = "" ;
curDayStr = "" ;
preYearStr = "" ;
preMonthStr = "" ;
preDayStr = "" ;
const lim = ( app.itemCount ) ;
const filesArr = [] ;
// Fill array with filenames:
for ( j = 0 ; j < lim ; j++ ) {
filesArr[j] = app.getItem( j ).newBasename ;
app.log( "PRE - filesArr["+j+"] = " + filesArr[j] ) ;
}
// Functions
function f_splitDate( date ) {
curYearStr = date.slice( 0, 4 ).toString() ;
curMonthStr = date.slice( 4, 6 ).toString() ;
curDayStr = date.slice( 6, 8 ).toString() ;
}
function f_splitPreDate( date ) {
preYearStr = date.slice( 0, 4 ).toString() ;
preMonthStr = date.slice( 4, 6 ).toString() ;
preDayStr = date.slice( 6, 8 ).toString() ;
}
//----------------------------------
MAIN SCRIPT:
//----------------------------------
// ---------------- Add from-date to bank statement filenames ----------------
// ****** SEE PRE-BATCH SCRIPT ******
// capture current basename:
curFN = item.newBasename ;
// capture previous basename from filesArr[]:
preFN = filesArr[index - 1] ;
// capture current date string from curFN:
curDateStr = curFN.substring( 0, 8 ) ;
// capture the rest of the filename without end date:
fileRest = curFN.substring( 8 ) ;
// split end date into Y,M,D strings:
f_splitDate( curDateStr ) ;
// Deal with first record - no previous date to capture:
if ( index == 0 ) {
// capture current date into Y,M,D strings:
preYearStr = curYearStr ;
preMonthStr = curMonthStr ;
preDayStr = curDayStr ;
// Check if end date is first month of year;
// if yes, roll back to previous month/year date:
if ( preMonthStr = "01" ) {
preYearStr = ( preYearStr - 1 ).toString() ;
preMonthStr = "12" ;
} else {
preMonthStr = ( curMonthStr - 1 ).toString() ;
}
} else {
// if not first file, capture previous date vars with function f_splitPreDate():
f_splitPreDate( preFN.substring( 0, 8 ) ) ;
}
// return start date + filename body + end date:
return preYearStr + "-" + preMonthStr + "-" + preDayStr +
fileRest + "_" + curYearStr + "-" + curMonthStr + "-" + curDayStr ;
//----------------------------------
Screenshot: https://drive.google.com/file/d/1JGVFPhfJC7QpvmEMOAGgz0yyBCT OFDIo/view?usp=sharing
If your end date is elsewhere in the filename, the easiest way to deal would be to add a replace method above to move it to the front (and format it if necessary).
I'm not sure if anyone cares, but it's been slow around here and I'm bored. :) If anyone tries this and has problems (or not...) let me know.
Best,
DF
Reply to #3:
>> but it's been slow around here and I'm bored. :)
Well then, might I humbly suggest a different use of your time?
With Love and Respect to Kim, this forum [censored], er... is Lacking, from a usability standpoint.
So one project for you is to:
A) Gather up all the misleading, unhelpful, or just plain wrong thread titles.
B) Generate new, useful searchable titles. See: https://www.nngroup.com/articles/microcontent-how-to-write-h eadlines-page-titles-and-subject-lines/ etc.
EG: "New spelling" to something like: "How to Title-case uppercase filenames"
C) Post an "index thread" with the new (searchable) titles and links to the threads.
~~~~~~~~~~~~~~~~~
Of course if you want to be a real hero, (help) convince Kim to use any one of the existing free tools instead of this forum. My preference would be one of the Stack Overflow clones, but any widely used forum software would also work.
Advantages:
1) Users could get actual email notices of replies to their posts.
2) Users could delete their own accidental or obsolete posts (within reason).
3) Code could be pasted/shown in decent format.
4) Users could help report spam.
5) Trusted users could edit titles into usefulness/accuracy for search.
6) With a Stack Overflow-type clone, the best answers are easier to identify.
7) Most such tools already comply with insane laws and/or have a large community to help with such.
8) Easy to tag version specific threads with that version.
9) Tagging in general.
etc.
I would also help with this if needed.
>> but it's been slow around here and I'm bored. :)
Well then, might I humbly suggest a different use of your time?
With Love and Respect to Kim, this forum [censored], er... is Lacking, from a usability standpoint.
So one project for you is to:
A) Gather up all the misleading, unhelpful, or just plain wrong thread titles.
B) Generate new, useful searchable titles. See: https://www.nngroup.com/articles/microcontent-how-to-write-h eadlines-page-titles-and-subject-lines/ etc.
EG: "New spelling" to something like: "How to Title-case uppercase filenames"
C) Post an "index thread" with the new (searchable) titles and links to the threads.
~~~~~~~~~~~~~~~~~
Of course if you want to be a real hero, (help) convince Kim to use any one of the existing free tools instead of this forum. My preference would be one of the Stack Overflow clones, but any widely used forum software would also work.
Advantages:
1) Users could get actual email notices of replies to their posts.
2) Users could delete their own accidental or obsolete posts (within reason).
3) Code could be pasted/shown in decent format.
4) Users could help report spam.
5) Trusted users could edit titles into usefulness/accuracy for search.
6) With a Stack Overflow-type clone, the best answers are easier to identify.
7) Most such tools already comply with insane laws and/or have a large community to help with such.
8) Easy to tag version specific threads with that version.
9) Tagging in general.
etc.
I would also help with this if needed.
Reply to #4:
Obviously I (as a frequent user) have wished for a more refined forum infrastructure... many times. But Kim is a one-person shop, and I assume he has his hands full with maintenance/upgrades to the program, so I hesitate to bother him with this. Besides, there are several people that monitor this board and keep up with questions (including Kim, which is one more thing he has to do).
TL;DR: I'd be happy to see better forum software, but I'm not holding my breath.
I'd also be happy to index the board, but a lot of old posts are no longer applicable/available, so I assume that there's some mechanism that removes the oldest posts as a way to keep size down.
Besides, I'm as bad as anyone at posting opaque thread titles... :)
Best,
DF
Obviously I (as a frequent user) have wished for a more refined forum infrastructure... many times. But Kim is a one-person shop, and I assume he has his hands full with maintenance/upgrades to the program, so I hesitate to bother him with this. Besides, there are several people that monitor this board and keep up with questions (including Kim, which is one more thing he has to do).
TL;DR: I'd be happy to see better forum software, but I'm not holding my breath.
I'd also be happy to index the board, but a lot of old posts are no longer applicable/available, so I assume that there's some mechanism that removes the oldest posts as a way to keep size down.
Besides, I'm as bad as anyone at posting opaque thread titles... :)
Best,
DF
Reply to #5:
Whelp, I tried. (^_^)
Anyway this forum is low volume, and any changes would only increase traffic, meaning either more work for Kim, or necessitating help and/or user moderators.
As for titles, you could feed each thread into GPT (etc) and ask for a summary. Hell some AI's may be able to "Summarize each thread into a title" for the whole forum. I haven't tried, but I'm guessing the results could be glorious. ;)
UPDATE:
I tried a 5-second test with that thread:
This:
please summarize this thread: https://www.advancedrenamer.com/forum_thread/new-spelling-16 387
then
Great, but "in advanced renamer" is unnecessary
And Got:
"How to Convert Uppercase Filenames to Capitalized Case"
in Perplexity
Whelp, I tried. (^_^)
Anyway this forum is low volume, and any changes would only increase traffic, meaning either more work for Kim, or necessitating help and/or user moderators.
As for titles, you could feed each thread into GPT (etc) and ask for a summary. Hell some AI's may be able to "Summarize each thread into a title" for the whole forum. I haven't tried, but I'm guessing the results could be glorious. ;)
UPDATE:
I tried a 5-second test with that thread:
This:
please summarize this thread: https://www.advancedrenamer.com/forum_thread/new-spelling-16 387
then
Great, but "in advanced renamer" is unnecessary
And Got:
"How to Convert Uppercase Filenames to Capitalized Case"
in Perplexity
Reply to #6:
HA! AI bots hallucinate! There's news! :)
Still, it was a good idea.
HA! AI bots hallucinate! There's news! :)
Still, it was a good idea.
Reply to #5:
I do see the benefit of this forum having more functionality, but I don't see myself dedicate time for it anytime soon. If anyone wants to create a community driven forum I will support by participating, but I will not initiate it.
I do see the benefit of this forum having more functionality, but I don't see myself dedicate time for it anytime soon. If anyone wants to create a community driven forum I will support by participating, but I will not initiate it.
Reply to #8:
Fair enough. I'd help if someone does.
But I've got too many other "pending" projects to take the lead.
Fair enough. I'd help if someone does.
But I've got too many other "pending" projects to take the lead.
Reply to #9:
Yeah, there's the rub... Randy and Kim are too busy. I haven't done any web stuff in over 20 years and I don't think I have the skillset at this point to do it. Besides, I'm like a thousand years old and could kick off at any time, that wouldn't bode well for a website.
But like you, Randy, if someone built it, I would come... and help.
Yeah, there's the rub... Randy and Kim are too busy. I haven't done any web stuff in over 20 years and I don't think I have the skillset at this point to do it. Besides, I'm like a thousand years old and could kick off at any time, that wouldn't bode well for a website.
But like you, Randy, if someone built it, I would come... and help.