Sign in to follow this  
Followers 0
grunewald6

Check for user profile before backup

6 posts in this topic

Ok, I'm pretty new at this but I've been teaching myself AutoIT by using the help file and reading this forum. I'm making a user profile backup/restore tool to use at work to ease reimaging of our computers. I want my script to check for the user's profile to make sure it was not mistyped before it runs the backup. I can't get it to work quite right. I've made this function to check for the user:

Func CheckUser ()

$username = guictrlread($input)

If not FileExists("C:\documents and settings\" & $username) Then

MsgBox(0, "Error", "Check spelling. That user profile is not on this computer.")

EndIf

EndFunc

This is the main program run:

While 1

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Case $msg = $Backup

Checkuser ()

Backup () ;Will Run Backup Function

Exit

Case $msg = $Restore

Restore () ;Will Run Restore Function

exit

EndSelect

WEnd

What I need is for it to start over or wait for the username to be changed in the input box after it sees that the username is not valid. Can anyone help? Sorry if this isn't enough info to explain my prob. Any help is appreciated.

Share this post


Link to post
Share on other sites



It would help if you gave me your whole code instead of one function and your main loop.

Share this post


Link to post
Share on other sites

Can do.

#include <GUIConstants.au3>
#include <Process.au3>

;Installs resource files
FileInstall ("printmig.exe", @TempDir & "\")

;GUI
$Form1 = GUICreate("Profile Backup and Restore", 280, 149, 193, 125)
$Label = GUICtrlCreateLabel("Enter the Username", 24, 11, 122, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$Input = GUICtrlCreateInput("", 17, 36, 137, 21)
$Label2 = GUICtrlCreateLabel("Include Printers?", 24, 71, 100, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$Printers = GUICtrlCreateCheckbox("", 130, 69, 20, 20)
GuiCtrlSetState(-1, $GUI_CHECKED)
$NBMS = GUICtrlCreateRadio("NBMS", 192, 36, 57, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$NBES = GUICtrlCreateRadio("NBES", 192, 60, 57, 17)
$Amqui = GUICtrlCreateRadio("AMQUI", 192, 84, 57, 17)
$Label2 = GUICtrlCreateLabel("What school?", 176, 11, 85, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$Backup = GUICtrlCreateButton("Backup", 8, 102, 73, 33, 0)
$Restore = GUICtrlCreateButton("Restore", 89, 102, 73, 33, 0)
GUISetState(@SW_SHOW)


;Main program
While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $Backup
                Checkuser ()
                Backup ()   ;Will Run Backup Function
                Exit
            Case $msg = $Restore
                Restore ()  ;Will Run Restore Function
                exit
        EndSelect
WEnd

;Functions
Func Backup ()
    $username = guictrlread($input)
        Select
            Case GUICtrlRead($NBMS) = $GUI_CHECKED
                $server = "\\600npsdtconsole\backup$\"
            Case GUICtrlRead($NBES) = $GUI_CHECKED
                $server = "\\007npsltconsole\backup$\"
            Case GUICtrlRead($Amqui) = $GUI_CHECKED
                $server = "\\007npsltconsole\backup$\"
        EndSelect
        ProgressOn("Backing up System...", "Copying Desktop Files", "Running...", -1, -1, 18); Create a Progress bar.
        WinSetOnTop ("Backing", "", 1)
        DirCopy ("c:/documents and settings/" & $username & "/desktop", $server & $username & "\Desktop", 9)
        ProgressSet(12.5, "", "Copying My Documents"); Advance the Progress Bar.
        DirCopy ("c:/documents and settings/" & $username & "/My Documents", $server & $username & "\My Documents", 9)
        ProgressSet(25, "", "Copying Favorites"); Advance the Progress Bar.
        DirCopy ("c:/documents and settings/" & $username & "/Favorites", $server & $username & "\Favorites", 9)
        ProgressSet(37.7, "", "Copying Examview Banks"); Advance the Progress Bar.
        DirCopy ("c:/program files/examview/banks", $server & $username & "\My Documents\Examview Banks", 9)
        ProgressSet(50, "", "Copying Examview Tests"); Advance the Progress Bar.
        DirCopy ("c:/program files/examview/tests", $server & $username & "\My Documents\Examview Tests", 9)
        ProgressSet(62.5, "", "Copying Outlook Files"); Advance the Progress Bar.
        FileCopy ("C:/Documents and Settings/" & $username & "/Local Settings/Application Data/Microsoft/Outlook/*.pst", $server & $username & "\My Documents\", 9)
        ProgressSet(75, "", "Copying Outlook Files"); Advance the Progress Bar.
        FileCopy ("C:/Documents and Settings/" & $username & "/Application Data/Microsoft/Outlook/*.nk2", $server & $username & "\", 9)
        If GUICtrlRead($Printers) = $GUI_CHECKED Then
            ProgressSet(87.5, "", "Copying Printers"); Advance the Progress Bar.
            _RunDOS ("%temp%\printmig -b " & $server & $username & "\printerbackup.cab")
        EndIf
        ProgressSet(100, "Done", "Backup Complete"); Advance the Progress Bar.
        Sleep(2000); Wait 2 seconds.
        ProgressOff(); Close progress window.
        FileDelete (@TempDir & "\printmig.exe")
EndFunc

Func Restore ()
    $username = guictrlread($input)
    Select
        Case GUICtrlRead($NBMS) = $GUI_CHECKED
            $server = "\\600npsdtconsole\backup$\"
        Case GUICtrlRead($NBES) = $GUI_CHECKED
            $server = "\\007npsltconsole\backup$\"
        Case GUICtrlRead($Amqui) = $GUI_CHECKED
            $server = "\\007npsltconsole\backup$\"
    EndSelect
    ProgressOn("Restoring System...", "Restoring Desktop Files", "Running...", -1, -1, 18); Create a Progress bar.
    WinSetOnTop ("Restoring", "", 1)
    DirCopy ($server & $username & "\Desktop", "c:/documents and settings/" & $username & "/desktop/", 9)
    ProgressSet(20, "", "Restoring My Documents"); Advance the Progress Bar.
    DirCopy ($server & $username & "\My Documents", "c:/documents and settings/" & $username & "/My Documents/", 9)
    ProgressSet(40, "", "Restoring Favorites"); Advance the Progress Bar.
    DirCopy ($server & $username & "\Favorites", "c:/documents and settings/" & $username & "/Favorites/", 9)
    ProgressSet(60, "", "Restoring Outlook Files"); Advance the Progress Bar.
    FileCopy ($server & $username & "\*.nk2", "C:/Documents and Settings/" & $username & "/Application Data/Microsoft/Outlook/", 9)
    If FileExists ($server & $username & "\printerbackup.cab") then
        ProgressSet(80, "", "Restoring Printers"); Advance the Progress Bar.
        _RunDOS ("%temp%\printmig -r " & $server & $username & "\printerbackup.cab")
    EndIf
    ProgressSet(100, "Done", "Restore Complete"); Advance the Progress Bar.
    Sleep(2000); Wait 2 seconds.
    ProgressOff(); Close progress window.
    FileDelete (@TempDir & "\printmig.exe")     
EndFunc

Func CheckUser ()
    $username = guictrlread($input)
    If not FileExists("C:\documents and settings\" & $username) Then 
        MsgBox(0, "Error", "Check spelling.  That user profile is not on this computer.")
    EndIf   
EndFunc

Share this post


Link to post
Share on other sites

This will work, but just a note, this script will not work on Vista because vista does not use C:\Documents and Settings\ it uses, C:\Users\.

#include <GUIConstants.au3>
#include <Process.au3>

;Installs resource files
FileInstall("printmig.exe", @TempDir & "\")

;GUI
$Form1 = GUICreate("Profile Backup and Restore", 280, 149, 193, 125)
$Label = GUICtrlCreateLabel("Enter the Username", 24, 11, 122, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$Input = GUICtrlCreateInput("", 17, 36, 137, 21)
$Label2 = GUICtrlCreateLabel("Include Printers?", 24, 71, 100, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$Printers = GUICtrlCreateCheckbox("", 130, 69, 20, 20)
GUICtrlSetState(-1, $GUI_CHECKED)
$NBMS = GUICtrlCreateRadio("NBMS", 192, 36, 57, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$NBES = GUICtrlCreateRadio("NBES", 192, 60, 57, 17)
$Amqui = GUICtrlCreateRadio("AMQUI", 192, 84, 57, 17)
$Label2 = GUICtrlCreateLabel("What school?", 176, 11, 85, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$Backup = GUICtrlCreateButton("Backup", 8, 102, 73, 33, 0)
$Restore = GUICtrlCreateButton("Restore", 89, 102, 73, 33, 0)
GUISetState(@SW_SHOW)


;Main program
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Backup
            If Not CheckUser() = 0 Then
                Backup()
                Exit
            EndIf
        Case $msg = $Restore
            Restore() ;Will Run Restore Function
            Exit
    EndSelect
WEnd

;Functions
Func Backup()
    $username = GUICtrlRead($Input)
    Select
        Case GUICtrlRead($NBMS) = $GUI_CHECKED
            $server = "\\600npsdtconsole\backup$\"
        Case GUICtrlRead($NBES) = $GUI_CHECKED
            $server = "\\007npsltconsole\backup$\"
        Case GUICtrlRead($Amqui) = $GUI_CHECKED
            $server = "\\007npsltconsole\backup$\"
    EndSelect
    ProgressOn("Backing up System...", "Copying Desktop Files", "Running...", -1, -1, 18); Create a Progress bar.
    WinSetOnTop("Backing", "", 1)
    DirCopy("c:/documents and settings/" & $username & "/desktop", $server & $username & "\Desktop", 9)
    ProgressSet(12.5, "", "Copying My Documents"); Advance the Progress Bar.
    DirCopy("c:/documents and settings/" & $username & "/My Documents", $server & $username & "\My Documents", 9)
    ProgressSet(25, "", "Copying Favorites"); Advance the Progress Bar.
    DirCopy("c:/documents and settings/" & $username & "/Favorites", $server & $username & "\Favorites", 9)
    ProgressSet(37.7, "", "Copying Examview Banks"); Advance the Progress Bar.
    DirCopy("c:/program files/examview/banks", $server & $username & "\My Documents\Examview Banks", 9)
    ProgressSet(50, "", "Copying Examview Tests"); Advance the Progress Bar.
    DirCopy("c:/program files/examview/tests", $server & $username & "\My Documents\Examview Tests", 9)
    ProgressSet(62.5, "", "Copying Outlook Files"); Advance the Progress Bar.
    FileCopy("C:/Documents and Settings/" & $username & "/Local Settings/Application Data/Microsoft/Outlook/*.pst", $server & $username & "\My Documents\", 9)
    ProgressSet(75, "", "Copying Outlook Files"); Advance the Progress Bar.
    FileCopy("C:/Documents and Settings/" & $username & "/Application Data/Microsoft/Outlook/*.nk2", $server & $username & "\", 9)
    If GUICtrlRead($Printers) = $GUI_CHECKED Then
        ProgressSet(87.5, "", "Copying Printers"); Advance the Progress Bar.
        _RunDOS("%temp%\printmig -b " & $server & $username & "\printerbackup.cab")
    EndIf
    ProgressSet(100, "Done", "Backup Complete"); Advance the Progress Bar.
    Sleep(2000); Wait 2 seconds.
    ProgressOff(); Close progress window.
    FileDelete(@TempDir & "\printmig.exe")
EndFunc   ;==>Backup

Func Restore()
    $username = GUICtrlRead($Input)
    Select
        Case GUICtrlRead($NBMS) = $GUI_CHECKED
            $server = "\\600npsdtconsole\backup$\"
        Case GUICtrlRead($NBES) = $GUI_CHECKED
            $server = "\\007npsltconsole\backup$\"
        Case GUICtrlRead($Amqui) = $GUI_CHECKED
            $server = "\\007npsltconsole\backup$\"
    EndSelect
    ProgressOn("Restoring System...", "Restoring Desktop Files", "Running...", -1, -1, 18); Create a Progress bar.
    WinSetOnTop("Restoring", "", 1)
    DirCopy($server & $username & "\Desktop", "c:/documents and settings/" & $username & "/desktop/", 9)
    ProgressSet(20, "", "Restoring My Documents"); Advance the Progress Bar.
    DirCopy($server & $username & "\My Documents", "c:/documents and settings/" & $username & "/My Documents/", 9)
    ProgressSet(40, "", "Restoring Favorites"); Advance the Progress Bar.
    DirCopy($server & $username & "\Favorites", "c:/documents and settings/" & $username & "/Favorites/", 9)
    ProgressSet(60, "", "Restoring Outlook Files"); Advance the Progress Bar.
    FileCopy($server & $username & "\*.nk2", "C:/Documents and Settings/" & $username & "/Application Data/Microsoft/Outlook/", 9)
    If FileExists($server & $username & "\printerbackup.cab") Then
        ProgressSet(80, "", "Restoring Printers"); Advance the Progress Bar.
        _RunDOS("%temp%\printmig -r " & $server & $username & "\printerbackup.cab")
    EndIf
    ProgressSet(100, "Done", "Restore Complete"); Advance the Progress Bar.
    Sleep(2000); Wait 2 seconds.
    ProgressOff(); Close progress window.
    FileDelete(@TempDir & "\printmig.exe")
EndFunc   ;==>Restore

Func CheckUser()
    $username = GUICtrlRead($Input)
    If Not FileExists("C:\documents and settings\" & $username) Then
        MsgBox(0, "Error", "Check spelling.  That user profile is not on this computer.")
        Return 0
    EndIf
    Return 1
EndFunc   ;==>CheckUser

Share this post


Link to post
Share on other sites

Use this to query checkboxes and radios

\Select
    Case _IsChecked($NBMS)
        $server = "\\600npsdtconsole\backup$\"
    Case _IsChecked($NBES)
        $server = "\\007npsltconsole\backup$\"
    Case _IsChecked($Amqui)
        $server = "\\007npsltconsole\backup$\"
EndSelect


Func _IsChecked($control)
    Return BitAND(GUICtrlRead($control), $GUI_CHECKED) = $GUI_CHECKED
EndFunc   ;==>_IsChecked

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

Thanks TehWhale. That worked perfectly. Thanks to both on simplifying and notation tips. Still learning this but it is actually pretty fun. This forum has been indispensible.

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
Sign in to follow this  
Followers 0