PowerShell脚本将文件从映射的网络驱动器复制到同一映射的网络驱动器的子文件夹,然后将日期添加到文件名中

问题描述 投票:0回答:1

我是PowerShell的新手,已经阅读了许多类似的问题。对于我想做的事情,答案似乎很复杂。我想:

  1. 复制文件,testVerifyLog.xlsm
  2. 粘贴到相同的映射网络驱动器的子文件夹中:日志副本
  3. 重命名文件,并将当前日期(YYYYMMDD)添加到因此,到2020年6月1日的最终结果是:testVerifyLog_20200601.xlsm

下面的代码是我想出的,但是显然不起作用。我在PowerShell ISE中遇到的错误是:

copy-item:路径中的非法字符。

在\ TheMappedDrive \ Share \ AllDepts \ OneDept \ VerifyLogsMove.ps:2 char:1

copy-item testVerifyLog.xlsm- 目的地'\ TheMappedDrive \ Share \ AllDepts \ OneDept ...

CategoryInfo:未指定:(:) [Copy-Item],ArgumentException

FullyQualifiedErrorID: System.ArgumentException,Microsoft.PowerShell.Commands.CopyItemCommand

Set-Location \\TheMappedDrive\Share\AllDepts
copy-item testVerifyLog.xlsm -destination '\\TheMappedDrive\Share\AllDepts\OneDept\Copies of Logs\testVerifyLog ; testVerifyLog-$(((get-date).ToUniversalTime()).ToString("yyyyMMdd")).xlsm'
powershell copy rename paste
1个回答
0
投票

这样的东西

$curDate = Get-Date -Format yyyyMMdd
$destinationFolder = '\\TheMappedDrive\Share\AllDepts\OneDept\Copies of Logs\testVerifyLog'
$destinationFile = Join-Path $destinationFolder -ChildPath ('testVerifyLog_' + $curDate + '.xlsm') 
copy-item '\\TheMappedDrive\Share\AllDepts\testVerifyLog.xlsm' -destination $destinationFile

© www.soinside.com 2019 - 2024. All rights reserved.