web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / For Each 2 current ite...
Power Automate
Unanswered

For Each 2 current items simultaneously

(0) ShareShare
ReportReport
Posted on by 141

My Problem: I'm trying to find uncommon items between 2 list, except the file directory is different locations so i just need the current name from each. List 1 contains 520 and List 2 contains 496, so basically i want to remove uncommon items so that both list.count match with all matching Job Names between Folder and File name. This will increase the speed for another automation i have. 

 

I know how to do this the slow way, but i want a very quick version, kind of like how excel instantly highlights common variables as there is about 500 of each and 1000 folders/files takes a long time to use a "For Each" for.

 

One list contains subfolders within a folder named the Job folder and the other is a list of files named job files as well. They Both have Currentitem.name similar properties, E.g. File = 26734 Folder = 26734.

 

In theory i could run an excel automation to do this? But not the most familiar with that format. The theory behind that automation would be: Remove common directory name from list 1 and remove common directory name for list 2 (Which is a different directory). Then Im left with 2 list that only has Job numbers. Then highlight common items and move unhighlight items to List 3. From list 3 move folders to "do not match Folder". Done... 

 

Any ideas? 

 

Reference image for excel: image.pngAs you can see there is 1 folder that doesnt match any file and 1 file that doesnt match any folder. These id want to move to their own list. 

 

I have the same question (0)
  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    Are you okay doing this via command line?  My example took 2 seconds and had 100 files in each folder with 20 differences (10 from each folder).  The reason it is so fast is that it builds out the two tables of files as arrays, makes the unique lists immediately, and moves all the files in the unique lists at the same time, so no loop.

    Here is the text for the .bat file that you need to copy to notepad and save down as "[filename].bat" with whatever filename you want:

    @Echo off
    setlocal enabledelayedexpansion
    
    :: Assigning folders from arguments
    set "sourceFolder1=%~1"
    set "sourceFolder2=%~2"
    set "targetFolder=%~3"
    
    :: Create an associative array for files in Jobs1
    for %%F in ("%sourceFolder1%\*.csv") do (
     set "file1=%%~nxF"
     set "exist1[!file1!]=yes"
    )
    
    :: Check each file in Jobs2 and see if it exists in Jobs1, move if not
    for %%F in ("%sourceFolder2%\*.csv") do (
     set "file2=%%~nxF"
     if not defined exist1[!file2!] move "%%F" "%targetFolder%\"
    )
    
    :: Create an associative array for files in Jobs2
    for %%F in ("%sourceFolder2%\*.csv") do (
     set "file2=%%~nxF"
     set "exist2[!file2!]=yes"
    )
    
    :: Check each file in Jobs1 and see if it exists in Jobs2, move if not
    for %%F in ("%sourceFolder1%\*.csv") do (
     set "file1=%%~nxF"
     if not defined exist2[!file1!] move "%%F" "%targetFolder%\"
    )
    
    endlocal


    4 inputs either in the top right or setup as "Set Variable" actions to create the inputs:

    MichaelAnnis_1-1705966736502.png

     

    Then, 1 line:  Run Application; here are the parameters (where "no_matches" should be your [filename] for the .bat file:

     

    MichaelAnnis_2-1705966769442.png

     

    This will run with 1 action from PAD and almost instantly move any non matching files from the two source folders to the %TargetFolder%.

    If I misread your requirements, please let me know.

     

    Thanks,


    Mike

  • jkingno1 Profile Picture
    141 on at

    Hi, sorry for only just getting back to you. 

    Did yours have folders in 1 and files (Images) in the other? E.g. one 'Folder contains files(Images)' and the other 'Folder contains folders'? As i seems to not be working for my problem. 

     

     

  • jkingno1 Profile Picture
    141 on at

    @MichaelAnnis 

    jkingno1_0-1707213408731.png

    I made this as a temporary fix but no where near as fast as 2 seconds and only does 1 of the 2 types (I had this remove 28 Folders that were marked as 'Non-Matching from my excel spreadsheet) then i manual removed the none matching files so both are now 462 each which makes the next automation a breeze 🙂 This is a pretty common thing that comes up with my automation, so ideally id like something that works effectively, if we can work out what's wrong with your above suggestion as 2 seconds sounds super good!

  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    Apologies.  I misread the original ask.  Mine was to compare two sets of files, not one set of files and one set of subfolders.

    Just to confirm, we have as inputs:
    %MainFolder% (where all the subfolders reside)
    %FileFolder% where all the files reside

    %TrashFolder% where we want to move all files or subfolders that don't match

     

    Otherwise the logic is the same:

    Get all %Subfolders% in %MainFolder%. 

    Get all %Files% in %FileFolder%.

    For each %Subfolder% in %Subfolders%

        If %Subfolder% does not exist in %Files%, move %Subfolder% to %TrashFolder%

    End (for each)

    For each %File% in %Files%

        If %File% does not exist in %Subfolder%, move %File% to %TrashFolder%

    End (for each)

    If anything I said in the above logic or required inputs (understanding some .name/parsing requirements) is not correct, please let me know.  Once I have the process down, I can get the .bat corrected.

     

    Regards,

     

    Mike

  • jkingno1 Profile Picture
    141 on at

    @MichaelAnnis Yes, that logic sounds correct to me. I'm keen for the .bat file 🙂 

  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    I haven’t been able to test this because I’m on my phone. Good luck.

     

    @Echo off
    setlocal enabledelayedexpansion

    :: Assigning the main folder, file folder, and trash folder from arguments
    set "MainFolder=%~1"
    set "FileFolder=%~2"
    set "TrashFolder=%~3"

    :: Create an associative array for subfolders in the MainFolder
    for /d %%D in ("%MainFolder%\*") do (
    set "subfolder=%%~nxD"
    set "existSubfolder[!subfolder!]=yes"
    )

    :: Check each file in FileFolder, move to TrashFolder if no corresponding subfolder exists
    for %%F in ("%FileFolder%\*.*") do (
    set "file=%%~nF"
    if not defined existSubfolder[!file!] move "%%F" "%TrashFolder%\"
    )

    :: Reset the associative array for reuse
    set "existSubfolder="

    :: Create an associative array for files in FileFolder
    for %%F in ("%FileFolder%\*.*") do (
    set "file=%%~nF"
    set "existFile[!file!]=yes"
    )

    :: Check each subfolder in MainFolder, move to TrashFolder if no corresponding file exists
    for /d %%D in ("%MainFolder%\*") do (
    set "subfolder=%%~nxD"
    if not defined existFile[!subfolder!] move "%%D" "%TrashFolder%\"
    )

    endlocal

     

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 261 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 185

#3
Haque Profile Picture

Haque 168

Last 30 days Overall leaderboard