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
| Option Explicit
Dim CheminRepertoire, objFSO, objFolder, MonFolder
CheminRepertoire = "E:\Sauvegarde\Test" 'Chemin du répertoire à traiter
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Effacement des fichiers contenus dans un répertoire
objFSO.DeleteFile CheminRepertoire & "\*.*", True
'Effacement des sous-dossiers contenus dans un répertoire
Set objFolder = objFSO.GetFolder(CheminRepertoire)
For Each MonFolder In objFolder.SubFolders
objFSO.DeleteFolder MonFolder, True
Next
Set objFolder = Nothing
Set objFSO = Nothing
'*****************************************************************************************************
'ici ce fait la séparation entre les parties de la Suppression
'*****************************************************************************************************
WScript.Sleep 5000
Dim Source,Destination
Source = "C:\mémoire_systeme"
Destination = "E:\Sauvegarde\Test-"&Day(Date)&"-"& Month(Now) &"-"&Year(Date)
If AppPrevInstance() Then
MsgBox "Il y a une instance déjà en cours !"& VbCrLF & CommandLineLike(WScript.ScriptName),VbExclamation,"Il y a une instance déjà en cours !"
WScript.Quit
Else
Call CopyDirs(Source,Destination)
End If
'*************************************************************************************************
Sub CopyDirs(fromFolder,toFolder)
Dim oFSO,c_folder,c_File,subFolder
Set oFSO = CreateObject("Scripting.FileSystemObject")
If Not(oFSO.FolderExists(fromFolder)) Then
MsgBox "ATTENTION le dossier " & DblQuote(fromFolder) & " n'existe pas !",vbExclamation,_
"ATTENTION le dossier " & DblQuote(fromFolder) & " n'existe pas !"
Wscript.Quit
End if
Set c_folder = oFSO.GetFolder(fromFolder)
On Error Resume Next
If Not(oFSO.FolderExists(toFolder)) Then
oFSO.CreateFolder toFolder
If Err <> 0 Then
MsgBox Err.Description & Vbcr &_
"Le chemin du " & DblQuote(toFolder) &" est introuvable !" ,VbCritical,Err.Description
Wscript.Quit
End if
End If
For Each c_File In c_folder.Files
If Not(oFSO.FileExists(toFolder & "\" & c_File.Name)) Then
oFSO.CopyFile c_folder.Path & "\" & c_File.Name,toFolder & "\" & c_File.Name
End If
Next
For Each subFolder In c_folder.SubFolders
If Not( oFSO.FolderExists(toFolder & "\" & subFolder.Name)) Then
oFSO.CreateFolder toFolder & "\" & subFolder.Name
End If
'Appel récursive de la procèdure CopyDirs()
Call CopyDirs(subFolder.Path,toFolder & "\" & subFolder.Name)
Next
End Sub
'*****************************************************************************************************
'Fonction pour ajouter les doubles quotes dans une variable
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'******************************************************************************************************
Function AppPrevInstance()
With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
" AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")
AppPrevInstance = (.Count > 1)
End With
End With
End Function
'*******************************************************************************************************
Function CommandLineLike(ProcessPath)
ProcessPath = Replace(ProcessPath, "\", "\\")
CommandLineLike = "'%" & ProcessPath & "%'"
End Function
'****************************************************************************************************** |
Partager