# Move Groups of Files from List (Array)

**URL:** <https://forums.powershell.org/t/move-groups-of-files-from-list-array/24747>\
**Category:** PowerShell Help\
**Created:** [September 14, 2024, 8:39pm UTC](https://forums.powershell.org/t/move-groups-of-files-from-list-array/24747 "2024-09-14T20:39:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zero269](https://avatars.discourse-cdn.com/v4/letter/z/da6949/32.png) [@zero269](https://forums.powershell.org/u/zero269)\
**Post date:** [September 14, 2024, 8:39pm UTC](https://forums.powershell.org/t/move-groups-of-files-from-list-array/24747/1 "2024-09-14T20:39:24Z")

</div>

Hello,

I just created a script that will loop through a CSV file that will:

1. Create a folder
2. Get the first ‘n’ number of files to move
3. Move those identified files by name in a 2nd level loop (foreach)

```auto
# Root Path
$RootPath = 'D:\_TEMP\Move_Files'

# Source File
$csv_file = "$RootPath\Move_Files.csv"

# Import CSV > Process each row of data
$data = Import-CSV $csv_file | ForEach-Object{
    # Create Folder
    New-Item -ItemType Directory -Name $_.folder #-WhatIf
    # Get n files to be moved
    $files = (Get-ChildItem -Path $RootPath -Filter "*.png" | Select-Object -First $_.Files).name
    
    # Pause execution for .25 seconds
    Start-Sleep -Seconds .25
    # Move Files
    foreach ($file in $files){
        Move-Item $file -Destination $_.folder #-WhatIf
    }
}

```

I ran into an issue where some files didn’t wind up in the correct folder. For example, IMG\_3687.PNG was skipped, while IMG\_3688.PNG ended up in the folder by mistake. Appears to be a filesystem refresh issue as my first test had issues with the folder not existing, so I added a 1/4 second delay and no issues. However, on a larger batch of files, I ran into this 2nd issue regarding those PNG files.

I’m thinking the best approach would be to just create a list of _all_ the PNG files in the directory instead of doing that within the loop for each group/batch of files.

However, I’m not entirely sure how to use the 2nd array and continue down that list, while at the same time looping through my source file that contains the number of files that need to be moved.

My CSV file contains 3 columns:  
`Folder,Files,Order`

The **Folder** name to be created, the _number_ of **Files** that will be moved to that Folder, and the **Order** of processing the files/folders. The CSV files is sorted by the **Order** column as they line up with the order of the PNG files in the source folder.

Thank you; any help would be greatly appreciated…

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 14, 2024, 9:04pm UTC](https://forums.powershell.org/t/move-groups-of-files-from-list-array/24747/2 "2024-09-14T21:04:18Z")

</div>

Hmmm … you’re overcomplicating this a little I think … This should work:

```auto
Import-CSV $csv_file | 
ForEach-Object{
    New-Item -ItemType Directory -Name $_.folder #-WhatIf
    
    Get-ChildItem -Path $RootPath -Filter '*.png' | 
        Select-Object -First $_.Files |
            Move-Item -Destination $_.folder
    }

```

**Edit:**

BTW:

If you set the “$RootPath” outside of the loop you’re copying the same files again and again just to different target folders contained in your CSV file. Is that by purpose?

---

<div class="post-metadata">

**Author:** ![zero269](https://avatars.discourse-cdn.com/v4/letter/z/da6949/32.png) [@zero269](https://forums.powershell.org/u/zero269)\
**Post date:** [September 15, 2024, 2:24am UTC](https://forums.powershell.org/t/move-groups-of-files-from-list-array/24747/3 "2024-09-15T02:24:21Z")

</div>

Hi Olaf,

Good to hear from you… it’s been awhile since I’ve come here for help. Kinda took a step back from POSH… and I absolutely regret it. I feel completely lost again.

> [@Olaf](#):
>
> Hmmm … you’re overcomplicating this a little I think …

I can’t argue with that. 🙂

> [@Olaf](#):
>
> If you set the “$RootPath” outside of the loop you’re copying the same files again and again just to different target folders contained in your CSV file.

I’m moving the files which is why I’m experience that system refresh issue.

Ironically, when I first ran your code, I thought it was copying the files. I noticed after the test was done, the files I expected to MOVE were still there. It turns out that POSH handles _ **Selections** _ differently based on the filesystem. I never knew that, and it makes sense considering it was moving files based on when they were created and not by the filename.

So, after some more online research, it turns out I needed to **Sort** the data before piping it into **Select-Object**. That worked and I just ran another test against 140 files and all were moved to the correct place.

A side note: I used that same method to **Sort** my CSV import so I didn’t have to do it manually before importing the data. 👍🏼

Final results with your code and my inserted **Sort-Object** _s_:

```auto
# Root Path
$RootPath = '\\nas\photo\Questions'

# Source File
$csv_file = "$RootPath\Move_Files.csv"

Import-CSV $csv_file | Sort-Object { [double]$_.order } |
ForEach-Object{
    New-Item -ItemType Directory -Name $_.folder #-WhatIf
    
    Get-ChildItem -Path $RootPath -Filter '*.png' | Sort-Object Name |
        Select-Object -First $_.Files |
            Move-Item -Destination $_.folder #-Force #-WhatIf
}

```

Thank you again, Olaf. This was very helpful…

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 15, 2024, 7:37am UTC](https://forums.powershell.org/t/move-groups-of-files-from-list-array/24747/4 "2024-09-15T07:37:33Z")

</div>

> [@zero269](#):
>
> I’m moving the files which is why I’m experience that system refresh issue.

You’re right. 👌🏼 I think my mind stuck with a copy job - not a move job. 🤷🏼‍♂️

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [October 15, 2024, 7:38am UTC](https://forums.powershell.org/t/move-groups-of-files-from-list-array/24747/5 "2024-10-15T07:38:20Z")

</div>

Closed on October 15, 2024, 7:38am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
