Sign in to follow this  
Followers 0
godspeedcapri

replace or rename file extensions

12 posts in this topic

Hi ,

I am trying to write a script which can change file extensions for all files under a specified directory.

I looked around in this forum and in the help file. There is no function to rename files. I am extracting the last three bits of the file extension using StringRight function. Could someone suggest as to how I can replace the last three bits of the file name.

Ta.

GodspeedCapri

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Maybe

$ext = .txt

$filename = My-GUI.au3

$filename = stringtrimright( $filename, 4 )

$filename = $filename & $ext

thus.....

$filename = My-GUI.txt

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

...a script which can change file extensions for all files under a specified directory.

I would do it via DOS:

rename "C:\Temp\New Folder\*.*" "*.whatever"

If you must script it, wrap a "Run" around it.


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

; ----------------------------------------------------------------------------

;

; AutoIt Version:

; Author: GodSpeedCapri

;

; Script Function:

; Delete Temporary files in specific locations

;

; ----------------------------------------------------------------------------

$pathentered = InputBox("Folder Location", "Please Enter the exact location of folder")

msgbox(0,"",$pathentered)

;$extension = InputBox("Extension","Enter the file extension required [eg: .txt,.doc,.jpg,.zoo]")

$path = $pathentered & "\"

msgbox(0,"",$path)

$rnj = FileFindFirstFile($path & "*.*")

while 1

$file = FileFindNextFile($rnj)

msgbox(0,"",$file)

if @error then ExitLoop

if stringright($file,4) == ".mp3" or stringright($file,4) == ".txt" Then

msgbox(0,"",$path)

FileMove($path & "*.*", $path & "*.zoo",9)

Else

Endif

WEnd

The above is what I am trying to..Does not work as intended...

Also the StringRegExp does not work with AutoIT Beta version.???

Share this post


Link to post
Share on other sites

maybe this

; ----------------------------------------------------------------------------
;
; AutoIt Version:
; Author: GodSpeedCapri
;
; Script Function:
; Delete Temporary files in specific locations
;
; ----------------------------------------------------------------------------

$pathentered = FileSelectFolder("Choose a folder.", "")
;$pathentered = InputBox("Folder Location", "Please Enter the exact location of folder")
MsgBox(0, "", $pathentered)
$extension = InputBox("Extension","Enter the file extension required [eg: .txt,.doc,.jpg,.zoo]")

$path = $pathentered & "\"
MsgBox(0, "", $path)
$rnj = FileFindFirstFile($path & "*.*")
While 1
    $file = FileFindNextFile($rnj)
    MsgBox(0, "", $file)
    
    If @error Then ExitLoop
    
    If StringRight($file, 4) == ".mp3" Or StringRight($file, 4) == ".txt" Then
        
        MsgBox(0, "", $path)
        
        $newfile = StringTrimRight($file, 4)
        
        FileMove($path & $file, $path & $newfile & $extension, 9)
    EndIf
    
WEnd

not tested

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

No need to test it Valuater, as MsgBox does not return error so the exitloop will never work. Just thought you would like to know.

Share this post


Link to post
Share on other sites

I'm going to start sending MHz all my scripts to proof them, I can only imagine how much more efficient I'd be!! :o


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

I'm going to start sending MHz all my scripts to proof them, I can only imagine how much more efficient I'd be!! ;)

Good Idea, just send them to MHz@dodgeit.com :geek:

Include all your backup scripts also for safe handling. :o

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I'd always search for the correct suffix, since the length will not always be the same (htm/html/au). This works for me:

$a = 0
While 1
    $a = $a + 1
    $Error = StringInStr($File, ".", 0, $a)
    If $Error = 0 Then
        $Pos = StringInStr($File, ".", 0, $a - 1)
        $Name = StringLeft($File, $Pos - 1)
        $Suffix = StringTrimLeft($File, $Pos)
        ExitLoop
    EndIf
WEnd
Edited by dabus

Share this post


Link to post
Share on other sites

Your original post states that you wish to change the file extensions for all files in a folder and yet the code that you posted has a line like this: If StringRight($file, 4) == ".mp3" Or..... and a description that reads: Delete Temporary files in specific locations

...so I'm a little confused...

I changed what you posted into these 3 lines:

$pathentered = FileSelectFolder("Choose a folder.", "")


$extension = InputBox("Extension", "Enter the file extension required [eg: .txt, .doc, .jpg, .zoo]")


RunWait(@ComSpec & " /c " & 'rename ' & '"' & $pathentered & '\*.*" "*' & $extension & '"')
That code should replace all of the file extensions in the folder selected.

I think that the DOS "rename" snytax has been the same since W95.

If you want to specify what to replace and what to replace it with - try this code:

$pathentered = FileSelectFolder("Choose a folder.", "")

$ext1 = InputBox("Extension", "To be replaced [eg: .txt, .doc, .jpg, .zoo]")

$ext2 = InputBox("Extension", "Replaced with [eg: .txt, .doc, .jpg, .zoo]")

RunWait(@ComSpec & " /c " & 'rename ' & '"' & $pathentered & '\*' & $ext1 & '" "*' & $ext2 & '"')
You can hide the "DOS window" if you wish.

...hope this helps...


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
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
Sign in to follow this  
Followers 0