Jump to content

rename file extensions


Bob1988
 Share

Recommended Posts

 

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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
Link to comment
Share on other sites

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