0

我正在尝试从一台机器复制多个文件,这些文件的名称每天都在变化,并且遇到了问题。名称始终以“库存状态”开头,然后以数字结尾。

我尝试了以下代码:

$strSourceFile = "C:\Test\[Stock]*"
$strTargetDir = "C$\Test\Test2"
$astrComputerList = ( "kburrows-xplt" )

if ([System.IO.File]::Exists($strSourceFile)) 
{
    foreach ($strComputer in $astrComputerList) {
        $strTargetPath = "\\$strComputer\$strTargetDir"
        copy-item $strSourceFile -destination $strTargetPath
    }
}

问题在于 strSourceFile 设置为字符串,但我认为它需要是一个表达式,这样才能工作。

有谁知道如何做到这一点?也许我做错了。

  Directory: C:\Test


Mode                LastWriteTime     Length Name                                                                                                                          
----                -------------     ------ ----                                                                                                                          
-a---         11/1/2013   3:09 AM    2954557 Stock Status Report88744.XML                                                                                                  
-a---         11/1/2013   3:25 AM     528934 Stock Status Report89386.XML                                                                                                  
-a---         11/1/2013   3:31 AM     103583 Stock Status Report89772.XML       
4

2 回答 2

1

这是我对此操作的建议:

$strSourceFile = "C:\Test\"
$strTargetDir = "C$\Test\Test2"
$astrComputerList = ( "kburrows-xplt" )

Get-ChildItem $strSourceFile -Filter "Stock Status*" | foreach{
  if (Test-Path -Path $_.FullName -PathType Leaf) 
  {
    foreach ($strComputer in $astrComputerList) {
        $strTargetPath = "\\$strComputer\$strTargetDir"
        copy-item $_.FullName -destination $strTargetPath
    }
  }
}

$_ 是管道中的当前对象。类似于其他语言中的“this”。允许您根据-Filter需要修剪结果。我相信-Include-Exclude允许您根据扩展名进行过滤,如果有帮助的话。

我也没有可用的 1.0,所以我无法完全测试。

于 2013-11-22T21:42:23.493 回答
1

尝试删除第一行中 Stock 周围的方括号。您可以在字符串中使用通配符作为 Copy-Item 的一部分。

此外,您可以更换

[System.IO.File]::Exists($strSourceFile) 

Test-File $strSourceFile

我刚刚注意到您将此标记为 PowerShell 1.0,因此通配符可能不适用于该版本上的 Copy-Item。在这种情况下,请尝试以下操作:

Get-ChildItem $strSourceFile | Copy-Item -Destination $strTargetPath

不幸的是,我没有一个带有 PowerShell 1.0 的系统来方便地测试它。

于 2013-11-22T21:13:12.180 回答