Add date to filename and change extension?

Discuss and share scripts and script files...
Post Reply
avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

Add date to filename and change extension?

Post by avsfan »

I've just started playing around with scripting a little bit, and am getting unpredictable results...

The situation is that when I scan documents to pdf format, the program always uses an uppercase extension (i.e. filename.PDF).

What I'd like to do is have a script that will change the extension from .PDF to .pdf, and change the basename to append today's date. i.e. filename.PDF becomes filename-20090727.pdf.

The script that I came up with and seems to *sometimes* work (but not reliably), looks like this:

Code: Select all

rename r, ".PDF > .pdf";rename b, "*-<datem yyyymmdd>", p;
I'm sure you scripting wizards can help me do this better -- help, please!

thanks!

andy

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Add date to filename and change extension?

Post by TheQwerty »

avsfan wrote:

Code: Select all

rename r, ".PDF > .pdf";rename b, "*-<datem yyyymmdd>", p;
<datem ...> uses an item's date modified. For the current date just use <date ...>, so:

Code: Select all

rename r, ".PDF > .pdf";rename b, "*-<date yyyymmdd>", p;
You could also replace the extension case changing with: #136;:

Code: Select all

#136;rename b, "*-<date yyyymmdd>", p;

EDIT: Some other hints... 'rename r, ".PDF > .pdf"' wouldn't work exactly as you'd expect for two reasons.
1) The regular expression would search for all instances of ".PDF" in the filename, not just the extension, so "A.PDF FILE.PDF" would get renamed to "A.pdf FILE.pdf". You can avoid this by telling it that the pattern is at the end of the string using '$', or 'rename r, ".PDF$ > .pdf". (Likewise you can use '^' to define the beginning of the string.)

2) In a regular expression '.' would translate into any character. So it would rename "bPDF.txt" to ".pdf.txt". You'd have to escape '.' by using '\.' if you want it to truly match a period.

avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

Re: Add date to filename and change extension?

Post by avsfan »

Thanks much for the additional clarification!
TheQwerty wrote:You could also replace the extension case changing with: #136;:

Code: Select all

#136;rename b, "*-<date yyyymmdd>", p;
Thanks for the helpful information. One thing I noticed is that if I'm on a preview tab, the extension change doesn't happen (whether doing the rename special using #136 or from the menu), but the preview stops (i.e. it's not shown in the preview window any more). However, if I do the command twice (once to "clear" the preview, and once for the rename), I get the expected result.

So if I use the command:

Code: Select all

#136;#136;rename b, "*-<date yyyymmdd>", p;
all seems to work.

EDIT: well, it works in "try script" mode, but not as a single operation... If the preview mode is off, it's all fine, but not in preview mode...

If I add #136; at the end, it doesn't make any difference (or if I have two of them at beginning and/or end...)

It appears we have found a small bug in the script renaming... probably the same one we saw when trying to rename a previewed file using slow double-click. That issue is now fixed, but apparently the issue still exists with these commands -- or at least this one).

andy

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Add date to filename and change extension?

Post by TheQwerty »

You might be able to avoid that problem by calling #260; (Find Files) first, but it's a bad solution.
Regardless of if you were previewing the file or not (or even if the IP is closed) you'll now be on the Find Files tab.

Though I'm guessing if it executes the #136 before waiting for the preview to close it will probably execute it before waiting for the Find Files tab to be active.

It probably is a bug but it also demonstrates the need for better scripting commands for controlling the IP. (Close IP, Turn off Preview, Get IP state, blah blah...)

avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

Re: Add date to filename and change extension?

Post by avsfan »

Well, rather than calling find files, I could use command #1020 (Select Info Panel Tab / Properties), or #1021 (Version Tab) -- it seems that should accomplish the same thing, but without the find...

There's also the command to toggle previewing, but to use it effectively, I'd need to know whether preview was currently on or not...

However, both #1020 and #1021 seem not to work if starting on the Preview Tab -- the downcase rename doesn't work. In fact, if I use #665 (hide/show IP) to completely hide the IP, the downcase extension still fails. So XY still thinks the file is locked from the preview even after a variety of scripting commands that should remove the lock... (including multiple commands i.e. #1020;#1021;#665; prior to the rename attempt...)

Seems definitely like a bug to me...

For now, I'll just make sure I'm not on the preview tab when running this command...

avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

Re: Add date to filename and change extension?

Post by avsfan »

avsfan wrote:

Code: Select all

#136;rename b, "*-<date yyyymmdd>", p;
doesn't work if the file is being previewed...

It appears we have found a small bug in the script renaming... probably related to the one we saw when trying to rename a previewed file using slow double-click. That issue is now fixed, but apparently the issue still exists with these commands -- or at least this one).

For now, I'll just make sure I'm not on the preview tab when running this command...
Or, now that Don's back, maybe he'll feel inspired to have the rename script command make sure the file is unlocked prior to the rename... :)

Welcome Back, Don!

admin
Site Admin
Posts: 66075
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Add date to filename and change extension?

Post by admin »

avsfan wrote:Or, now that Don's back, maybe he'll feel inspired to have the rename script command make sure the file is unlocked prior to the rename... :)

Welcome Back, Don!
Thanks!

Renaming previewed files works with all files AFAIK, apart from DOCs and PDFs because they like to hang around in memory for an indefinite time even after closing them. I don't know any good way to handle this.

avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

Re: Add date to filename and change extension?

Post by avsfan »

Ah, ok... that's very odd... maybe at some point new info will come to light on how this can be done... (though it *does* work OK with the slow-double-click... maybe some way to add a delay for pdf and doc files? just thinking in public...)

thanks!

andy

admin
Site Admin
Posts: 66075
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Add date to filename and change extension?

Post by admin »

avsfan wrote:Ah, ok... that's very odd... maybe at some point new info will come to light on how this can be done... (though it *does* work OK with the slow-double-click... maybe some way to add a delay for pdf and doc files? just thinking in public...)

thanks!

andy
I tried all sorts of delay: no success.

slow-double-click: works because since a couple of weeks I close the preview before you even trigger the rename.

avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

Re: Add date to filename and change extension?

Post by avsfan »

admin wrote:slow-double-click: works because since a couple of weeks I close the preview before you even trigger the rename.
Is there a script command I can use to close the preview (regardless of whether it's actually previewed or not -- i.e. a "force-close") before triggering the rename in the script? (or will it still not make any difference?)

Thanks!

andy

admin
Site Admin
Posts: 66075
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Add date to filename and change extension?

Post by admin »

avsfan wrote:
admin wrote:slow-double-click: works because since a couple of weeks I close the preview before you even trigger the rename.
Is there a script command I can use to close the preview (regardless of whether it's actually previewed or not -- i.e. a "force-close") before triggering the rename in the script? (or will it still not make any difference?)
I don't think there is a straight way to close a preview via scripting (I'm still half on the beach...). I should add a "Close Preview" command to Miscellaneous probably...

avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

Re: Add date to filename and change extension?

Post by avsfan »

admin wrote:
avsfan wrote:
admin wrote:slow-double-click: works because since a couple of weeks I close the preview before you even trigger the rename.
Is there a script command I can use to close the preview (regardless of whether it's actually previewed or not -- i.e. a "force-close") before triggering the rename in the script? (or will it still not make any difference?)
I don't think there is a straight way to close a preview via scripting (I'm still half on the beach...). I should add a "Close Preview" command to Miscellaneous probably...
I looked, but all I could see were toggles (so there's no way to force a preview on, either, I believe...)

Commands to Open Preview and Close Preview could be very handy, perhaps... So I'll cast my vote here!

Thanks, Don! (and I hope the beach was WONDERFUL!)

andy

Post Reply