Sign in to follow this  
Followers 0
YonZ

Create "Robocopy" Script

13 posts in this topic

Hello

I'm just wondering if anyone have a sample on how to create a script who will do a filecopy (mirroring) from one fileshare to a local disk, it has to be a true mirroring, so old files will be deleted on the local disk...

Just as i would do with "Robocopy /MIR" or is it possible to make a "wrapper" around robocopy to do this?

This script will then be scheduled to run hourly or more often.... with some error checks (if the PC is not on the network it will not compline, just try again later...

Thanx

YonZ

Share this post


Link to post
Share on other sites



Hello

I'm just wondering if anyone have a sample on how to create a script who will do a filecopy (mirroring) from one fileshare to a local disk, it has to be a true mirroring, so old files will be deleted on the local disk...

Just as i would do with "Robocopy /MIR" or is it possible to make a "wrapper" around robocopy to do this?

This script will then be scheduled to run hourly or more often.... with some error checks (if the PC is not on the network it will not compline, just try again later...

Thanx

YonZ

http://www.autoitscript.com/forum/index.ph...ode=adv&f=2


[quote name='AceLoc']I gots new sunglasses there cool.[/quote]

Share this post


Link to post
Share on other sites

:P I don't want any GUI, max simple icon in the Taskbar, showing status... running or waiting or no connection to fileserver....

Do you want a side order of fries to go with that also?

Share this post


Link to post
Share on other sites

Do you want a side order of fries to go with that also?

:P MHz woke up with Jokes Today!! :nuke:

[center]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.[/center]

Share this post


Link to post
Share on other sites

:P MHz woke up with Jokes Today!! :nuke:

Slow down guy's, I'm only trying to do my work.... as I'm not a Scripting Guru, I need to ask!

I thought gus like you liked to show us how clever you are?

I have created a script for this, but it does not look nice, I did want to use varibales to make the runwait part look better, as I do in my Batch scripts... But I can find out how to do it correctly.

Dim $options, $dest, $logg, $source, $test, $what, $val, $prog, $val2

$options='/R:15 /W:53 /TBD /RH:0715-2359 /MOT:39'

$logg='/LOG+:C:\temp\_katastrofe_dok3.log /NFL /NDL'

;~ :: /R:n :: number of Retries

;~ :: /W:n :: Wait time between retrie

;~ :: /RH:hhmm-hhmm :: Run Hours - times when new copies may be started.

;~ :: /MOT:n :: MOnitor source; run again in m minutes Time, if changed.

;~ :: /LOG :: Output log file

;~ :: /NFL :: No file logging

;~ :: /NDL :: No dir logging

$what='/COPYALL /B /SEC /MIR'

;~ :: /COPYALL :: COPY ALL file info

;~ :: /B :: copy files in Backup mode.

;~ :: /SEC :: copy files with SECurity

;~ :: /MIR :: Mirror a directory tree

$source='"\\global-data-server\global\work\KA"'

$dest=' c:\test '

;~ name of the exe file

$prog='C:\Script\robocopy.exe'

;~ I wanted to combining all varibles in to one, but it does not work!

; $runner = & $prog & $source & $dest& $what & $options & $logg

; then I wanted to run it like this... and that does not work too...

;~ RunWait(@ComSpec & $runner, 'c:\script', @SW_HIDE)

; This line does work.

RunWait( 'c:\script\robocopy "\\global-data-server\global\work\KA" c:\test /COPYALL /B /SEC /MIR /R:15 /W:53 /TBD /RH:0715-2359 /MOT:39 /LOG+:C:\temp\_katastrofe_dok2.log /NFL /NDL', 'c:\script', @SW_HIDE)

Share this post


Link to post
Share on other sites

Try clipput($runner), and then paste that line into the command prompt, and see what you get. It looks like you're generating some invalid switches for RoboCopy. Here's a hint: +" "+


[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

This may do your commandline. I am counting that you will not have spaces in your log file.

$prog = 'c:\script\robocopy'
$source = '\\global-data-server\global\work\KA'
$dest = 'c:\test'
$what = '/COPYALL /B /SEC /MIR'
$options = '/R:15 /W:53 /TBD /RH:0715-2359 /MOT:39'
$logg = '/LOG+:C:\temp\_katastrofe_dok2.log /NFL /NDL'

RunWait(@ComSpec & ' /c "' & $prog & '" "' & $source & '" "' & $dest & '" ' & $what & ' ' & $options & ' ' & $logg, 'c:\script', @SW_HIDE)

:P

Edit:

Added & just before $logg as issue mentioned below.

Edited by MHz

Share this post


Link to post
Share on other sites

This may do your commandline. I am counting that you will not have spaces in your log file.

$prog = 'c:\script\robocopy'
$source = '\\global-data-server\global\work\KA'
$dest = 'c:\test'
$what = '/COPYALL /B /SEC /MIR'
$options = '/R:15 /W:53 /TBD /RH:0715-2359 /MOT:39'
$logg = '/LOG+:C:\temp\_katastrofe_dok2.log /NFL /NDL'

RunWait(@ComSpec & ' /c "' & $prog & '" "' & $source & '" "' & $dest & '" ' & $what & ' ' & $options & ' ' $logg, 'c:\script', @SW_HIDE)

:nuke:

I did try your code... but... I got Error... :P

This expression stuff is to complicated for me... I just can't figure out how to do it... Stupid, yes I think so.. :">

+> Starting AutoIt3Wrapper v.1.7.1

>Running:(3.2.0.1):C:\Program Files\AutoIt3\autoit3.exe "C:\script\katastrofe.au3"

C:\script\katastrofe.au3 (34) : ==> Error in expression.:

RunWait(@ComSpec & ' /c "' & $prog & '" "' & $source & '" "' & $dest & '" ' & $what & ' ' & $options & ' ' $logg, 'c:\script', @SW_HIDE)

RunWait(^ ERROR

+>AutoIT3.exe ended.rc:0

>Exit code: 0 Time: 1.317

Share this post


Link to post
Share on other sites

Ah, left out an "&", my mistake, sorry. Corrected above rather then reposting code. Au3Check says it is fine now.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

It always surprise me how usefull it is to Read The F.... Manual... :nuke:

It works now!

Dim $options, $dest, $logg, $source, $test, $what, $val, $prog, $val2, $runner

$options = '/R:15 /W:53 /TBD /RH:0715-2359 /MOT:39'
$logg = '/LOG+:C:\temp\_katastrofe_dok2.log /NFL /NDL'
$what = '/COPYALL /B /SEC /MIR'
$source = 'c:\temp'
$dest = 'c:\test'
$prog = 'c:\script\robocopy'

$runner = $prog & ' "' & $source & '" "' & $dest & '" ' & $what & ' ' & $options & ' ' & $logg

RunWait( $runner, 'c:\script', @SW_HIDE)

Thanx for the help!!

Now I can add all the fancy error checks and stuff... with help from the forum of course.... :P

Edited by YonZ

Share this post


Link to post
Share on other sites

Thanx for the help!!

Now I can add all the fancy error checks and stuff... with help from the forum of course.... :)

And here it is... working with all the fancy stuff....

And what Will this script do? Well It use robocopy to Synchronize data between 2 share, and I will then Schedule this to run every hour. This Script read robocopy settings from the Robo-Sync.ini file, and user can display result after the run is wanted.

I use second Destination, "USB Drive", Where I first run the Synchronization between FileServer end the PC, and then from the Destenation Source (Local Source) to the USB Drive, But I always check if the Drive is up before any run, as this will be running on laptops, who is not always on the network..

Nb. This is my first AutoIT Script, so donæt say at I did not warn you! :P

#include <file.au3>

Global $robocopy, $options, $dest, $logg, $source, $what, $runner, $prog_source, $LogFile, $var, $err, $ini, $usbdest, $usbrunner, $dractive

$ini = @ScriptDir & '\' & 'Robo-Sync.ini'
$LogFile = @TempDir & '\_Robo-Sync-Run.log'


If FileExists(@TempDir & '\_Robo-Sync-Run.log') Then
    FileDelete(@TempDir & '\_Robo-Sync-Run.log')
EndIf

; Check if ini file exist
If FileExists($ini) Then
    $source = IniRead($ini, "SourceDir", "catalog", "NotFound")
    $dest = IniRead($ini, "DestDir", "catalog", "NotFound")
    $options = IniRead($ini, "RopocopyParamters", "options", "NotFound")
    $logg = '/log:' & @TempDir & '\_Robo-Sync-Robo.log ' & IniRead($ini, "RopocopyParamters", "logg", "NotFound")
    $what = IniRead($ini, "RopocopyParamters", "what", "NotFound")
    $active = IniRead($ini, "USB-Drive", "active", "NotFound")
    $usbdest = IniRead($ini, "USB-Drive", "catalog", "NotFound")
    $dractive = IniRead($ini,"DisplayResault","active","NotFound")
Else
    _FileWriteLog($LogFile, $ini & " not found!! ")
    Exit
EndIf

$prog_source = $source & '\_script\robocopy.exe'

$runner = ' "' & $source & '" "' & $dest & '" ' & $what & ' ' & $options & ' ' & $logg
$usbrunner = ' "' & $dest & '" "' & $usbdest & '" ' & $what & ' ' & $options & ' ' & $logg

; Check for Robocopy, if not then copy from $source/_script path
If FileExists(@ScriptDir & '\robocopy.exe') Then
    $robocopy = @ScriptDir & '\robocopy.exe'

Else
    IF FileExists('c:\minint\system32\robocopy.exe') Then
        FileCopy('c:\minint\system32\robocopy.exe', @ScriptDir,1.0.0.2)
    Else
        FileCopy($prog_source, @ScriptDir,1.0.0.2)
    EndIf
    $robocopy = @ScriptDir & '\robocopy.exe'
EndIf

; Robocopy job, runs according to settings in Robo-Sync.ini
Sleep(50)
If FileExists($source) Then
    $var = RunWait($robocopy & $runner, @ScriptDir, @SW_HIDE)
    _RoboErrorLevel()
Else
    $var = 99
    _RoboErrorLevel()
EndIf


; Robocopy job for Copy from lokal dest to USB Drive, change settings in Robo-Sync.ini
Sleep(50)
If $active = 1 Then
    If FileExists($usbdest) Then
        $var = RunWait($robocopy & $usbrunner, @ScriptDir, @SW_HIDE)
        _RoboErrorLevel()
    EndIf
Else
    $var = 98
    _RoboErrorLevel()
EndIf

If $dractive = 0 Then
    $t_LogText = FileRead($LogFile)
    $s_LogText = FileRead(@TempDir&'\_Robo-Sync-Robo.log')
    MsgBox(0,"Robo-Sync Log file: ", $t_LogText & $s_LogText, 25)
EndIf

; Checks the results / errorlevel from Robocopy and write to logfile
Func _RoboErrorLevel()
    Sleep(100)
    If $var = 99 Then
        $err = $source & '  network not avalible at the moment...'
    ElseIf $var = 98 Then
        $err = $usbdest & '  USB Disk not avalible at the moment...'
    ElseIf $var = 16  Then
        $err = '  ***FATAL ERROR*** '
    ElseIf $var = 15  Then
        $err = ' FAIL MISM XTRA COPY'
    ElseIf $var = 14  Then
        $err = ' FAIL MISM XTRA     '
    ElseIf $var = 13  Then
        $err = ' FAIL MISM      COPY'
    ElseIf $var = 12  Then
        $err = ' FAIL MISM          '
    ElseIf $var = 11  Then
        $err = ' FAIL      XTRA COPY'
    ElseIf $var = 10  Then
        $err = ' FAIL      XTRA     '
    ElseIf $var =  9  Then
        $err = ' FAIL           COPY'
    ElseIf $var =  8  Then
        $err = ' FAIL               '
    ElseIf $var =  7  Then
        $err = '      MISM XTRA COPY'
    ElseIf $var =  6  Then
        $err = '      MISM XTRA     '
    ElseIf $var =  5  Then
        $err = '      MISM      COPY'
    ElseIf $var =  4  Then
        $err = '      MISM          '
    ElseIf $var =  3  Then
        $err = '           XTRA COPY'
    ElseIf $var =  2  Then
        $err = '           XTRA     '
    ElseIf $var =  1  Then
        $err = '                COPY'
    ElseIf $var =  0  Then
        $err = '    --no change--   '
    EndIf
    _FileWriteLog($LogFile, "Resault from Robo-Sync: " & $err)
EndFunc

Robo-Sync.ini

[RopocopyParamters]options = /R:15 /W:53 /TBD

logg = /NP /NFL /NDL /NJH

what = /COPYALL /B /SEC /MIR /XD _script

[sourceDir]

catalog = c:\temp

[DestDir]

catalog = c:\test

[uSB-Drive]

active = 1

catalog = "x:\Some Doc path"

[DisplayResault]

active = 1

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