Jump to content

FileMove() problem


Recommended Posts

Hi,

This is my first post so please bear with me as I'm a newb! to the AutoIt language.

I am trying to export the registry and archive the original .reg file if it exists. I also need to add the current date as dd/mmm/yy format to the filename.

The export part of the code works, but the existing file is not moved and renamed. I'm at a loss as to what is wrong?

Any help would be very much appreciated.

Dim $message, $message1, $message2, $message3
Dim $m0, $m1, $m2, $m3
Dim $srcfile, $destfile

$message = "The windows registry will now be backed up." _
         & "This will create a reg file that can be restored if " _
         & "the registry is corrupt."
$message1 = "Backup now in progress"
$message2 = "Registry has been backed up successfully"
$message3 = "Backup was not successfull. Archived file can be found at " & $destfile
$m0 = @MDAY
$m1 = @MON
$m2 = @YEAR
$m3 = @MDAY & "/" & @MON & "/" & @YEAR & ".old"
$srcfile = "D:\Backup\Registry\Backup.reg"
$destfile = "D:\Backup\Registry\Backup\Archive" & $m3

SplashTextOn("Registry export", $message, 350, 55, @DesktopWidth - 895, _
        @DesktopHeight - 890, 0, "Tahoma", 10, 700)
Sleep(4000)
If FileExists($srcfile) Then
    FileMove($srcfile, $destfile)
EndIf
SplashTextOn("Registry export", $message1, 350, 55, @DesktopWidth - 895, _
        @DesktopHeight - 890, 0, "Tahoma", 10, 700)
RunWait("Regedit /e " & $srcfile)
If FileExists($srcfile) Then
    SplashTextOn("Registry export", $message2, 350, 55, @DesktopWidth - 895, _
            @DesktopHeight - 890, 0, "Tahoma", 10, 700)
    Sleep(3000)
Else
    MsgBox(64, "Registry export", $message3)
EndIf
Exit
Edited by tinkythomas
Link to comment
Share on other sites

Right.... I have been digging around in the help files and discovered _NowDate() which gives me the required format dd/mmm/yy. So I ammended my code to as follows:

#include <Date.au3>
Dim $message, $message1, $message2, $message3
Dim $date
Dim $srcfile, $destfile

$message = "The windows registry will now be backed up." _
         & "This will create a reg file that can be restored if " _
         & "the registry is corrupt."
$message1 = "Backup now in progress"
$message2 = "Registry has been backed up successfully"
$message3 = "Backup was not successfull. Archived file can be found at " & $destfile
$date = _NowDate() & ".old"
$srcfile = "D:\Backup\Registry\Backup.reg"
$destfile = "D:\Backup\Registry\Backup\Archive\Backup_" & $date

SplashTextOn("Registry export", $message, 350, 55, @DesktopWidth - 895, _
        @DesktopHeight - 890, 0, "Tahoma", 10, 700)
Sleep(4000)
If FileExists($srcfile) Then
    FileMove($srcfile, $destfile)
EndIf
SplashTextOn("Registry export", $message1, 350, 55, @DesktopWidth - 895, _
        @DesktopHeight - 890, 0, "Tahoma", 10, 700)
RunWait("Regedit /e " & $srcfile)
If FileExists($srcfile) Then
    SplashTextOn("Registry export", $message2, 350, 55, @DesktopWidth - 895, _
            @DesktopHeight - 890, 0, "Tahoma", 10, 700)
    Sleep(3000)
Else
    MsgBox(64, "Registry export", $message3)
EndIf
Exit
but the file is still not moved and renamed. I'm puzzled

Link to comment
Share on other sites

Hi! Works fine for me. Check file path.

#include <Date.au3>

Dim $message, $message1, $message2, $message3
Dim $date
Dim $srcfile, $destfile

$message = "The windows registry will now be backed up." _
         & "This will create a reg file that can be restored if " _
         & "the registry is corrupt."

$message1 = "Backup now in progress"
$message2 = "Registry has been backed up successfully"
$message3 = "Backup was not successfull. Archived file can be found at " & $destfile

$date = _NowDate() & ".old"

$srcfile = "d:\BackUp\Registry\Backup.reg"
$destfile = "D:\BackUp\Registry\Archive\Backup_" & $date
ConsoleWrite($destfile & @LF)

SplashTextOn("Registry export", $message, 350, 55, 100, _
             100, 0, "Tahoma", 10, 700)

Sleep(1000)

If FileExists($srcfile) Then
    FileMove($srcfile, $destfile, 1)
EndIf

SplashTextOn("Registry export", $message1, 350, 55, 100, _
             100, 0, "Tahoma", 10, 700)
RunWait("Regedit /e " & $srcfile)

If FileExists($srcfile) Then
    SplashTextOn("Registry export", $message2, 350, 55, 100, _
            100, 0, "Tahoma", 10, 700)
    Sleep(3000)
Else
    MsgBox(64, "Registry export", $message3)
EndIf
Link to comment
Share on other sites

Hi,

Thanks for the reply rasim. I see you have added ConsoleWrite to output the variable $destfile. I found this useful for checking the filepath.

I did check the filepath and it was correct, so I'm totally stumped why it works fine for you and not me?

Thank-you for your help though.

Link to comment
Share on other sites

I see the condition to check if the source exists but none for the destination. FileMove() may fail if the directory does not exist and the directory is not created.

ConsoleWrite() condition for visual test.

ConsoleWrite('FileExists($destfile) = ' & FileExists($destfile) & @CRLF)

If it shows the value of 0 then FileMove() is failing due to a path that does not exist.

You may want to try using DirCreate() before using FileMove() or use a flag parameter of 9 with FileMove().

:)

Link to comment
Share on other sites

Hi guys,

Sorry for the late reply but have been away for a few days. Right I managed to solve my problem, it was the date that was causing the issue.

You cannot have "/" in the filename which the date function returns dd/mm/yyyy, so I created an array to split the _date()

$Date = _NowDate()
$DateArray = StringSplit($Date, "/")
$FormattedDate = $DateArray[1] & "-" & $DateArray[2] & "-" & $DateArray[3]

So instead of "/" I have "-", works fine now.

Thanks to everyone who helped.

Link to comment
Share on other sites

  • Moderators

Could have done:

$Date = StringReplace(_NowDate(), "/", "-")

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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