2021-07-28

How to get all files in multiple folders from array

I have an array of paths

$to_delete = @(
"D:\test\to_delete\", 
"E:\test\to_delete\", 
"F:\test\to_delete\"
)

The files in these folders I need to select, and then if conditions are met, I would delete one file from one of those folders and put a newly created file into the same drive in the "new" folder

$final_dir = @(
"D:\test\new\",
"E:\test\new\",
"F:\test\new\"
)

Please note that these folders could be named differently.

So what I have so far is:

$number = 0
$window_title = $host.UI.RawUI.WindowTitle
while($true){
    $new_file = Get-ChildItem -Path C:\zips\ -Filter *.zip | Select-Object -First 1
    if ($new_file.count -gt 0){
        $number++
          $move_to = $number % $to_delete.count
          $delete = Get-ChildItem -Path $to_delete[$move_to] | Select-Object -First 1
          write-host "Deleting, $($delete.fullname)"
          Remove-Item -Path $delete.fullname -Force
    
          write-host "Moving #$($number), $($new_file)"
          Move-Item -Path $new_file.fullname -Destination $final_dir[$move_to] -force
          write-host "Finished moving"
          write-host "Moved files: $($number)"
    
          $host.UI.RawUI.WindowTitle = "$($window_title) -- moved $($number) files --"
    }
}

Now, the issue as you may have noticed is when selecting the delete file. If there is no file in that folder it should try the other drives. The $move_to variable was used only so it wouldn't fill the drives one at a time. I'm willing to not use it if it's not possible. But I would still have the issue with selecting the delete file as I can't scan all the folders to find the next file to delete and it needs to put the new file in the corresponding drive's folder.


-The C:\zips folder is where the newly created file goes, first the script should delete a file from one of the folders $to_delete and place the new file into the corresponding $final_dir


Additionally, I'd like to know if this would hold from executing the next lines until the moving is complete?



from Recent Questions - Stack Overflow https://ift.tt/2VdvusW
https://ift.tt/eA8V8J

No comments:

Post a Comment