【问题标题】:PowerShell code to copy files from %temp% folder从 %temp% 文件夹复制文件的 PowerShell 代码
【发布时间】: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


【解决方案1】:

Get-ChildItem 传递给它:

Get-ChildItem -Path $tempfolder -include "*.sv$" -Recurse | Copy-item -Destination $destination 

【讨论】:

  • 谢谢,不知道这种可能性! Get-ChildItem -Path $tempfolder -filter "*.sv$" -Recurse | Copy-item -Destination $destination 将 -include 更改为 -filter 即可完成工作! :)
猜你喜欢
  • 1970-01-01
  • 2015-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-18
  • 2020-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多