This article mainly introduces the implementation of PowerShell file synchronization script sharing, this article directly gives the implementation code, the need for friends can refer to the following
?
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 |
#分别定义源, target folder, note case sensitive $folder _a_path = "D:a" $folder _b_path = "D:b" #遍历源文件夹下所有文件 $folders _a = GCI $folder _a_path-recurse F Oreach ($folder _a in $folders _a) {#通过替换的方式, take the full path name of the destination file $b = $folder _a.fullname.replace ($folder _a_path, $folder _b_path) #判断目标文件是否存在, if present, first determine the new and old if (Test-path $b) {#判断目标是否为目录, if the directory is skipped, if not skipped, will create an empty directory if (!) ( (GI $b). Psiscontainer) {#判断目标文件, the new and old condition of the source file, and if the target already exists before the file was modified earlier than the source file, copy overwrite if ((GCI $b). Lastwritetime-lt $folder _a.lastwritetime {Copy-item $folder _a.fullname $b-force}}} #如果目标文件不存在, direct copy Else {Copy-item $folder _a.fullname $b}} |