Jump to content

Rename filename + extension


Recommended Posts

Dear AutoIt members,

I want to make a simple script: rename file-name (inputbox) with the extension rar if the extension ends up with part001.rar, part002.rar and so on rename to r01, r02

renaming the the extension is not the problem i found on this forum a good example script

now renaming the filename with the given inputbox name

FileMove($Directory & "\" & $files[$x], $Directory & "\" & $Input1[1] & ".r0" & $x)
above code is wrong it was

FileMove($Directory & "\" & $files[$x], $Directory & "\" & $files[1] & ".r0" & $x)

this is just for example to show

#NoTrayIcon
#include <file.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", 1)

Local $Directory = @ScriptDir

Local $Gui, $Input1, $Button1, $nMsg, $i
$Gui = GUICreate("Form1", 184, 91, 192, 124)
$Input1 = GUICtrlCreateInput("", 24, 16, 144, 21)
$Button1 = GUICtrlCreateButton("Button1", 24, 48, 145, 25, $WS_GROUP)
GUISetState(@SW_SHOW)

$i = 1
While 1
If GUICtrlRead($Input1) = "" And $i = 1 Then
GUICtrlSetState($Button1, $GUI_DISABLE)
$i = 0
Else
If $i = 0 And GUICtrlRead($Input1) <> "" Then
GUICtrlSetState($Button1, $GUI_ENABLE)
$i = 1
EndIf
EndIf

$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
_rar01()
EndSwitch
WEnd


Func _rar01()
Local $files, $filename, $x
$files = _FileListToArray($Directory, "*.rar", 1)

For $x = 1 To UBound($files) - 1
$filename = StringSplit($files[$x], ".")
If $x < 10 Then
FileMove($Directory & "\" & $files[$x], $Directory & "\" & $Input1[1] & ".r0" & $x)
Else
FileMove($Directory & "\" & $files[$x], $Directory & "\" & $Input1[1] & ".r" & $x)
EndIf
Next

EndFunc

Thanks

Edited by Mecano
Link to comment
Share on other sites

Replace with this

If $x < 10 Then
FileMove($Directory & "\" & $files[$x], $Directory & "\" & $filename[$filename[0]-1] & ".r0" & $x)
Else
FileMove($Directory & "\" & $files[$x], $Directory & "\" & $filename[$filename[0]-1] & ".r" & $x)
EndIf

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

It would be even better if written like this.

FileMove($Directory & "\" & $files[$x], $Directory & "\" & $filename[$filename[0] - 1] & ".r" & StringFormat("%02s", $x)

This way you don't need the If..Then statement, it will just format the string with either one or 2 0's depending upon how many digits the number $x is.

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

Making progress,

now

old file new file + ext

example-file.part001 -> part001.r01

example-file.part002 -> part001.r02

example-file.part003 -> part001.r03

have to become like this

inputbox name example: "new"

old file new file + ext

example-file.part001 -> new.r01

example-file.part002 -> new.r02

example-file.part003 -> new.r03

@BrewManNH

error

did I overlooked something?

(46,123) : ERROR: syntax error

FileMove($Directory & "\" & $files[$x], $Directory & "\" & $filename[$filename[0] - 1] & ".r" & StringFormat("%02s", $x)

Link to comment
Share on other sites

I missed the final closing parentheses on the FileMove command it should be like this.

FileMove($Directory & "\" & $files[$x], $Directory & "\" & $filename[$filename[0] - 1] & ".r" & StringFormat("%02s", $x))
Edited by BrewManNH

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

thx BrewManNH,

Any idea how to get the value from: $Input1 = GUICtrlCreateInput(""

to file name

FileMove($Directory & "" & $files[$x], $Directory & "" & $filename[$filename[0] - 1] & ".r" & $x)

This is not working, I know it's a simple, looking afterwards

FileMove($Directory & "" & $files[$x], $Directory & "" & $filename[$Input1[0] - 1] & ".r" & $x)

: ==> Subscript used with non-Array variable.:

Link to comment
Share on other sites

FileMove($Directory & "" & $files[$x], $Directory & "" & GUICtrlRead($Input1) & ".r" & $x) Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

  • 2 weeks later...

Replace with this

If $x < 10 Then
FileMove($Directory & "\" & $files[$x], $Directory & "\" & $filename[$filename[0]-1] & ".r0" & $x)
Else
FileMove($Directory & "\" & $files[$x], $Directory & "\" & $filename[$filename[0]-1] & ".r" & $x)
EndIf

Just a question about $filename[$filename[0]-1]

let say you have the filename: test.dummy.part.01.rar this will end up like

testdummy.dummy.r01

 

How to keep test.dummy.r01 ?

Thanks in advance

Edit 1

spell

Edit 2

Better example

Edited by Mecano
Link to comment
Share on other sites

This is how i would do it:

#NoTrayIcon
#include <file.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", 1)

Local $Directory = @ScriptDir
Local $Count

Local $Gui, $Input1, $Button1, $nMsg, $i
$Gui = GUICreate("Form1", 184, 91, 192, 124)
$Input1 = GUICtrlCreateInput("", 24, 16, 144, 21)
$Button1 = GUICtrlCreateButton("Button1", 24, 48, 145, 25, $WS_GROUP)
GUISetState(@SW_SHOW)

$i = 1
While 1
If GUICtrlRead($Input1) = "" And $i = 1 Then
GUICtrlSetState($Button1, $GUI_DISABLE)
$i = 0
Else
If $i = 0 And GUICtrlRead($Input1) <> "" Then
GUICtrlSetState($Button1, $GUI_ENABLE)
$i = 1
EndIf
EndIf

$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
_rar01()
EndSwitch
WEnd


Func _rar01()
Local $files, $filename, $x
$files = _FileListToArray($Directory, "*.rar", 1)
For $x = 1 To UBound($files) - 1
$Count = StringInStr ($files[$x], ".", 2, -3)
$filename = StringTrimRight ($files[$x], $Count)
If $x < 10 Then
FileMove($Directory & "\" & $files[$x], $Directory & "\" & $filename & "r0" & $x)
Else
FileMove($Directory & "\" & $files[$x], $Directory & "\" & $filename & "r" & $x)
EndIf
Next
EndFunc
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

This is how i would do it:

#NoTrayIcon#include #include #include #include #include Opt("MustDeclareVars", 1)Local $Directory = @ScriptDirLocal $CountLocal $Gui, $Input1, $Button1, $nMsg, $i$Gui = GUICreate("Form1", 184, 91, 192, 124)$Input1 = GUICtrlCreateInput("", 24, 16, 144, 21)$Button1 = GUICtrlCreateButton("Button1", 24, 48, 145, 25, $WS_GROUP)GUISetState(@SW_SHOW)$i = 1While 1If GUICtrlRead($Input1) = "" And $i = 1 ThenGUICtrlSetState($Button1, $GUI_DISABLE)$i = 0ElseIf $i = 0 And GUICtrlRead($Input1) <> "" ThenGUICtrlSetState($Button1, $GUI_ENABLE)$i = 1EndIfEndIf$nMsg = GUIGetMsg()Switch $nMsgCase $GUI_EVENT_CLOSEExitCase $Button1_rar01()EndSwitchWEndFunc _rar01()Local $files, $filename, $x$files = _FileListToArray($Directory, "*.rar", 1)For $x = 1 To UBound($files) - 1$Count = StringInStr ($files[$x], ".", 2, -3)$filename = StringTrimRight ($files[$x], $Count)If $x < 10 ThenFileMove($Directory & "\" & $files[$x], $Directory & "\" & $filename & "r0" & $x)ElseFileMove($Directory & "\" & $files[$x], $Directory & "\" & $filename & "r" & $x)EndIfNextEndFunc

Thank you, this works great.

Can you explain what 2, -3 does?

$Count = StringInStr ($files[$x], ".", 2, -3)

I try to learn and understand :think:

Edit;

Just read the help file, thanks

Edit: 2

After some other testing, it's not where I looking for

Edited by Mecano
Link to comment
Share on other sites

The help file states that:

2 = not case sensitive, using a basic/faster comparison

Next parameter = Which occurrence of the substring to find in the string. Use a negative occurrence to search from the right side. The default value is 1 (finds first occurrence).

So, -3 means the third occurence starting the count from the right...Searches for the third dot and retrieves it's position.

StringTrimRight, cuts the string by so many characters as defined in the previous instruction.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

Oops,
How to get the first file from the array
$files = _FileListToArray($Directory, "*.rar", 1)
 
When renaming the first file (part001.rar) this must be .rar the files after .rar: must be  .r00, .r01, .r02, .r03...
Searching the forum give me _FO_FileSearch ,FileFindFirstFile
 
arrays are not my best skills

Sorry,

thanks in advance

Edited by Mecano
Link to comment
Share on other sites

The first file will be $files[1]

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

@careca,
all clear, sounds logic thanks
 
After longer testing
test.dummie.part001.rar makes test.dummie.rar, test.dummie.r02 , test.dummie.r03...
how to keep test.dummie.r01 ?
 

 Func _rar01()
Local $files, $filename, $x
$files = _FileListToArray($Directory, "*.rar", 1)
For $x = 1 To UBound($files) - 1
$Count = StringInStr ($files[$x], ".", 2, -2)
$filename = StringTrimRight ($files[$x], $Count)

FileMove($Directory & "\" & $files[1], $Directory & "\" & $filename & ".rar")
FileMove($Directory & "\" & $files[$x], $Directory & "\" & $filename & ".r0" & StringFormat("%01s", $x))

Next
EndFunc
Link to comment
Share on other sites

Hi, do you want it to start with .rar, or should the first one be the .r1?

xxx.rar

xxx.r1

xxx.r2

xxx.r3

Also, keep in mind that, the parameters are set to a specific filename, i mean, you mentioned before the filename "test.dummy.part.01.rar" now you're talking about "test.dummie.part001.rar" These are different, mainly because the second one has more dots.

Now to make it work like you want for that specific filetype, try this:

Func _rar01()
Local $files, $filename, $x
$files = _FileListToArray($Directory, "*.rar", 1)
For $x = 1 To UBound($files) ;- 1
$Count = StringInStr ($files[$x], ".", 2, -2)
$filename = StringTrimRight ($files[$x], $Count)
If $x < 10 Then
FileMove($Directory & "\" & $files[$x], $Directory & "\" & $filename & ".r0" & $x)
Else
FileMove($Directory & "\" & $files[$x], $Directory & "\" & $filename & ".r" & $x)
EndIf
Next
EndFunc









_

"

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

Regular expression way

;Working - Pattern
;Input : anything "." $s_sub (either "." or a digit) twodigits "." $s_ext
;Output : anything "." (first alphabet of $s_ext) twodigits


$String = "test.dummy.part.03.rar"
MsgBox( 64, "Return:", GetFileName_RegEx($String))

$String = "test.dummy.part001.rar"
MsgBox( 64, "Return:", GetFileName_RegEx($String))

Func GetFileName_RegEx( $s_filename, $s_ext = "rar", $s_sub = "part" )

    ;the file is not of the specified extension.
    If StringRight($s_filename, StringLen($s_ext)) <> $s_ext Then Return SetError(1, 0, 0)

    ;the following would be used in pattern so make sure there don't have any metachars.
    Escape_SpecialChars($s_ext)
    Escape_SpecialChars($s_sub)

    ConsoleWrite('$s_filename = ' & $s_filename & @crlf) ;### Debug Console
    ConsoleWrite('$s_ext = ' & $s_ext & @crlf) ;### Debug Console
    ConsoleWrite('$s_sub = ' & $s_sub & @crlf) ;### Debug Console

    Return StringRegExpReplace( $s_filename, "([^.]*)\." & $s_sub & "(?:\.|\d)(\d{2})(\." & $s_ext & ")$", "\1." & StringLeft($s_ext, 1) & "\2" )

EndFunc


Func Escape_SpecialChars( ByRef $SRE )
    $SRE = StringRegExpReplace($SRE, "[\\\-^]", "\\$0") ;the chars a set treats as special - escape them.
    $SRE = StringRegExpReplace($SRE, "[^\w\s]", "[$0]") ;insert any non-word/space in sets
EndFunc

Regards :)

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Hi, do you want it to start with .rar, or should the first one be the .r1?

 

xxx.rar

xxx.r1

xxx.r2

xxx.r3

 

Also, keep in mind that, the parameters are set to a specific filename, i mean, you mentioned before the filename "test.dummy.part.01.rar" now you're talking about "test.dummie.part001.rar" These are different, mainly because the second one has more dots.

_

"

Yes like test.dummie.rar | test.dummie.r01 | test.dummie.r02 etc. sorry was not clear

 

careca wrote: parameters are set to a specific filename $Count = StringInStr($files[$x], ".", 2, -2)

I noticed, that's why I changed it to -2

 

the reason is I've tried to make it more flexible

 

the result from the last code you gave all files are with .r01 , .r02 etc.

 

Thanks for looking

 

 

@PhoenixXL

 

Regular expression way / StringRegExp and patterns is way above my head it's like starting to read a study book and reading the last pages, I've no clue to place these in the script.

Thank you so much for your contribution (a new study case)

:thumbsup:

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

×
×
  • Create New...