$InputFile = "C:\InfoWorkflows.txt"
$fileText = Get-Content $InputFile
foreach ($LineText in $fileText){
echo "LineText is $LineText"
$containsWord = $LineText | %{$_ -match "test_control.xml"}
if($containsWord -eq $false)
{
$SplitText = $LineText -split' '
$Change = $SplitText[0]
$fileName = $SplitText[1]
echo "fileName is $fileName"
echo "Copy-Item -Path C:\mb\INFO\$fileName -Destination C:\BackOrder\INFOT"
}
}
在目标文件夹C:\ BackOrder \ INFOT中,在执行PS时,我能够看到inputFile中提到的PS“ test_Control.xml”以外的文件,并且还从Source Path创建了空白文件夹(INFO)。我从未提供过任何指示。有人可以调查并帮助我吗?
您拥有的代码不会检查输出文件夹'C:\ BackOrder \ INFOT'是否存在,也不会检查'C:\ mb \ INFO \ $ fileName'是否存在。除此之外,我不明白您为什么要对Copy-Item
cmdlet进行细线处理。
另一件事是,您-match
对包含句点(.
)字符的文件名使用regex运算符,而不对其进行转义。在Regex中,点有特殊含义,即“任何字符”。
下面的代码使用String对象方法.Contains
过滤Where-Object{}
子句中不包含“ test_control.xml”的字符串。在代码注释中,[Regex]::Escape()
如果您想继续使用,则可以阅读使用方法-match
。
$InputFile = 'C:\InfoWorkflows.txt'
$OutputDir = 'C:\BackOrder\INFOT'
# test if output folder exists and if not create it first
if (!(Test-Path -Path $OutputDir -PathType Container)) {
Write-Host "Creating folder $OutputDir"
$null = New-Item -Path $OutputDir -ItemType Directory
}
# read the input file as string array and filter the lines that do not contain "test_control.xml")
# if you want to do that with the regex -match operator, you should escape it first:
# [Regex]::Escape("test_control.xml") --> "test_control\.xml"
$fileText = Get-Content $InputFile | Where-Object { !$_.Contains("test_control.xml") } | ForEach-Object {
$fileName = ($_ -split ' ')[1]
Write-Host "FileName is $fileName"
# combine the path and file name for the file to copy
$sourceFile = Join-Path -Path 'C:\mb\INFO' -ChildPath $fileName
if (Test-Path -Path $sourceFile -PathType Leaf) {
Copy-Item -Path $sourceFile -Destination $OutputDir
}
else {
Write-Warning "File '$sourceFile' could not be found."
}
}
希望能有所帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句