-4

I am looking to use powershell to do following:

Use filename (excluding extension) from one folder (Folder1), search that filename exists in other folders(Folder2, Folder3) If file exists in Folder2 or Folder3, delete file from Folder1. If file doesnt exist in either 2 or 3, move file from Folder1 to Folder4

I made a start on this using Get-ChildItem to succesfully extract the basename, then got in a muddle trying use that information so i am basically starting from beginning if anyone can help.

LeeJ
  • 1
  • 2
  • 2
    Welcome to Stack Overflow. This is a forum where coders voluntarily help other coders with their challenges. ;-) There are some rules and best practices helping you to get the most out of SO. First you may take the [Tour] to get an overview and then you may stop by on [ask]. To make it easier for others willing to help you to actually help you you should always [format your code](https://stackoverflow.com/help/formatting) properly and provide a [mre]. ;-) Have a nice day! – Olaf Apr 13 '23 at 14:51
  • Get-ChildItem is your start to locate your file, as you've determined. Now, just pass that to another GCI to loop through the other directories for that name. This a day 1, hour 1 PowerShell course, or a quick Youtube (file and folder management) search to see samples of it as well. – postanote Apr 13 '23 at 17:42

2 Answers2

0

I made you a little script with the logic you described.

$Paths = @(
    "C:\Path1", 
    "C:\Path2", 
    "C:\Path3", 
    "C:\Path4")

$i = 1
foreach ($path in $Paths[0..2]) {
    New-Variable -Name "files$i" -Value (Get-ChildItem -Path $path -File)
    $i++
}

foreach ($file in $files1) {
    if ($file.BaseName -in ($files2.BaseName + $files3.BaseName)) {
        $file | Remove-Item -Confirm:$false
    }
    else {
        $file | Move-Item -Destination $Paths[3] -Confirm:$false
    }
}
  • Thats great thanks, it is giving me errors however - Cannot find path 'C:\Temp\\######\Folder1\########.bmp' because it does not exist. The remit change slightly to searching only one folder location, if exist move file to Folder2 , if doesnt move to folder4, so no deletions. I have a solution whihc will post but I will try to get your solution to work also. – LeeJ Apr 21 '23 at 12:28
0

#This appears to work for me just a bit more testing to check its robust #- it stores all files as an array, then looping through pulling out the basename #from files, then using testpath with the generated basename.anyextension. Then If #Else to move the files accordingly.

#source folder locations
$source1 = "C:\temp\Folder1"
$source2 = "C:\temp\Folder2"
$source3 = "C:\temp\Folder3"
$source4 = "C:\temp\Folder4"
#store all file names that are in $source1 as an array
$files = Get-ChildItem $source1 
#loop through each file in $files array, searching for existence of basename
foreach($file in $files.basename){
#checks if the file exists in $source2
if (Test-Path -path $source2\$file.*)
{
    #if file exists in $source2, do this
    Move-Item -path $source1\$file.* -Destination $source3
}
else
{
    #if file doesn't exist in $source2, do this
    Move-Item -path $source1\$file.* -Destination $source4
}

}

LeeJ
  • 1
  • 2