我有一个文件夹,其中包含一堆包含图片的文件,其命名格式类似于:
kitty_BW.jpg
puppy_B_W.jpg
ball_BlWh.jpg
rope_bw.jpg
shoe_Bw.jpg
kitty_C.jpg
puppy_Color.jpg
ball_c.jpg
rope_color.jpg
shoe_col.jpg
移动我需要的文件我创建了一个列表
kitty
puppy
ball
rope
shoe
所以我需要的是将 NOT 中的文件移到TXT文件列表 AND 中比这个月还大。
我开始使用此代码
$file_list = Get-Content C:\photoList.txt
$search_folder = "D:\photos"
$destination_folder = "C:\Backup"
foreach ($file in $file_list) {
$file_to_move = Get-ChildItem -Path $search_folder -Filter $file -Recurse -ErrorAction SilentlyContinue -Force | % { $_.FullName}
if ($file_to_move) {
Move-Item $file_to_move $destination_folder
}
}
但这只有在文件的整个名称在列表中并且正在移动的文件在列表中的情况下才有效,但是我想移动不在列表中且比本月更早的文件,所以我可以处置它们。
修改
我一直在尝试各种各样的事情,但由于某种原因,它没有比较
$file_list = "C:\FAphotoList.txt"
$search_folder ="C:\FAphotos"
$destination_folder = "C:\Backup\"
$first_day = $(Get-Date -UFormat "%Y / %m ") + "/ 01"
$list = (Get-ChildItem $search_folder | ? LastWriteTime -lt $first_day).name
echo $list | Select-String -Pattern '0183444'
Get-Content $file_list | % {
$list2 = ($list | Select-String -Pattern $_)
}
echo list2
echo $list2
#here is my problem
$list2 | % {
[array]::indexof($list,$_)
}
echo "final list"
echo $list
问题在于,当我尝试将列表与[array]::indexof($list, "kitty_BW.jpg")
进行比较时
它有效,但当我在样本中尝试时,我什么都没得到。
编辑2
$file_list = "C:\FAphotoList.txt"
$search_folder ="C:\FAphotos"
$destination_folder = "C:\Backup\"
$first_day = $(Get-Date -UFormat "%Y / %m ") + "/ 01"
$list = (Get-ChildItem $search_folder | ? LastWriteTime -lt $first_day).name
echo lista
Get-Content $file_list | % {
$list.Remove(($list | Select-String -Pattern $_))
}
$list | % {
Move-Item $file_to_move $destination_folder
}
echo "final list"
$list = (Get-ChildItem $search_folder | ? LastWriteTime -lt $first_day).name
echo $list
让我犯这个错误
Exception calling "Remove" with "1" argument(s): "Collection was of a fixed size."
At C:\FAphotos.ps1:17 char:5
+ $list += $list.Remove(($list | Select-String -Pattern $_))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
所以我试图投射到数组列表,但这没有用,所以我尝试了很多不同的东西。
答案 0 :(得分:0)
您有2.5个主要任务:
this month
更早的文件,此任务包括半个任务:您如何在本月的第一天获得?比较.txt
文件中 NOT 的文件名。
$first_day = $(Get-Date -Format "yyyy/mm") + "/01"
$list = (Get-ChildItem | ? LastWriteTime -lt $first_day).Name
Get-Content C:\photolist.txt | % {
$list.Remove(($list | Select-String -Pattern $_))
}
$list | % {
Move-Item $file_to_move $destination_folder
}
未经测试,请试一试,并告知我们结果。祝好运! :)
答案 1 :(得分:0)
经过一番挖掘和询问后,我终于开始工作了。
$baseWorkFolder = 'C:';
$searchFolder = Join-Path $baseWorkFolder "FAphotos";
$destFolder = Join-Path $baseWorkFolder "Backup";
$LastYear = '1/1/' + $(Get-Date -UFormat "%Y");
Write-Host "LastYear: $LastYear";
[string[]] $fileList = Get-Content "$($baseWorkFolder)\FAphotoList.txt";
Write-Host "FileList:`n`t$fileList" -foregroundcolor Green;
[string[]] $FilesInSearchFolder = Get-ChildItem $searchFolder -name;
Write-Host "FilesInfolder:`n`t$FilesInSearchFolder" -foregroundcolor Green;
$moveList = New-Object System.Collections.Generic.HashSet[String]
foreach ($file in $FilesInSearchFolder ){
$moveList.add($file)
}
Write-Host "FilesInfolder:`n`t$moveList" -foregroundcolor Green;
foreach ( $fileName in $FilesInSearchFolder ) {
foreach ( $empID in $fileList ) {
Write-Host "`n`tComparing Strings $fileName - $empID = $($fileName.IndexOf($empID))";
if ($fileName.IndexOf($empID) -ge 0) {
$moveList.remove($fileName);
}
}
}
Write-Host "Potential Remove List:`n`t$moveList" -foregroundcolor Red;
foreach ( $removeItem in $moveList ) {
$removegcItem = Get-ChildItem -Path $(Join-Path $searchFolder $removeItem);
Write-Host "`n`tProcessing $($removegcItem.FullName)";
Write-Host "`tChecking if file is older than $LastYear" -foregroundcolor Green;
if ($removegcItem.LastWriteTime -lt $LastYear) {
Write-Host "`tMoving $removegcItem to $destFolder";
Move-Item "$($removegcItem.FullName)" "$(Join-Path $destFolder $($removegcItem.Name))" ;
}
}
Write-Host "Final list (Newer than $LastYear):";
Write-Host "`t$( ( Get-ChildItem $searchFolder | Where-Object { $_.LastWriteTime -lt $LastYear } ).name )" `
-Foregroundcolor Yellow -BackgroundColor Black;
必须使用哈希集和另一个数组,因为我收到了一个不是可调整大小的对象的错误。
我认为我无法进行比较,因为我将字符串与路径进行比较。