![Excel ファイル内の番号に基づいて画像を見つけるにはどうすればよいですか?](https://rvso.com/image/1604632/Excel%20%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E5%86%85%E3%81%AE%E7%95%AA%E5%8F%B7%E3%81%AB%E5%9F%BA%E3%81%A5%E3%81%84%E3%81%A6%E7%94%BB%E5%83%8F%E3%82%92%E8%A6%8B%E3%81%A4%E3%81%91%E3%82%8B%E3%81%AB%E3%81%AF%E3%81%A9%E3%81%86%E3%81%99%E3%82%8C%E3%81%B0%E3%82%88%E3%81%84%E3%81%A7%E3%81%99%E3%81%8B%3F.png)
答え1
PowerShell で次のようなアプローチを試すことができます。確かに最適化できますが、2 つの手順で簡単に理解できるようにしたいと考えました。- 最初の手順: 入力ファイル (.csv) に従ってターゲット ツリーを作成する - 2 番目の手順: 対応するサブディレクトリに画像ファイルを移動する)
#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
}
注意してください。コードを完全にテストしていないので、注意して使用してください。
オリーブ