Jump to content

Rename \ Adjusting file names


 Share

Recommended Posts

So I have a pptx file w 30 slides that I convert to .jpg monthly.  Working on renaming the 9 slides that have single digits by adding a 0 to the file name. I have the get file to array and file rename etc...

- Example script shows the logic used - wondering how  possibly make it smaller...

Local $sInput = "Slide1.jpg"                        ;-- File name
Local $len = StringLen($sInput)                     ;-- Get string length
if $len = 10 then                           ;-- If string = 10 continue - "else"
   Local $var = StringRight($sInput, 5)                 ;-- Hold right 5 places
   Local $result = StringTrimRight($sInput, 4)              ;-- Trim right 4 placed
   Local $sOutput = StringRegExpReplace($result, "[123456789]", "0")    ;-- replace digits with 0
   MsgBox(0, "Result", $sInput & " - is now - " & $sOutput & $var )     ;-- put back together
else
   MsgBox(0, "Results", "Incorrect target Length" )             ;-- If not string length = 10
EndIf

Luv Autoit - Use it daily.

Much Thanks

I Break and Fix things... If the surf is up I'm outta here.....

Link to comment
Share on other sites

Local $sInput = "Slide1.jpg"                        ;-- File name
Local $len = StringLen($sInput)                     ;-- Get string length
if $len = 10 then                           ;-- If string = 10 continue - "else"
   ;Local $var = StringRight($sInput, 5)                 ;-- Hold right 5 places
  ; Local $result = StringTrimRight($sInput, 4)              ;-- Trim right 4 placed
   Local $sOutput = StringRegExpReplace($sInput, "[123456789]", "0")    ;-- replace digits with 0
   MsgBox(0, "Result", $sInput & " - is now - " & $sOutput & $var )     ;-- put back together
else
   MsgBox(0, "Results", "Incorrect target Length" )             ;-- If not string length = 10
EndIf

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I should have stated that the out put should be -

Slide1.jpg = Slide01.jpg

Slide2.jpg = Slide02.jpg

and so on - through the 9 slides.

note todays surf report

- North Shore - 6-8ft  8-15ft face  slight side on shore wind..  little choppy -  I'm going surfing....

I Break and Fix things... If the surf is up I'm outta here.....

Link to comment
Share on other sites

Try this on your file names.
 

Local $sOutput = StringRegExpReplace("Slide1.jpg", "([a-zA-Z]+)(\d{1})(\..+)", "${1}0$2$3") ;-- add a 0 before single digit
MsgBox(4096, "Test", $sOutput)

You'll get the file name, followed by at least 2 digits, followed by ".jpg", if the file name already contains more than 1 digit (Slide11.jpg for example), it will return the file name as-is, if the digits are less than 2 it adds a zero in front of the digit.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • 2 weeks later...

Bunch of thanks - BrewManNH - Set me on a path learning more StringRegExp and patterns... From what I've learned I've been able to use StringRegExpReplace often in my schedule project. Project is running well after two days of tests. Still learning and crunching my codes down in size. Mahalo...

I Break and Fix things... If the surf is up I'm outta here.....

Link to comment
Share on other sites

BrewmanNH - I don't get the curly braces in the first replacement group.  Can you explain?

I also don't get why it works with the following

Local $sOutput = StringRegExpReplace("Slide1A9.jpg", "([a-zA-Z]+)(\d{1})(\..+)", "${1}0$2$3") ;-- add a 0 before single digit
MsgBox(4096, "Test", $sOutput)

It seems like the pattern should fail at the first digit not followed by a period.

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

An other way

$string = "Slide1A9.jpg"
Local $sOutput = Execute(StringRegExpReplace($string, '(.+\D)(\d+)(\..+)', "'$1' & StringFormat('%02i', '$2') & '$3' & ") & "''")
MsgBsox(4096, "Test", $sOutput)

Slightly different from BrewMaNH's if you apply on "Slide1A.9.jpg" or "Slide1.A.9.jpg" or "Slide1A_9.jpg" etc

Edit

kylomas,

${1}0 is used to avoid a possible confusion with $10

Edited by mikell
Link to comment
Share on other sites

MPHillier,

See links in my signature for regexp tutorials. Beware that there are a number of slightly different regexp engines floating around. AutoIt engine is called PCRE (Perl Compatible Regular Expressions).

You can also put StringFormat to good use:

For $i = 1 To 30
    ConsoleWrite(StringFormat("Slide%02i.jpg", $i) & @LF)
Next

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

There is no black magic here, just backtracking:

this part ([a-zA-Z]+)(\d{1}) first matches Slide1 but then (\..+) fails as the next character is A and not a dot.
So we backtrack, thus leaving Slide1 untouched and not affected (hence it will get verbatim in the output) and get
([a-zA-Z]+)(\d{1}) match A9, then (\..+) matches .jpg and we're done.

The output now is:
Slide1       (verbatim because not involved in the match)
A            as $1
0            inserted in replacement pattern
9            as $2
.jpg         as $3

Does the mud clear now?
Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

I'd say:

Local $sOutput = StringRegExpReplace("Slide1A9.jpg", "((?<!\d)\d)(\..+)", "0$1$2")
MsgBox(4096, "Test", $sOutput)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...