我对此并不陌生,所以希望我做对了。过去几天,我一直在努力研究此代码,但我无法让它做我想做的事情。
我有一个包含几个目录的源目录。我有一个包含几个目录的目标目录。目录名称中都有一个四位数的数字,但名称不在同一位置。我想在每个源目录的名称中找到该数字,并使用该数字在目标目录中的名称中使用相同的数字。找到匹配的目标目录后,将带有文件夹名称33-fit-1234的源目录移动到具有匹配编号(即)的目标目录中。 33-F-1234。我目前正在通过复制然后删除源进行移动。
源文件夹名称可以是33-ait-1743A或33-xx-4533 UNKNOWN,等等。
目标文件夹的名称均采用以下格式:33-x-####,其中一些带有单个字母后缀,例如A,B等。
代码如下:
$sourceDirectory = "D:\Test Source"
$destinationDirectory = "D:\Test Dest"
$sourceFolders = Get-ChildItem -Path $sourceDirectory -Directory | Select-Object -ExpandProperty Name
$destinationFolders = Get-ChildItem -Path $destinationDirectory -Directory | Select-Object -ExpandProperty Name
$matchesInBoth = $sourceFolders -match "\d{4}" | ?{$_ -eq $_}
$matchesInBoth | ForEach-Object{
$sourcePath = (Join-Path $sourceDirectory $_)
Copy-Item -Path $sourcePath -Destination
$destinationDirectory\$matchesInBoth\$_ -Recurse -whatif
Remove-Item $sourcePath -Force -Recurse -WhatIf
}
很接近,我得到的输出是:
What if: Performing the operation "Copy Directory" on target "Item: D:\Test
Source\33-FV-1414 Destination: D:\Test Dest\33-FIT-1414 33-FIT-1576 33-FIT-
1654A 33-fred-1234 unknown 33-FSL-1649 33-FV-1414 33-FV-1654 33-FV-1882
33-H-1657 33-H-1814 33-H-1924\33-FV-1414".
What if: Performing the operation "Remove Directory" on target "D:\Test
Source\33-FV-1414".
问题是: 我最终将源文件夹复制到一个以我的源目录中所有文件夹名称命名的新文件夹中。
我也尝试了此代码,但没有结果:
$sourceDirectory = "D:\Test Source"
$destinationDirectory = "D:\Test Dest"
$sourceFolders = Get-ChildItem -Path $sourceDirectory -Directory | Select- Object -ExpandProperty BaseName | Select-String "\d{4}"
$destinationFolders = Get-ChildItem -Path $destinationDirectory -Directory | Select-Object -ExpandProperty BaseName
$matchesInBoth = $destinationFolders | ?{$_ -contains $sourceFolders}
$matchesInBoth | ForEach-Object{
$sourcePath = (Join-Path $sourceDirectory $_)
Copy-Item -Path $sourcePath -Destination $destinationDirectory\$matchesInBoth\$_ -Recurse -whatif
Remove-Item $sourcePath -Force -Recurse -WhatIf
}
源文件夹名称为:
"D:\Test Source\33-FIT-1414"
"D:\Test Source\33-FIT-1576"
"D:\Test Source\33-FIT-1654A"
"D:\Test Source\33-fred-1234 unknown"
"D:\Test Source\33-FSL-1649"
"D:\Test Source\33-FV-1414"
"D:\Test Source\33-FV-1654"
"D:\Test Source\33-FV-1882"
"D:\Test Source\33-H-1657"
"D:\Test Source\33-H-1814"
"D:\Test Source\33-H-1924"
"D:\Test Source\asjhdsdlljhsdflj"
目标文件夹名称为:
"D:\Test Dest\33"
"D:\Test Dest\33-F-1108"
"D:\Test Dest\33-F-1111"
"D:\Test Dest\33-F-1120"
"D:\Test Dest\33-F-1125"
"D:\Test Dest\33-F-1130"
"D:\Test Dest\33-F-1135"
"D:\Test Dest\33-F-1209"
"D:\Test Dest\33-F-1223"
"D:\Test Dest\33-F-1252"
"D:\Test Dest\33-F-1254"
"D:\Test Dest\33-F-1307"
"D:\Test Dest\33-F-1309"
"D:\Test Dest\33-F-1317"
"D:\Test Dest\33-F-1351"
"D:\Test Dest\33-F-1414"
"D:\Test Dest\33-F-1426"
"D:\Test Dest\33-F-1428"
"D:\Test Dest\33-F-1432"
"D:\Test Dest\33-F-1433"
"D:\Test Dest\33-F-1434"
"D:\Test Dest\33-F-1435"
"D:\Test Dest\33-F-1451"
"D:\Test Dest\33-F-1476"
"D:\Test Dest\33-F-1526"
"D:\Test Dest\33-F-1528"
"D:\Test Dest\33-F-1532"
"D:\Test Dest\33-F-1533"
"D:\Test Dest\33-F-1554"
"D:\Test Dest\33-F-1565"
"D:\Test Dest\33-F-1576"
"D:\Test Dest\33-F-1623"
"D:\Test Dest\33-F-1626"
"D:\Test Dest\33-F-1649"
"D:\Test Dest\33-F-1654"
"D:\Test Dest\33-F-1659"
"D:\Test Dest\33-F-1671"
"D:\Test Dest\33-F-1709"
"D:\Test Dest\33-F-1712"
"D:\Test Dest\33-F-1799"
"D:\Test Dest\33-F-1800"
"D:\Test Dest\33-F-1801"
"D:\Test Dest\33-F-1882"
"D:\Test Dest\33-F-1883"
"D:\Test Dest\33-F-2000"
"D:\Test Dest\33-F-2001"
"D:\Test Dest\33-F-2002"
"D:\Test Dest\33-ggg-1234"
"D:\Test Dest\1234"
"D:\Test Dest\daddybear"
有想法吗?
答案 0 :(得分:1)
您的问题很含糊,
[
{
"Address": 25,
"AlertType": 1,
"Area": "North",
"Date": "2019-02-01T00:01:01.001Z",
"Value": -1
},
{
"Address": 26,
"AlertType": 1,
"Area": "West",
"Date": "2016-04-12T15:13:11.733Z",
"Value": -1
},
{
"Address": 25,
"AlertType": 1,
"Area": "North",
"Date": "2017-02-01T00:01:01.001Z",
"Value": -1
}
.
.
.
]
Move-Item
样本输出:
(在我的ramdrive A上有文件夹:)
## Q:\Test\2018\10\18\SO_52866947.ps1
$SrcDir = "A:\Test\Source"
$DstDir = "A:\Test\Dest"
# Build hash table from Destination with 4digit key and Directory FullName as value
$DstHash = @{}
Get-ChildItem $DstDir -Directory | Where-Object Name -match '\d{4}'| ForEach-Object {
$DstHash.$($Matches.Values)=$_.FullName
}
# Iterate source and check if destination key/value pair exists
Get-ChildItem $SrcDir -Directory | Where-Object Name -match '\d{4}'| ForEach-Object {
if ($DstHash.ContainsKey($($Matches.Values))){
"Move {0} to {1}" -f $_.FullName, $DstHash.$($Matches.Values)
Move-Item $_.FullName -Destination $DstHash.$($Matches.Values) -Force
} else {
"no corresponding Destination found for: {0}" -f $_.FullName
}
}