【发布时间】:2019-06-18 15:22:11
【问题描述】:
我正在尝试在 PowerShell 中编写代码,将临时目录中的所有“.*sv$”复制到自定义文件夹。
我将使用任务计划程序每 15 分钟运行一次此代码,以避免在您关闭 AutoCAD 而不保存时丢失数据。
这是我写的代码:
Set-ExecutionPolicy unrestricted
$tempfolder = $env:temp
$destinazione = "Z:\Autocad temp\"
$tempfolder
(Get-ChildItem -Path $tempfolder -include "*.sv$" -Recurse).Count
Copy-Item -Path $tempfolder\ -Include "*.sv$" -Destination $destinazione
(Get-ChildItem -Path $destinazione -Recurse).Count
这就是我得到的:
PS C:\Windows\system32> Z:\Autocad temp\salva sv$.ps1 C:\Users\dedil\AppData\Local\Temp 5 1其中 1 个文件是我的 ps1 文件。所以临时目录中的 5 个文件都没有被复制。
【问题讨论】:
-
将
-Recurse添加到Copy-Item -
复制项目 -path $tempfolder\ -include "*.sv$" -Destination $destinazione -Recurse PS C:\Windows\system32> Z:\Autocad temp\salva sv$.ps1 C :\Users\dedil\AppData\Local\Temp 6 1
-
不幸的是,即使使用 -recurse 方法也没有复制任何内容:源目录中的 6 个文件,目标目录中的 1 个文件
-
不要将
-Include仅用于一种文件模式。改用-Filter- 在文件系统上运行,-Include和-Exclude参数由 cmdlet 处理,而不是文件系统,并且速度明显较慢。 [咧嘴] -
哇,感谢您的提示!现在它运行了,但我得到了复制文件夹的事件。我怎样才能避免它们?复制项 -path $tempfolder\ -filter "*.sv$" -Destination $destinazione -Recurse
标签: powershell