Bonjour à tous.
J'ai un script powershell pour du robocopy. Je rencontre un problème sur le décompte des fichiers restants qui passe en négatif alors que le robocopy continue de copier des fichiers. Je ne vois pas du tout où se situe le problème. Si vous avez des pistes, je suis preneur.

Code powershell : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
 
Clear-Host
Write-Host ""
Write-Host -ForegroundColor Red -NoNewline " | ROBOCOPY    "; Write-Host -ForegroundColor White -NoNewline "   BRBIT©"; Write-Host -ForegroundColor Red " |";
Write-Host -ForegroundColor Red -NoNewline " |"; Write-Host -ForegroundColor Green -NoNewline " Robocopy des fichiers"; Write-Host -ForegroundColor Red " |";
Write-Host ""
Sleep 2
 
Clear-Host
Function FolderBrowserDialogShow([string]$description, [System.Environment+SpecialFolder]$rootFolder = [System.Environment+SpecialFolder]::MyComputer) {
    $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
    $foldername.RootFolder = $rootFolder
    $foldername.Description = $description
 
    if ($foldername.ShowDialog() -eq "OK") {
        return $foldername.SelectedPath
    }
    return ""
}
 
# Création de la forme
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
 
$Form = New-Object System.Windows.Forms.Form
$Form.width = 400
$Form.height = 205
$Form.Text = "ROBOCOPY - Gestion des Fichiers"
$Form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$Form.Topmost = $True
 
# Fixer la taille de la fenêtre
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$Form.MaximizeBox = $false
 
# Ajout d'une icône à la fenêtre
$Form.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon("C:\ROBOCOPY_HDD\RobocopyGUI\Robocopy.ico")
 
# Ajout Premier Bouton
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Size = New-Object System.Drawing.Size(100, 40)
$Button1.Text = "Source"
$Button1.BackColor = [System.Drawing.Color]::LightBlue
$Button1.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
 
# Changement de couleur au survol du bouton
$Button1.Add_MouseHover({ $Button1.BackColor = [System.Drawing.Color]::SkyBlue })
$Button1.Add_MouseLeave({ $Button1.BackColor = [System.Drawing.Color]::LightBlue })
$Button1.Add_Click({
    $global:source = FolderBrowserDialogShow -description "Sélectionnez la Source"
})
 
# Ajout Second Bouton
$Button2 = New-Object System.Windows.Forms.Button
$Button2.Size = New-Object System.Drawing.Size(100, 40)
$Button2.Text = "Destination"
$Button2.BackColor = [System.Drawing.Color]::LightYellow
$Button2.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
 
# Changement de couleur au survol du bouton
$Button2.Add_MouseHover({ $Button2.BackColor = [System.Drawing.Color]::Gold })
$Button2.Add_MouseLeave({ $Button2.BackColor = [System.Drawing.Color]::LightYellow })
$Button2.Add_Click({
    $global:destination = FolderBrowserDialogShow -description "Sélectionnez la Destination"
})
 
# Bouton OK pour lancer la copie Robocopy
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Size = New-Object System.Drawing.Size(100, 20)
$OKButton.Text = "Robocopy"
$OKButton.BackColor = [System.Drawing.Color]::LightGreen
$OKButton.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
$OKButton.Add_Click({
    if (-not $global:source -or -not $global:destination) {
        [System.Windows.Forms.MessageBox]::Show("Veuillez sélectionner une source et une destination.", "Erreur", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
    } else {
        $Form.DialogResult = [System.Windows.Forms.DialogResult]::OK
        $Form.Close()
    }
})
 
# Bouton Annuler
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Size = New-Object System.Drawing.Size(100, 20)
$CancelButton.Text = "Annuler"
$CancelButton.BackColor = [System.Drawing.Color]::LightCoral
$CancelButton.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
$CancelButton.Add_Click({
    $Form.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $Form.Close()
})
 
# Création du layout en colonne
$tableLayoutPanel = New-Object System.Windows.Forms.TableLayoutPanel
$tableLayoutPanel.RowCount = 5  # Augmente le nombre de lignes
$tableLayoutPanel.ColumnCount = 1
$tableLayoutPanel.Dock = [System.Windows.Forms.DockStyle]::Fill
 
# Ajustement de la position des boutons
$tableLayoutPanel.Controls.Add($Button1, 0, 1)  # Déplace le bouton Source à la ligne 1
$tableLayoutPanel.Controls.Add($Button2, 0, 2)  # Déplace le bouton Destination à la ligne 2
$tableLayoutPanel.Controls.Add($OKButton, 0, 3)  # Déplace le bouton OK à la ligne 3
$tableLayoutPanel.Controls.Add($CancelButton, 0, 4)  # Déplace le bouton Annuler à la ligne 4
 
$Form.Controls.Add($tableLayoutPanel)
 
# Ajout d'une barre de statut en bas de la fenêtre
$statusBar = New-Object System.Windows.Forms.StatusStrip
$statusLabel = New-Object System.Windows.Forms.ToolStripStatusLabel
$statusLabel.Text = "En attente d'une action"
$statusBar.Items.Add($statusLabel)
$Form.Controls.Add($statusBar)
 
# Configuration des boutons ajustables en fonction de la taille de la fenêtre
$Button1.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Right
$Button2.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Right
$OKButton.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Right
$CancelButton.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Right
 
$Form.Add_Shown({$Form.Activate()})
 
$result = $Form.ShowDialog()
 
if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
    Write-Host "Source : $global:source" -ForegroundColor Red
    Write-Host "Destination : $global:destination" -ForegroundColor Green
 
    # Calcul des valeurs pour le suivi de progression
    $sourceFiles = Get-ChildItem -Path $global:source -Recurse -File
    $count = $sourceFiles.Count
    $fLeft = $count
    $number = 0
    $errorCount = 0
 
    # Fichier de log
    $logFile = "C:\ROBOCOPY_HDD\RobocopyGUI\log.txt"  # Remplace ce chemin par le chemin où tu veux enregistrer le log
    $logEntries = @()
 
    # Création de l'en-tête du fichier de log
    $logEntries += "========================="
    $logEntries += "   Rapport de Robocopy"
    $logEntries += "========================="
    $logEntries += "Date: $(Get-Date)"
    $logEntries += "Source: $global:source"
    $logEntries += "Destination: $global:destination"
    $logEntries += "-------------------------"
    $logEntries += "Fichiers copiés: $number"
    $logEntries += "Erreurs rencontrées: $errorCount"
    $logEntries += "-------------------------"
 
    # Fonction Robocopy
    Robocopy $global:source $global:destination /E /IS /NJH /NJS /NDL /NC /BYTES | ForEach-Object {
        $Script:data = $_ -split '\x09'
 
        # Comptage des fichiers copiés et des erreurs
        if (![String]::IsNullOrEmpty("$($data[4])")) {
            $Script:file = $data[4] -replace '.+\\(?=(?:.(?!\\))+$)'
            $fLeft--
            $number++
        }
 
        # Comptage des erreurs
        if (![String]::IsNullOrEmpty("$($data[0])") -and $data[0] -like "*ERROR*") {
            $errorCount++
        }
 
        # Progression
        if (![String]::IsNullOrEmpty("$($data[0])")) {
            $Script:percent = ($data[0] -replace '%') -replace '\s'
        } else {
            $Script:percent = 0  # Valeur par défaut si null ou vide
        }
        Write-Progress -Activity "Copie en cours:  ..\$file" -CurrentOperation "Total des fichiers: $count     Fichier(s) copié(s): $number     Fichier(s) restant(s): $fLeft" -PercentComplete $percent
    }
 
    # Ajout des résultats au fichier de log
    $logEntries += "-------------------------"
    $logEntries += "Détails de la copie"
    $logEntries += "Fichiers source: $count"
    $logEntries += "Fichiers copiés: $number"
    $logEntries += "Erreurs rencontrées: $errorCount"
    $logEntries += "-------------------------"
 
    # Écriture du log dans le fichier
    $logEntries | Out-File -FilePath $logFile -Encoding utf8
 
    Write-Progress -Activity " " -Completed
    Clear-Host
    Write-Host ""
    Write-Host -ForegroundColor Red -NoNewline " | ROBOCOPY    "; Write-Host -ForegroundColor White -NoNewline "         BRBIT©"; Write-Host -ForegroundColor Red " |";
    Write-Host -ForegroundColor Red -NoNewline " |"; Write-Host -ForegroundColor Green -NoNewline " Copie des fichiers terminée"; Write-Host -ForegroundColor Red " |";
    Write-Host ""
    Sleep 3	
}
Sleep 3