Jump to content

Rename file array?


Recommended Posts

Hi all, long time lurker, first time poster! I've been using AutoIT on and off trying to learn bits hear and there over the past couple of years and found it very useful. I dont have a scripting background at all but I try my best.

Anyhow, I'm trying to rename files in a folder and I've go this so far:

 

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
Global $aFileList


BuildArray()
Global $path = "c:\Users\rjohns02\Desktop\temp\"




Func BuildArray()
    ; List all the files and folders in the desktop directory using the default parameters.
     $aFileList = _FileListToArray($path, "*.exe - Shortcut.lnk", 1)
    If @error = 1 Then
        MsgBox($MB_SYSTEMMODAL, "", "Path was invalid.")
        Exit
    EndIf
    If @error = 4 Then
        MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.")
        Exit
    EndIf
    ; Display the results returned by _FileListToArray.
    ;_ArrayDisplay($aFileList, "$aFileList")


For $i = 1 to UBound($aFileList) -1


         ;Rename files
         $newname = StringTrimRight($aFileList[$i], 19)
        $newname = $newname & ".lnk"
         ;
    FileMove($aFileList[$i], $newname, 1)

    MsgBox($MB_SYSTEMMODAL, "Title", $aFileList[$i] & " | " & $newname, 10)

    Next




EndFunc

The messagebox at the end displays the old file name and the new file name but the FileMove function doesnt do anything?! Any advice much appreciated. 

Link to comment
Share on other sites

Hi @mrjonz1, and welcome to the AutoIt forums :welcome:

14 minutes ago, mrjonz1 said:

$newname = StringTrimRight($aFileList[$i], 19)

Trimming a string like this one:

c:\Users\rjohns02\Desktop\temp\somefile.exe

with the statement above, would return something like this:

c:\Users\rjohns02\Deskto

Could you please replace the MsgBox with a ConsoleWrite(), so you can paste the Console output here? :)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Hi FrancescoDimuro thanks for the welcome!

The console output actually works the way i would expect and want it to, i.e. old name | new name:

3D Coat(DX).exe - Shortcut.lnk | 3D Coat(DX).lnk
3D Coat.exe - Shortcut.lnk | 3D Coat.lnk

Basically the files are all .lnk files so i am trimming off the 19 characters ( the ".exe - Shortcut.lnk" part) and then adding the .lnk extension back on the end. I just cant get the actual files to rename....

 

Thanks

Link to comment
Share on other sites

Use 0 in your FileListToArray so it returns the full path to the file as the FileMove needs the path, not just the filename.

$aFileList = _FileListToArray($path, "*.exe - Shortcut.lnk", 0)

hmm... looking further it appears you should just need to put $path & before the From and To in the filemove

Edited by BigDaddyO
Link to comment
Share on other sites

15 hours ago, FrancescoDiMuro said:

0 = False, so it need to set 1 as the $bReturnPath parameter :)

Thanks guys, the "1" was already set however.

I had also previously tried putting the $path in but I put it back in as 

FileMove($path & $aFileList[$i], $path & $newname, 1)

Anyway found the issue, I noticed in the console it saying undeclared variable:

Global $aFileList
BuildArray()
Global $path = "c:\Users\rjohns02\Desktop\temp\"

^ As above, i've declared my variable(s) then called the array function and then tried to declare my $path variable. I just put $path before BuildArray and its all working :P I feel so silly. Thanks for your help guys, I did find both your replies helpful in troubleshooting!

 

Rob

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