![如何根據 Excel 檔案中的編號來尋找影像?](https://rvso.com/image/1604632/%E5%A6%82%E4%BD%95%E6%A0%B9%E6%93%9A%20Excel%20%E6%AA%94%E6%A1%88%E4%B8%AD%E7%9A%84%E7%B7%A8%E8%99%9F%E4%BE%86%E5%B0%8B%E6%89%BE%E5%BD%B1%E5%83%8F%EF%BC%9F.png)
答案1
您可以在 powershell 中嘗試如下方法它當然可以優化,但我希望通過兩個步驟簡單地理解: - 第一個:根據您的輸入文件(.csv)創建目標樹 - 第二個:相應子目錄中的移動影像檔案)
#The process :
$BasedImages = "\\path\to\ImagesDirectory"
$csvfile = "\\path\to\Inputcsvfile.csv"
#Gather all subfolders in the Images Directory (only first level) and put the name of these folder in a var. The only property useful for later use is the Directory name. Useless to gather all properties
$ExistingSubDir = Get-ChildItem -Path $BasedImages -Directory | Select-Object -Property name
# Gather unique diagnosis in the input file and put in a var. The only useful property in the dx property for a later use. Useless to collect more info.
$UniqueDiagnosis = Import-Csv -Path $csvfile | Select-Object -property dx -Unique
# gather all images files FullName in the Images Directory and put in a var. it seems that only Name,DirectoryName, FullName properties will be usefull for later use
$AllImagesFiles = Get-ChildItem -Path $BasedImages -File | Select-Object -Property Name, DirectoryName FullName
# now First Step : build a Tree with subfolders named by the unique Diagnosis name.
foreach ($Diagnosis in $UniqueDiagnosis)
{
# search if a diagnosis dir name (dx field in the input .csv file) exist in the ImageDirectory and put the result in a var
if ($ExistingSubDir -contains $Diagnosis)
{
Write-Host "$ExistingSubDir is still existing, no action at this step" -ForegroundColor Green
}
else
{
New-Item -Path $BasedImages -Name $Diagnosis -ItemType Directory
Write-Host "a sub-directory named $Diagnosis has been created in the folder $BasedImages" -ForegroundColor Yellow
}
}
# At this step, you'll have some sub directories named with the name of all diagnosis (fied dx in the input file)
# Now Step 2 Time to move the files in the root folder
foreach ($image in $AllImagesFiles)
{
$TargetSubDir = Get-Item -Path $($image.fullName)
Move-Item -Path $($Image.FullName) -Destination ( Join-Path -Path (Split-Path -Path $($Image.DirectoryName) -Parent) -ChildPath $TargetSubDir)
Write-Host "the image named $($image.name) has been moved to the sud directory $TargetSubDir" -ForegroundColor Green
}
小心,我還沒有完全測試程式碼,請謹慎使用。
奧利佛