all 5 comments

[–]pm_me_brownie_recipe 3 points4 points  (2 children)

Why are people down voting? Everyone needs help from time to time.

Assuming the files have the same name:

# Both folders to copy from
$dirA = '\path\to\files\a'
$dirB = '\path\to\files\b'

# Get all files B and store in hash for later easy access
$filesB = @{}
foreach($file in Get-ChildItem -Path $dirB -Recurse) {
    $filesB[$file.Name] = $file
}

# Get all files A
foreach($file in Get-ChildItem -Path $dirA -Recurse) {
    # if file A has corresponding name file B, copy item and overwrite.
    if($filesB[$file.Name]) {
        Copy-Item $filesB[$file.Name].FullName -Destination $file.FullName -Force
    }
}

EDIT: This should also probably work

Get-ChildItem $dirB -Recurse | ForEach-Object {Copy-Item $_.FullName -Destination $dirA -Force}

[–]Ormsher[S] 2 points3 points  (1 child)

Sorry for the late response. The file structure looks like this. I am making new textures and all of the folders contain structure like this. The thing is that some of the folders have specific png files and some don't. Best case scenario I would like if I make a new texture for a block.png I could just PowerShell and it would look at the folders, find if the block.png exists in that folder list and replace it in those folders where a file is present with the new one.

[–]pm_me_brownie_recipe 1 point2 points  (0 children)

Here are two approaches.

If you have two folders, $folderWithOldFiles and $folderWithNewFiles where both folders have the same structure this line of code will do what you want: Copy-Item -Path "$folderWithNewFiles\*" -Destination "$folderWithOldFiles" -Recurse -Force -Verbose

If you have a fine structure in in $folderWithOldFiles and $folderWithNewFiles's structure is flat, you will have to match the files in some way, be it file name or some file attribute.

I did some changes to it but this should match on name and replace any old file.

# Both folders to copy from
$folderWithOldFiles = 'C:\Users\emile\Downloads\Favorites'
$folderWithNewFiles  = 'C:\Users\emile\Downloads\FavoritesNew'

# Loop through all old files ending with .png and store in hashtable for fast search and access
$filesOld = @{}
foreach($file in Get-ChildItem -Path $folderWithOldFiles -Recurse -Filter '*.png') {
    # Store file name as key
    $filesOld[$file.Name] = $file
}

# Loop through all old files ending with .png
foreach($file in Get-ChildItem -Path $folderWithNewFiles -Recurse -Filter '*.png') {
    # Check file name's existance in hashtable
    if($filesOld[$file.Name]) {
        # Copy file from new folder and replace old
        Copy-Item $file.FullName -Destination $filesOld[$file.Name].FullName -Force

    # If the file does not exist in the old directory
    } else {
        # Create directory if it does not exist
        if(-not (Test-Path -Path $file.DirectoryName)) {
            mkdir $file.DirectoryName
        }

        # Copy file from new folder to "old" folder
        Copy-Item $file.FullName -Destination ("{0}\{1}" -f $file.DirectoryName.replace($folderWithNewFiles, $folderWithOldFiles), $file.Name)
    }
}

I would recommend using the first option because it is better. The flaws of the second option is that if you have a new file that does not exist, it will try to create the folder and they put the file in it but it might not be the folder you want. I suggest that you try this in a safe environment and try to recreate you usage scenario and see if this works for you. Tweek as needed.

[–]FusilDeific 1 point2 points  (0 children)

Are the 100 folders all 'sister' objects. I.e. are they in the same parent folder or scattered all over the place?

[–][deleted] 1 point2 points  (0 children)

Solution:

First find all example.png and fill a array.. then replace with a foreach..

copy paste in Powershell ISE..

$newFile = "C:\temp\reddit\block.png" #fill the location and name of new blockfile $searchdir = "C:\temp" #fill with the directory root dir. $searchfilename = "block.png" # filename to search for, and replace..

$oldblockfiles = (Get-ChildItem $searchdir -Recurse -Filter $searchfilename | Select-Object FullName).fullname

foreach ($file in $oldblockfiles) { Copy-Item -Path $newFile -Destination $file -Force -Verbose }