Jump to content
MFrancisca

Confused by FileMove (Rename)

Recommended Posts

MFrancisca

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!  

Share this post


Link to post
Share on other sites
Earthshine

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

 

Share this post


Link to post
Share on other sites
MFrancisca

I know that 0 is failure, I've been reading the help file :)

I should not need it?, the file is in a subfolder of the Desktop ("Desktop\input\")

Share this post


Link to post
Share on other sites
Jos
Posted (edited)

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 Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
MFrancisca
16 minutes ago, Earthshine said:

Are you running with elevated privileges (as Admin)?

Just to cover all bases I added #requireadmin to the top of the script, recompiled and tried again. Same results :(

Share this post


Link to post
Share on other sites
Earthshine
Posted (edited)

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

 

Share this post


Link to post
Share on other sites
MFrancisca
Posted (edited)
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

Share this post


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

Share this post


Link to post
Share on other sites
Earthshine

from your output, make darn sure that there is no period at the end of your old and newfile please


My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
MFrancisca
3 minutes ago, Earthshine said:

from your output, make darn sure that there is no period at the end of your old and newfile please

No, those are from message box editing, proper grammar and all 

  • Like 1

Share this post


Link to post
Share on other sites
Earthshine
Posted (edited)

he did say he used MsgBox so... :P

Are the files really there?

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Share this post


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

Share this post


Link to post
Share on other sites
MFrancisca
1 minute ago, Earthshine said:

he did say he used MsgBox so... :P

Are the files really there?

yes, the file is there, is just not being renamed 

Share this post


Link to post
Share on other sites
Jos
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 Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


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

Share this post


Link to post
Share on other sites
MFrancisca
1 minute ago, Jos said:

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

Jos

Sadly I don't have control over that part of the system 

Share this post


Link to post
Share on other sites
MFrancisca
Posted (edited)
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

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

  • Similar Content

    • 232showtime
      By 232showtime
      hi im stuck with this I want to transfer the correct file in the existing folder
      #include <File.au3> #include <AutoItConstants.au3> #include <Array.au3> $Read = "C:\New folder (3)" $FLFiles1 = _FileListToArrayRec($Read, "*.xlsx", $FLTAR_FILES, $FLTAR_NORECUR, $FLTAR_SORT, $FLTAR_NOPATH) _ArrayDisplay($FLFiles1) For $i = 0 To UBound($FLFiles1) - 1 $STR = StringTrimRight($FLFiles1[$i], 5) ConsoleWrite($STR & @CRLF) $dirS = DirGetSize($Read & "\" & $STR) If $dirS = -1 Then MsgBox(16, $STR, $dirS) Else FileMove($Read, $Read & "\" & $STR & "\" & $STR & ".xlsx") EndIf next I have 3 folders under "C:\New folder (3)" and I have 5 excel files:
      folders:
      C:\New folder (3)\1
      C:\New folder (3)\3
      C:\New folder (3)\5
      excel files:
      1.xlsx (value of column A are all 1)
      2.xlsx (value of column A are all 2)
      3.xlsx (value of column A are all 3)
      4.xlsx (value of column A are all 4)
      5.xlsx (value of column A are all 5)
       
       I run the above script and those excel file 1, 3, 5 was transfered in the 1, 3, and 5 folders but the value of transfered file in Column A are  different and file no. 4 and 5 still remains in the folder "C:\New folder (3)".... ???
      whats wrong with the script???
       
       
    • copyleft
      By copyleft
      I am trying to create a script to clean up users' desktops by moving all desktop folders and files (except the two hidden "desktop.ini" files and a MyDesktop.lnk shortcut) to a different folder. The script below will move files but not folders. The other issue with the script is that it doesn't seem to execute from a location other than the user's desktop. I would appreciate any suggestions.
      #include <File.au3> MsgBox(64, "Desktop", "Cleaning up Desktop. This box will close in 4 seconds.", 4) $Files = _FileListToArray(@DesktopDir,"*",1) For $Index = 1 To $Files[0] If StringRight($Files[$Index],4) <> ".ini, MyDesktop.lnk" Then FileMove($Files[$Index],'F:\HOME\Desktop') EndIf Next  
    • XinYoung
      By XinYoung
      Hello again,
      I'm trying to use the FileMove function to rename a bunch of text files.
      Some of the files have "_1" at the end of their name, for example, "File123_1.txt". I want to remove the "_1" so it would become "File123.txt".
      What I currently have...
              FileMove($sSource & "\*_1.txt", $sDestination & "\*.txt")
      But since I'm using a wildcard, it doesn't appear to be working. I think it's just replacing .txt with .txt. Am I going about this the wrong way? How can I use wildcards and still accomplish this?
      Any help is greatly appreciated ^__^
    • climmax
      By climmax
      Hi all,
      As a newby i need some help.
      I am trying to add time stamps to file names.
      But it has to do it only once. So i made a check on lenght of filename.
      If filename extention and Lenght  is correct it renames the file with a time stamp.
      I use a ini file for settings. and extentions what needs to be renamed.
      The script works but only once.
      it renames all the  files that are in the ini . like test.txt to text050102.txt
      But when there is 2 files. Like test.txt and  text050102.txt
      It does not rename the test.txt file
      where am i doing this wrong?
       
       
      #include <Array.au3> #include <File.au3> #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> $path = IniRead("RenameFiles.ini","General","InputFolder","Error") $TimeOption = IniRead("RenameFiles.ini","General","TimeOption","Error") Local $aArray = iniReadSection("RenameFiles.ini","EXT") For $i = 1 To $aArray[0][0] $search01 = FileFindFirstFile($path&"*"&$aArray[$i][0]) If $aArray="" Then MsgBox(0,"Error","Check RenameFiles.ini","","") Exit EndIf While 1 $File = FileFindNextFile($search01) $StringLenght= StringLen ($File) if $StringLenght= $aArray[$i][1] Then $FileSplit= StringSplit($File,".") $Time=FileGetTime ( $File ,$TimeOption ,0 ) If @error Then ExitLoop FileMove($path & $File, $path & $FileSplit[1] & $Time[3] & $Time[4] & $Time[5] & $aArray[$i][0], 0) Else ExitLoop EndIf WEnd FileClose($search01) next Ini file looks like:
      [General] ;Enter folder where the files are located: ;Example C:\test\ InputFolder=C:\FileRename\ ;Enter the option you want to use, for timestamp ;0 = Last modified (default) ;1 = Created ;2 = Last accessed TimeOption=0 ;Enter Files extensions that needs a timestamp. ;FileExtention=FileNameLenght (characters) ;Example .TRX.MAU (Dont forget the leading point) ;FileNameLenght = how many characters the filename contains. Including "." ;If filename is longer or shorter it wil not rename the file. ;Example TR150730.TRX.MAU = 16 characters. [EXT] .txt=8 .log=8  
    • megaservo
      By megaservo
      Hi All, 
      I'm making a stage 1 preparation script to apply to target worksation Win 7 and Win 8.1 machines -in preparation to running a separate batch script later (stage 2) within our Windows Server with "netdom.exe move" xxxxxxxx
      Stage 1 Script Tasks: 
      1. delete some common shortcuts
      2. Then move all files within common user profile backup locations - including any files in the sub-directories of the common backups locations within each users profile folder. 
      Stage 1 Script Purpose: 
      Make it easy for staff to grab files and folders from one C:\_Backup directory
      Stage 1 Script PROBLEM:
      The script successfully removes the desired shortcuts - that part works well.
      Problem is it only copies files in root directory of each users common folder (eg: Desktop) but doesn't move all the sub-directories and its files and move it to the corresponding backup directory. I've searched the whole AutoIT forum domain for ideas but I've hit a dead end. 
       
      Here is the code: 
      ;START TASKS-------------------------------------------------------------------------- #include <FileConstants.au3> $ccsystem32 = @SystemDir ;define C:\Windows\System32 folder $ccdesktop = @DesktopCommonDir ;define all users desktop location ;rename CompanyName Remote Desktop shortcut FileCopy($ccdesktop&"\Remote Desktop.rdp", $ccdesktop&"\Company Remote Desktop.rdp", 0) ;delete copies of "SHIP", "eKeySilent", "eKey Manual Login", "eKey Admin" and "ConnX Login" from every profile, adds a copy ot each Desktop folder. $folder = StringLeft(@UserProfileDir,StringInStr(@UserProfileDir,"\",0,-1)) $search = FileFindFirstFile($folder&"*") While 1 $profile = FileFindNextFile($search) If @error Then ExitLoop If FileExists($folder&$profile&"\desktop\SHIP.url") Then FileDelete($folder&$profile&"\desktop\SHIP.url") EndIf If FileExists($folder&$profile&"\desktop\ekeySilent.lnk") Then FileDelete($folder&$profile&"\desktop\ekeySilent.lnk") EndIf If FileExists($folder&$profile&"\desktop\eKey Manual Login.url") Then FileDelete($folder&$profile&"\desktop\eKey Manual Login.url") EndIf If FileExists($folder&$profile&"\desktop\ConnX Login.lnk") Then FileDelete($folder&$profile&"\desktop\ConnX Login.lnk") EndIf If FileExists($folder&$profile&"\desktop\eKey Admin.url") Then FileDelete($folder&$profile&"\desktop\eKey Admin.url") EndIf If FileExists($folder&$profile&"\desktop\Remote Desktop.rdp") Then FileDelete($folder&$profile&"\desktop\Remote Desktop.rdp") EndIf ;backup desktop folder of each user If FileExists($folder&$profile&"\desktop\"&"*.*") Then FileMove($folder&$profile&"\desktop\"&"*.*", "C:\_Backup\"&$profile&"\Desktop\", 9) ;flag combining overwrite existing files & create destination structure EndIf ;backup downloads folder of each user If FileExists($folder&$profile&"\downloads\"&"*.*") Then FileMove($folder&$profile&"\downloads\"&"*.*", "C:\_Backup\"&$profile&"\Downloads\", 9) ;flag combining overwrite existing files & create destination structure EndIf ;backup Documents folder of each user If FileExists($folder&$profile&"\documents\"&"*.*") Then FileMove($folder&$profile&"\documents\"&"*.*", "C:\_Backup\"&$profile&"\Documents\", 9) ;flag combining overwrite existing files & create destination structure EndIf ;backup pictures folder of each user If FileExists($folder&$profile&"\pictures\"&"*.*") Then FileMove($folder&$profile&"\pictures\"&"*.*", "C:\_Backup\"&$profile&"\Pictures\", 9) ;flag combining overwrite existing files & create destination structure EndIf ;backup music folder of each user If FileExists($folder&$profile&"\music\"&"*.*") Then FileMove($folder&$profile&"\music\"&"*.*", "C:\_Backup\"&$profile&"\Music\", 9) ;flag combining overwrite existing files & create destination structure EndIf ;backup videos folder of each user If FileExists($folder&$profile&"\videos\"&"*.*") Then FileMove($folder&$profile&"\videos\"&"*.*", "C:\_Backup\"&$profile&"\Videos\", 9) ;flag combining overwrite existing files & create destination structure EndIf ;backup IEfavorites folder of each user If FileExists($folder&$profile&"\favorites\"&"*.*") Then FileMove($folder&$profile&"\favorites\"&"*.*", "C:\_Backup\"&$profile&"\IEFavorites\", 9) ;flag combining overwrite existing files & create destination structure EndIf WEnd FileClose($search) ;end of deletion of old shortcuts and copying of user files in backup directories. I tried DirMove but I can confirm that windows wont let me move the whole eg: Desktop folder as its read only and I obviously dont want to screw up that account as well (in case we need to log into it) - so I don't want to for example delete the actual Desktop directory. Just move  
      I dont know how to move only all the sub-directories of all the above folders eg: Desktop - and not move the directory eg: Desktop itself. is it 2 problems?
      sub-directories are read only? DirMove is not allowed to copy any sub-directories?  Reason why I was asked to use FileMove and DirMove is that we are concerned of running out of C drive space if we use FileCopy and DirCopy (works on eg: Desktop dir)
      How file structure of C:\_Backup should look like on each target workstation system:
      but it should not move the for eg: C:\Users\Admin\Desktop but should move all of its files and folders. Why move not copy? C drive space is limited on each workstation, so I don't want to create double the files.  Example of Desired File structure of backup directory: C:\_Backup\ C:\_Backup\Admin\ .\Desktop\* (includes all files & subdirectories) .\Documents\* (includes all files & subdirectories) .\Downloads\* (includes all files & subdirectories) .\IEFavorites\* (includes all files & subdirectories) .\Music\* (includes all files & subdirectories) .\Pictures\* (includes all files & subdirectories) .\Vidoes\* (includes all files & subdirectories) C:\_Backup\Administrator\ .\Desktop\* (includes all files & subdirectories) .\Documents\* (includes all files & subdirectories) .\Downloads\* (includes all files & subdirectories) .\IEFavorites\* (includes all files & subdirectories) .\Music\* (includes all files & subdirectories) .\Pictures\* (includes all files & subdirectories) .\Vidoes\* (includes all files & subdirectories) C:\_Backup\Administrator.DomainName\ .\Desktop\* (includes all files & subdirectories) .\Documents\* (includes all files & subdirectories) .\Downloads\* (includes all files & subdirectories) .\IEFavorites\* (includes all files & subdirectories) .\Music\* (includes all files & subdirectories) .\Pictures\* (includes all files & subdirectories) .\Vidoes\* (includes all files & subdirectories) C:\_Backup\Default\ .\Desktop\* (includes all files & subdirectories) .\Documents\* (includes all files & subdirectories) .\Downloads\* (includes all files & subdirectories) .\IEFavorites\* (includes all files & subdirectories) .\Music\* (includes all files & subdirectories) .\Pictures\* (includes all files & subdirectories) .\Vidoes\* (includes all files & subdirectories) C:\_Backup\Default User\ .\Desktop\* (includes all files & subdirectories) .\Documents\* (includes all files & subdirectories) .\Downloads\* (includes all files & subdirectories) .\IEFavorites\* (includes all files & subdirectories) .\Music\* (includes all files & subdirectories) .\Pictures\* (includes all files & subdirectories) .\Vidoes\* (includes all files & subdirectories) C:\_Backup\User1 .\Desktop\* (includes all files & subdirectories) .\Documents\* (includes all files & subdirectories) .\Downloads\* (includes all files & subdirectories) .\IEFavorites\* (includes all files & subdirectories) .\Music\* (includes all files & subdirectories) .\Pictures\* (includes all files & subdirectories) .\Vidoes\* (includes all files & subdirectories) C:\_Backup\Public\ .\Desktop\* (includes all files & subdirectories) .\Documents\* (includes all files & subdirectories) .\Downloads\* (includes all files & subdirectories) .\IEFavorites\* (includes all files & subdirectories) .\Music\* (includes all files & subdirectories) .\Pictures\* (includes all files & subdirectories) .\Vidoes\* (includes all files & subdirectories) Anybody has any ideas or assistance? 
      Might be a really simple thing that I missed. 
      Something missing to the while loop? Maybe the whole while loop needs to be redesigned? ???
       
×