Jump to content

Confused by FileMove (Rename)


Recommended Posts

Hello everyone,

I'm trying to make a script that renames a file. I'm using FileMove but I'm really confused.

The name change is on the EXTENSION, not on the file name itself. I need to change it from ".exe_" to "exe".

FileMove is returning a 0 value and the file is clearly not renamed. Here is the relevant code.

For $index = 1 to (UBound($file_array) - 1)
        $new_file_name = StringTrimRight($file_array[$index], 1)
        FileMove($dir & "\" & $file_array[$index], $dir & "\" & $new_file_name)
    Next

 some context notes: 

  • $file_array is the output of _FileListtoArray, and it has the correct values. 
  • $dir is known, and the script is detecting it without problem
  • I've checked that the script is finding the correct files, and that TrimRight is working. All variables have the correct values.
  • I've checked the file attributes, the are set to "A". Are those the correct attributes?
  • I've tried setting the flag of FileMove to 1, 8 and 9 with the same results
  • This was working fine 2 weeks ago when I did the last run,  and I've made no changes. 

(I don't know why the syntax highlighting is messed, quotes are paired correctly) 

 

I know that the help file says that FileMove will not succeed if the destination already exist... my question is, does FileMove count extensions? 

Thank you!  

Link to comment
Share on other sites

FileMove is returning 0 means FAILURE. from the help

Return Value

Success: 1.
Failure: 0 if source cannot be moved or if dest already exists and flag=0.

 

Are you running with elevated privileges (as Admin)?

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

  • Developers

So what does this give for output when you run it?

For $index = 1 To (UBound($file_array) - 1)
    $new_file_name = StringTrimRight($file_array[$index], 1)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $dir & "\" & $file_array[$index] = ' & $dir & "\" & $file_array[$index] & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    $rc=FileMove($dir & "\" & $file_array[$index], $dir & "\" & $new_file_name)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $rc=' & $rc & '  $dir & "\" & $new_file_name = ' & $dir & "\" & $new_file_name & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
Next

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

so i looked at the examples in help and this works fine for me. I modified it for static testing of one file, not generated temp files, but it demonstrates it working on windows 10, latest build and latest AutoIt, but before you do this, read Jos's post and do that first please.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

Example()

Func Example()
    ; Create a constant variable in Local scope of the filepaths that will be renamed.
    Local $sSource = @WorkingDir & "earthshine.exe_";
    Local $sDestination = @WorkingDir & "earthshine.exe"
MsgBox(0, '', $sSource);
MsgBox(0, '', $sDestination);
;~  Local Const $sSource = _WinAPI_GetTempFileName(@TempDir), $sDestination = _WinAPI_GetTempFileName(@TempDir)

    ; Create a temporary file to rename.
    If Not FileWrite($sSource, "This is an example of using FileMove.") Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
        Return False
    EndIf

    ; Rename a file using FileMove and overwrite the new file if it exists.
    FileMove($sSource, $sDestination, $FC_OVERWRITE)

    ; Display results that the destination file was renamed.
    MsgBox($MB_SYSTEMMODAL, "", "Does the source file exist?: " & FileExists($sSource) & @CRLF & _ ; FileExists should return 0.
            "Does destination file exist?: " & FileExists($sDestination) & @CRLF) ; FileExists should return 1.

    ; Delete the temporary files. FileDelete checks if the file exists.
;~  FileDelete($sSource)
;~  FileDelete($sDestination)
EndFunc   ;==>Example

 

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

13 minutes ago, Jos said:

So what does this give for output when you run it?

For $index = 1 To (UBound($file_array) - 1)
    $new_file_name = StringTrimRight($file_array[$index], 1)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $dir & "\" & $file_array[$index] = ' & $dir & "\" & $file_array[$index] & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    $rc=FileMove($dir & "\" & $file_array[$index], $dir & "\" & $new_file_name)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $rc=' & $rc & '  $dir & "\" & $new_file_name = ' & $dir & "\" & $new_file_name & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
Next

Jos

I had to use messageboxes, but here is the result: 

image.png.713a345b63d2e9cc0d1d54fe2aa61b88.png

 

As far as I know, FileMove does not set the @error flag.

 

tw, I forgot... I did set up the FileMove flag to 1 ($FC_OVERWRITE)

Edited by MFrancisca
Link to comment
Share on other sites

10 minutes ago, Earthshine said:

so i looked at the examples in help and this works fine for me

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

Example()

Func Example()
    ; Create a constant variable in Local scope of the filepaths that will be renamed.
    Local $sSource = @WorkingDir & "earthshine.exe_";
    Local $sDestination = @WorkingDir & "earthshine.exe"
MsgBox(0, '', $sSource);
MsgBox(0, '', $sDestination);
;~  Local Const $sSource = _WinAPI_GetTempFileName(@TempDir), $sDestination = _WinAPI_GetTempFileName(@TempDir)

    ; Create a temporary file to rename.
    If Not FileWrite($sSource, "This is an example of using FileMove.") Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
        Return False
    EndIf

    ; Rename a file using FileMove and overwrite the new file if it exists.
    FileMove($sSource, $sDestination, $FC_OVERWRITE)

    ; Display results that the destination file was renamed.
    MsgBox($MB_SYSTEMMODAL, "", "Does the source file exist?: " & FileExists($sSource) & @CRLF & _ ; FileExists should return 0.
            "Does destination file exist?: " & FileExists($sDestination) & @CRLF) ; FileExists should return 1.

    ; Delete the temporary files. FileDelete checks if the file exists.
;~  FileDelete($sSource)
;~  FileDelete($sDestination)
EndFunc   ;==>Example

 

I know, :( That's why I'm so frustrated

Link to comment
Share on other sites

  • Developers

right... I don't mind initiative, but the posted result can't be fromwhat I posted! ;) 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

1 minute ago, Jos said:

right... I don't mind initiative, but the posted result can't be fromwhat I posted! ;) 

I can't use Console Write, because the script is being executed remotely in another computer (using PSExec), so I don't get STOUT. MessageBox is the best way (that I know) to get any output i this case 

Link to comment
Share on other sites

  • Developers
1 minute ago, MFrancisca said:

so I don't get STOUT.

That should be possible with PSEXEXC, but you might have to compile the program as Console. :)

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Just now, Jos said:

Have you checked the file attribs and security for the source file? 

Jos 

I did (see original post), the file attribute string is "A" , and security is minimum, all users can do any action on the file

Link to comment
Share on other sites

  • Developers

I don't understand as I thought you were in control of the autoit3 script. Are you running it from source or compiling it first?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

1 minute ago, Jos said:

I don't understand as I thought you were in control of the autoit3 script. Are you running it from source or compiling it first?

Compiling first... then PSExec takes it and executes it on the other machine. The scripts are mine... all the rest not

Edited by MFrancisca
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

×
×
  • Create New...