Jump to content
Bob1988

rename file extensions

Recommended Posts

Bob1988

 

Hi ,

I have a folder, consists of several files and a database file (example.db)

I am trying to write a script which can copy and at the same time change file's extensions for all files except the data base file. (example.db)

How i can change the cod that i can chang file's extensions for all files except the data base file and rename the data base file from "exampel.db" to "exam.db"

#include <GUIConstants.au3>
#include <ComboConstants.au3> ; For GUICtrlCreateCombo
#include <File.au3> ; For _FileListToArray

Global $ComboExt, $Files ; For Find Files.

$Form1     = GUICreate("file's copy & rename the Extension ", 618, 134, 193, 125)
$Input_old = GUICtrlCreateInput("", 16, 16, 321, 21)
$Datei     = GUICtrlCreateButton("choose folder or file's ", 352, 16, 257, 25, 0)
$Go        = GUICtrlCreateButton("copy and rename ", 136, 88, 361, 41, 0)
$Input_new = GUICtrlCreateInput("", 16, 48, 321, 21)
$Button1   = GUICtrlCreateButton("new path or folder", 352, 48, 257, 25, 0)

GUICtrlSetCursor($Datei, 0)   ; ==> Added.
GUICtrlSetCursor($Go, 0)      ; ==> Added.
GUICtrlSetCursor($Button1, 0) ; ==> Added.

GUICtrlCreateLabel("New Extensions", 520, 80, 80, 20, 1) ; ==> Added.
$ComboExt = GUICtrlCreateCombo("", 510, 100, 100, 50, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL)) ; ==> Added.
GUICtrlSetData($ComboExt, ".jpg|.bmp|.png|.gif|", ".jpg") ; Choose The New Extensions Post File Move. ==> Added.
GUICtrlSetCursor($ComboExt, 0) ; ==> Added.

GUISetState(@SW_SHOW, $Form1)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit(0)

        Case $Datei
;~          ;$path = FileOpenDialog("choose folder or file's", @ScriptDir, "Alle Dateien (*.*)")
            $path = FileSelectFolder("choose folder or file's", @ScriptDir) ; Correction / Added.
            GUICtrlSetData($Input_old, $path)

        Case $Button1
;~          ;$path_new = FileSelectFolder ( "choose folder or file's", @ScriptDir, "Alle Dateien (*.*)")
            $path_new = FileSelectFolder("choose folder or file's", @ScriptDir, 1) ; Correction / Added.
            GUICtrlSetData($Input_new, $path_new)

        Case $Go
            GUICtrlSetState($Go, $GUI_DISABLE)
            GUICtrlSetState($Datei, $GUI_DISABLE)
            GUICtrlSetState($Button1, $GUI_DISABLE)
            GUICtrlSetData($Go, "wait!.")
            $Files = _FileListToArray($path , "*.*" , 1) ; 1 = Return Files Only. ==> Added.
            If Not @error Then _Copy($Files) ; ==> Added.

    EndSwitch
WEnd

Func _Copy($Files) ; Copies All Files to The New Location With The Extension You Have Chosen.

    Local $ReadExt = GUICtrlRead($ComboExt)

    For $i = 0 To UBound($Files)-1
        FileCopy($path & "\" & $Files[$i], $path_new & "\" & StringRegExpReplace($Files[$i], ".*\\|\.[^.]*", "") & $ReadExt)
    Next

    GUICtrlSetState($Go, $GUI_ENABLE)
    GUICtrlSetState($Datei, $GUI_ENABLE)
    GUICtrlSetState($Button1, $GUI_ENABLE)
    GUICtrlSetData($Go, "End of copy")

EndFunc

 

Edited by Bob1988

Share this post


Link to post
Share on other sites
Bob1988

Hi mikell,

 

Thx for your help, it's working with copy how can i rename the file too, for exampel if the file "test1.db"  to "test2.db". 

For $i = 1 To $Files[0]
   If StringRight($Files[$i], 3) = ".db" Then
      FileCopy($path & "\" & $Files[$i], $path_new & "\") ; how i can rename the file too, 
        Else
        ......

 

Share this post


Link to post
Share on other sites
SlackerAl
Local $path = "D:\AutoIT\Programs\Test\"
Local $Files[1] = ["test1.db"]
FileMove($path & $Files[0], $path & "test2.db")

Obviously, you can process $Files[0] anyway you like with string operations to generate a new name and use that instead of "test2.db"


Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Share this post


Link to post
Share on other sites
Bob1988

thx for ur help, i didn't get it. i get an error. :( 

 

Share this post


Link to post
Share on other sites
SlackerAl

Those 3 lines work, I've just run them on my PC.

What was your error?


Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Share this post


Link to post
Share on other sites
Bob1988

 

I change the code like that, but i get the error   -->   

Local $Files[0] = ["Test1.db"]
Local ^ ERROR

 

Func _Copy($Files) ; 

    Local $ReadExt = GUICtrlRead($ComboExt)
    local $path = "C:\..."; with the right path
    Local $Files[1] = ["Test1.db"]

    ;For $i = 0 To UBound($Files)-1
    For $i = 1 To $Files[0]
   If StringRight($Files[$i], 3) = ".db" then
      FileMove($path & $Files[0], $path & "Test2.db")


        Else

 

Edited by Bob1988
change

Share this post


Link to post
Share on other sites
mikell
Func _Copy($Files)  
    Local $ReadExt = GUICtrlRead($ComboExt)
    For $i = 1 To $Files[0]     ; $Files[0] contains the number of files !
        If StringRight($Files[$i], 3) = ".db" then
             FileCopy($path & "\" & $Files[$i], $path_new & "\")   ; first copy
             FileMove($path_new & "\" & $Files[$i], $path_new & "\" & "Test2.db")  ; then rename
        Else
            ; copy other files
        EndIf
    Next
EndFunc

 

Edited by mikell

Share this post


Link to post
Share on other sites
Bob1988

oh Many thx Mikell it's working. thx alot. 

and thx to slackeral too 

 

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

×