robocopy batch um for schleife für Pfade erweitern

user324543

Lt. Junior Grade
Registriert
Jan. 2020
Beiträge
264
Hallo,
ich habe vor geraumer Zeit ein Skript für Robocopy geschrieben, welches mehrere Quellverzeichnisse an mehrere Zielverzeichnisse synchronisieren soll. Da ich mittlerweile mit vielen Verzeichnissen arbeite, ist der Schreibaufwand doch etwas größer, sodass ich dies optimieren möchte.

Aktueller Stand:
Code:
@echo off & setlocal
::backup script

::Unicode
CHCP 1252

SET "source1=D:\source\test1"
SET "source2=D:\source\test2"

SET "destination1=D:\destination\test1"
SET "destination2=D:\destination\test2"

SET robocopy_exclude_directories="$RECYCLE.BIN" "System Volume Information" "Recovery"
SET robocopy_options=/MIR /FFT /R:3 /W:10 /NP /XF *.tmp /XD %robocopy_exclude_directories%
SET log_options=/NDL /TEE

set "date1=%DATE:~6,4%.%DATE:~3,2%.%DATE:~0,2%"
set "time1=%TIME:~0,2%.%TIME:~3,2%.%TIME:~6,2%"
set "date1=%date1: =0%"
set "time1=%time1: =0%"

SET "backup_destination_log=D:\destination\log"
SET "backup_log_name=\%date1%_%time1%_backup_log.txt"
SET "backup_log_command=/LOG+:%backup_destination_log%%backup_log_name%"

if not exist %backup_destination_log% mkdir %backup_destination_log%
if not exist %destination1% mkdir %destination1%
if not exist %destination2% mkdir %destination2%

ROBOCOPY %source1% %destination1% %backup_log_command% %log_options% %robocopy_options%
ROBOCOPY %source2% %destination2% %backup_log_command% %log_options% %robocopy_options%

attrib -s -h /s /d %destination1%
attrib -s -h /s /d %destination2%
exit

Die vielen Zeilen für source & destination nehmen logischerweise immer mehr zu, daher soll das Ganze über 2 String arrays (o.ä.) für die Pfadangaben laufen, damit man nur noch
Code:
list_source = ...
list_destination = ...
editieren muss, ohne das gesamte Skript jedesmal anfassen zu müssen.

Danke schonmal vorab :)
 
Schon mal darüber nachgedacht mit der PowerShell zu arbeiten?
 
  • Gefällt mir
Reaktionen: NotNerdNotDau
tollertyp schrieb:
Schon mal darüber nachgedacht mit der PowerShell zu arbeiten?
ja, allerdings waren mir die Befehle in Powershell damals zu konfus. Würde ungern bei 0 anfangen. Soll auch keinen Schönheitswettbewerb gewinnen, sondern funktionieren und möglichst geringen Änderungsaufwand bei den Pfaden bedeuten
 
Wie wärs denn hiermit:
PowerShell:
# liste mit Quell- und Zielverzeichnissen:
$folders = @{   'D:\Source\Test1' = 'D:\Destination\Test1'
                'D:\Source\Test2' = 'D:\Destination\Test2'
                'D:\Source\Test3' = 'D:\Destination\Test3'
                # usw ...
}
# robocopy parameter
$robocopy_exclude_directories = '"$RECYCLE.BIN" "System Volume Information" "Recovery"'
$robocopy_options             = "/MIR /FFT /R:3 /W:10 /NP /XF *.tmp /XD $robocopy_exclude_directories"
$robocopy_log_options         = "/NDL /TEE"
# timestamp
$timestamp                    = Get-Date -Format "yyyy.MM.dd_hh.mm.ss"
# log parameter
$backup_log_path              = "D:\destination\log"
$backup_destination_log       = Join-Path $backup_log_path "$timestamp.txt"
$backup_log_command           = "/LOG+:" + '"' + $backup_destination_log + '"'
if (-not (Test-Path $backup_log_path )) { New-Item -Path $backup_log_path -ItemType Directory | out-null } # out-null um Ausgabe zu unterdrücken
foreach ($source in $folders.Keys) {
        $destination = $folders[$source]
        # bin mir eigentlich sicher das Robocopy die Zielverzeichnisse selber anlegt, aber ok:
        if (-not (Test-Path $destination )) { New-Item -Path $destination  -ItemType Directory | out-null }
        ROBOCOPY $source $destination $backup_log_command.Split(' ')  $robocopy_log_options.Split(' ')  $robocopy_options.Split(' ') # das mit dem Split('') ist so eine Robocopy Eigenart bei Powershell
        ATTRIB -s -h /s /d $destination
}
Ergänzung ()

PS: bei Schleifen finde ich es IMMER einfacher mit Powershell zu arbeiten als mit Batches...
 
  • Gefällt mir
Reaktionen: NotNerdNotDau, tollertyp, kamanu und 4 andere
user324543 schrieb:
sondern funktionieren und möglichst geringen Änderungsaufwand bei den Pfaden bedeuten
Also du lieferst die Argumente für die PowerShell ja selbst, sehe ich.
 
  • Gefällt mir
Reaktionen: NotNerdNotDau und PHuV
PowerCopy?

Etwas altbacken, funktioniert aber immer noch tadellos:

https://github.com/KurtDeGreeff/PlayPowershell/blob/master/PowerCopy.ps1

Hier noch ein Bild von dem Teil:
PowerCopy.png
 
Zurück
Oben