I have 1,015 images in a folder and this image is classified into seven types and these types are categories of medical diagnoses of skin cancer, and the same images are classified in the Excel file by number (the number of each picture corresponds to the type of diagnosis of this image). Clearly, each image has a specific number corresponding to the diagnostic type, my question is how to sort these images in the folder in groups so that each group of images have the same diagnosis.

enter image description here enter image description here enter image description here enter image description here

  • There is no Diagnosis column in your data. And it is unclear what result does you want to achieve.
    – Akina
    Sep 24, 2019 at 10:23
  • diagnosis column is has label " dx" , I want to search for multiple of image files at once from the folder (ISIC2018_task 3_training_input) , like (ISIC_27320, ISIC_24306, ISIC_25370, and so on ) Sep 24, 2019 at 10:53
  • Well... you want to search... you searches... then? what result does you want to achieve? to send the found filenames list to a printer? to build anagramma from them? to summ the numbers in filenames? to calculate average image size? WHAT IS THE RESULT WHICH YOU NEED?
    – Akina
    Sep 24, 2019 at 11:18
  • You asked me what result I wanted to achieve ,well, I want to search for multiple files in this folder at once , like (SIC_27320, ISIC_24306, ISIC_25370 ,SIC_27320, ISIC_29306, ISIC_33472, .......) by windows search Sep 25, 2019 at 7:04
  • I want to search for multiple files in this folder at once This is process. Whereas I have asked about visible final result. For example, you may want to mark all found filenames in your worksheet with some color. Or open Explorer window with all these files selected. Or put filenames list on newly inserted worksheet. Or move these files into subfolder. Or something else. I don't know how to explain more clear...
    – Akina
    Sep 25, 2019 at 7:18

1 Answer 1


You can try an approch like the following in powershell It can certainly be optimized, but I wanted it simple to understand with 2 steps : - The first one : creation of the target tree according to your input file (.csv) - The second one : moving image files in the corresponding subdirectories)

#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 
        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

Careful, i haven't completly tested the code, use with caution.


  • How can i execute this code ? Sep 25, 2019 at 7:05
  • I have run the code by windows powershell , But I didn't get any result. Sep 25, 2019 at 8:17
  • I have run the code above , but I didn't understand. Can I change the path of the images folder and the Excel file to what is on my computer?, also i get an error with " full name" in code . Sep 29, 2019 at 8:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .