topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 1:50 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Replace text months to digital months  (Read 6942 times)

dcwul62

  • Supporting Member
  • Joined in 2013
  • **
  • default avatar
  • Posts: 336
    • View Profile
    • Donate to Member
Replace text months to digital months
« on: July 16, 2016, 02:27 AM »

The below is really a regular occurrence and so far I have either done nothing about it or manually corrected it.

Replacing text months (Jan* or January, Feb or February.. etc)
with -01- -02-  etc.

Example:

from
bla bla text-23june 2016.xlsx
Bla-Bla bla-bla - 2june 2016.xlsx
All bla-bla - 19 may 2016.xlsx
2nd to last bla bla 7 july 2016.xls
last bla-bla-bla-20 October 2016.xls


to:
bla bla text-23-06-2016.xlsx
Bla-Bla bla-bla - 2-06-2016.xlsx
All bla-bla - 19-05-2016.xlsx
2nd to last bla bla 7-07-2016.xls
last bla-bla-bla-20-10-2016.xls

ideal would be                            notes:
bla bla text 23-06-2016.xlsx          (-23june 2016.xlsx: removed '-' before 23, replaced june with -06-)
Bla-Bla bla-bla 02-06-2016.xlsx     (replaced \s-\s  with single space, added '0' to the 2 and rplaced june with -06-)
All bla-bla 19-05-2016.xlsx            (replaced \s-\s  with single space, replace \smay\s with -05-)
2nd to last bla bla 07-07-2016.xls  (added '0' to the 7, space-july-space with -07-)
last bla-bla-bla 20-10-2016.xls       (replaced -20 with space20 and space October space with -10-)

so
from English_full: January|February|March|April|May|June|July|August|September|October|November|December
from English_abbr: *Jan*|*Feb*|*Mar*|*Apr*|*May|*Jun*|*Jul*|*Aug*|*Sep*|*Oct*|*Nov*|*Dec*
to nmbrs : -01-|-02-|-03-|-04-|-05-|-06-|-07-|-08-|-09-|-10-|-11-|-12-

Maybe this is too complex for a single line regex and requires scripting.
Dates usually are at the filename, any type of filename, so something like:     \.(\w{3,4})

The code could be adjusted to months using local language, e.g. dutch

from Dutch_full: januari|februari|maart|april|mei|juni|juli|augustus|september|oktober|november|december
from Dutch_abbr: *jan*|*feb*|*mrt*|*apr*|*Mei|*jun*|*jl*|*aug*|*sep*|*okt*|*nov*|*dec*
to nmbrs : -01-|-02-|-03-|-04-|-05-|-06-|-07-|-08-|-09-|-10-|-11-|-12-

Any suggestions?

Thanks
=

Lintalist

  • Participant
  • Joined in 2015
  • *
  • Posts: 120
    • View Profile
    • Lintalist
    • Donate to Member
Re: Replace text months to digital months
« Reply #1 on: July 20, 2016, 11:06 AM »
I don't know if you are using AutoHotkey - https://autohotkey.com/ - but if you do and/or are willing you can try this script, you only need to set your work folder at the top of the script (here c:\testing) and the run the script, it should also create a log file so you can see what has been done. ALWAYS BACKUP your data before you start testing it.

Code: Autohotkey [Select]
  1. ; set working folder here
  2. Folder:="c:\testing"
  3.  
  4. ; build filelist
  5. Loop, %folder%\*.xlsx
  6.         filelist .= A_LoopFileName "`n"
  7.  
  8. logfile:=folder "\" A_Now ".txt"
  9.  
  10. Months:=["Jan[a-z]+","Feb[a-z]+","Ma[ar][a-z]+"
  11.         , "Apr[a-z]+","May|Mei","Jun[ie]", "Jul[iy]"
  12.         , "Aug[a-z]+", "Sep[a-z]+", "O[ck]t[a-z]+"
  13.         , "Nov[a-z]+", "Dec[a-z]+" ]
  14.  
  15. loop, parse, filelist, `n, `r
  16.         {
  17.          in:=A_LoopField
  18.          for k, v in Months
  19.                 {
  20.                  out:=RegExReplace(in,"i)-?\s*(\d{1,2})\s*(" v ")\s*"," $1-" SubStr("0" k,-1) "-")
  21.                  if (out <> in)
  22.                         {
  23.                          FileMove, %folder%\%in%,  %folder%\%out%
  24.                          FileAppend, %in% -> %out%`n, %logfile%
  25.                          Counter++
  26.                         }
  27.                 }
  28.         }
  29.  
  30. MsgBox Done, %counter% files renamed, see %logfile%
« Last Edit: July 20, 2016, 11:15 AM by Lintalist, Reason: minor typo in script »

dcwul62

  • Supporting Member
  • Joined in 2013
  • **
  • default avatar
  • Posts: 336
    • View Profile
    • Donate to Member
Re: Replace text months to digital months
« Reply #2 on: July 21, 2016, 06:54 AM »
Many many thanks for taking the time to create a script.
I really feel sorry to say that I am not familiar with autohotkey.
Whilst knowing the existence of autohotkey, but I would not know how it works.
I was hoping for a kind of regex solution of vb script that I could use with Directory Opus.

btw: in this example I took .xls as an extension, but it could be anything, .png, .tif, .doc, .pdf ... etc.

Again, thanks for taking the time!

=

tomos

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 11,959
    • View Profile
    • Donate to Member
Re: Replace text months to digital months
« Reply #3 on: July 21, 2016, 07:29 AM »
^ to run the script:

Download AHK and get it started -- I'm using a portable version, they also have an installer version.

Save script above to a file, and give the file an .AHK extension

Modify script as required:
  • path
  • replace xslx with * if you want all files

Run script file
Tom

IainB

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,540
  • @Slartibartfarst
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Replace text months to digital months
« Reply #4 on: July 21, 2016, 08:06 AM »
A useful experiential tip that I learned for document retrieval and sorting in document managementsystems: if you need/have dates in the filename, then consider putting them at the front of the filename, and in ISO format:
For example: 2016-07-22 This is the filename.xls

Thus, you can always find/sort documents by date (thus putting the date to good use), and there's no easy mistake about dates in ISO format.

Lintalist

  • Participant
  • Joined in 2015
  • *
  • Posts: 120
    • View Profile
    • Lintalist
    • Donate to Member
Re: Replace text months to digital months
« Reply #5 on: July 21, 2016, 12:44 PM »
Good tip from IainB there - I do this myself as well, makes it easy to sort :-)

If the above script works for you can call it in your file manager with some parameters. In Total Commander (TC) you can pass on the current folder, selected files etc. That way you can only process the folder or files you have selected - it (TC) can also ask you to input or confirm some data about the selected files or folder. I don't know if DO can do this as well.

But it would be fairly easy to modify the posted script so it asks you to:
- select a folder
- enter a file mask

You can also compile such a script so you have an EXE so you can use it everywhere without the need for AutoHotkey to be installed.

I would suggest you try the script as it is first and install AutoHotkey (or use the portable installer - but installing makes your life easier as you can just double click to start the script or right click run and right click compile for example)

ConstanceJill

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 205
    • View Profile
    • Donate to Member
Re: Replace text months to digital months
« Reply #6 on: July 27, 2016, 06:20 AM »
[…]
Thus, you can always find/sort documents by date (thus putting the date to good use), and there's no easy mistake about dates in ISO format.
Yup :)
Also, http://xkcd.com/1179/ (kinda relevant)

IainB

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,540
  • @Slartibartfarst
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Replace text months to digital months
« Reply #7 on: July 27, 2016, 12:23 PM »
@ConstanceJill: Yes, the xkcd cartoon is spot-on.
There's a great Dilbert one too:

Dilbert - file naming convention.jpg

magician62

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 178
    • View Profile
    • Donate to Member
Re: Replace text months to digital months
« Reply #8 on: August 01, 2016, 02:21 PM »

Thus, you can always find/sort documents by date (thus putting the date to good use), and there's no easy mistake about dates in ISO format.
Isn't this how everyone does it? :) I have been using this technique since 1982. And the ISO wasn't even published till 1988 :)
Why an I Magician62? Because Magician1 thru 61 were gone. :)

ConstanceJill

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 205
    • View Profile
    • Donate to Member
Re: Replace text months to digital months
« Reply #9 on: August 02, 2016, 04:41 AM »
Isn't this how everyone does it? :)
Well, unfortunately, no.
I tried explaining to my mother how much more logical it was to proceed this way, but...

Lintalist

  • Participant
  • Joined in 2015
  • *
  • Posts: 120
    • View Profile
    • Lintalist
    • Donate to Member
Re: Replace text months to digital months
« Reply #10 on: August 03, 2016, 04:46 PM »
@dcwul62 if you want to write your own solution using a batch file you could try GREN. I've used it for many years and found it to be very useful (but now use AHK for nearly everything) "GREN is an extremely powerful command-line renaming utility which understand Perl's regular expressions". It still works on Win 8 so might be worth a shot. A 12-24 line batch file (one for each month) still gets the job done :-)

"Official" Geocities website long gone of course but you can still find it online and download GREN.ZIP
http://web.archive.o...karpo/gren/gren.html

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: Replace text months to digital months
« Reply #11 on: August 04, 2016, 09:01 PM »
It still works on Win 8 so might be worth a shot. A 12-24 line batch file (one for each month) still gets the job done :-)

Or he could do the same thing in DOpus which is what he 'hoped' for: Script to perform multiple Regular Expressions (It will need to be updated but the basics are there.)

I have to wonder why he didn't ask on the DOpus Resource Centre forum first.