文件同步

 1 #分别定义源、目标文件夹,注意大小写敏感
 2 $folder_a_path = "D:a"
 3 $folder_b_path = "D:"
 4 #遍历源文件夹下所有文件
 5 $folders_a = gci $folder_a_path -Recurse
 6 foreach ($folder_a in $folders_a)
 7     {
 8     #通过替换的方式,取目标文件的全路径名称
 9     $b = $folder_a.fullname.replace($folder_a_path,$folder_b_path) 
10     #判断目标文件是否存在,如果存在则先判断新旧
11     If (test-path $b)
12        {
13          #判断目标是否为目录,如果是目录则跳过,如果不跳过,则会创建一级空目录
14          If (!((gi $b).PSIsContainer))
15             {
16               #判断目标文件、源文件的新旧情况,如果目标已存在文件的修改时间早于源文件,则重新拷贝覆盖
17               If ((gci $b).lastwritetime -lt $folder_a.lastwritetime)
18                 {
19                  copy-item $folder_a.fullname $b -force
20                 }
21             }
22         
23        }
24     #如果目标文件不存在,则直接拷贝
25     Else
26         {
27          copy-item $folder_a.fullname $b
28         }
29         
30         }       
原文地址:https://www.cnblogs.com/dreamer-fish/p/4185216.html