Jump to content

replace or rename file extensions


Recommended Posts

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

Link to comment
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.???

Link to comment
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

Link to comment
Share on other sites

  • Moderators

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

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.

Link to comment
Share on other sites

i'm sure he will love to check all your scripts

maybe you could do a little combo-script (maybe 50of your scripts together in 1) and let him check :o

i'm going to think about this as well - nice idea

mhz what do you think - wanna be the autoit-free-to-use-script-checker?

Link to comment
Share on other sites

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
Link to comment
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]

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...