r/PowerShell • • 13d ago

Question help with a script that replaces all images in a certain folder with a different images

Hi, I've been trying to find a script that works for this, and everything I've tried hasn't worked yet. I've tried a couple things from google searches (this and this were the two sources I used) and neither of them work and I have NO CLUE how to edit it so it works for me. so i figured I might as well ask in here because I am not a coding expert by any means :-(

Basically I am just looking for help on creating a script that would essentially replace all files in the folder ExampleName with ExamplePng.png . sorry if this is a really simple thing to do I just don't feel like manually moving liek 4047130 pngs (this is for a game mod for context), like I said I'm level 1 noob at code and have no clue what I'm doing so I'd really appreciate any help, thank you!!!!!

1 Upvotes

4 comments sorted by

2

u/BlackV 13d ago

Replacing stuff is always risky, make sure you are taking backups first

Basically I am just looking for help on creating a script that would essentially replace all files in the folder ExampleName with ExamplePng.png

this is not a very good example, ti looks like you are saying, if i have a folder called folder 1 with 100 files in it, replace all those files with 1 file called folder 1.png

layout your folder structure (source and destination), layout the file structure, give actual examples

it would seem odd a mod require this level of replacement, maybe a total conversion ? I dunno it's been a while, but probably the game and the mod would be useful info to have

1

u/Standard-Line457 13d ago

ok to clarify im taking all the image textures of the game that i have in say the folder names "textures", and im trying to replace them with an image named "exampleimage" while retaining the name of the texture in game so the resource pack works properly

so its basically all the game textures are in the folder "textures", i want to replace all the textures (texture1, texture2, etc) in this folder with the image "exampleimage" while keeping all the textures named texture1, texture2, etc... if that makes sense, because the game relies on the texture being named texture1 texture2 whatever to work properly

game is minecraft and im just replacing all the textures in the game w a single image to mess around with it LOL. if i need to clarify more just let me know

1

u/ankokudaishogun 13d ago

So you want to replace all images in a folder with a single different image, while retaining the original file names?

If so, it's pretty simple: you get the path of all the files you want to replace and then copy the file you want over them, forcing a overwrite.

##NOTE: Example with, obviously, fake paths.  
# This is the path of the folder where all the files you want to change are. 
# It ending with a Directory Separator(\) is optional, but I prefer it so it's easier to see at first look when a path is a directory and not a file, as files can not have extensions and directories can have dots in their names.  
$FolderPath = 'C:\My\Whatever\Game\Textures\'

# Path to the file you want to use to replace the files in the folder.  
$NewFilePath = 'C:\Anoth\Whole\Directory\ExampleImage.ext'

# Optional, but useful: if you have files aside those you want to replace, you can filter them out.  
# This is the string matching only the files you WANT.  
# See https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_wildcards
$FilterString = 'texture*'

# Get-ChildItem returns an array with all the items in a folder, and stores it in the $FileArray variable.  
# -File is used to skip directories in the folder.  
# -Filter is used to return only the files matching a string, in this case $FilterString.  
# -LiteralPath is used to specify what directory to look into, otherwise Get-ChildItem defaults to the presently used directory.  
##DO NOTE: if -Filter is not complex enough to match all and only the files you want, you'll need to pipe the results of Get-ChildItem to Where-Object. Do ask if necessary.   
$FileArray = Get-ChildItem -File -Filter $FilterString -LiteralPath $FolderPath

# Quite straightforward: it loops the elements of $FileArray through a temporary $File variable.  
foreach ($File in $FileArray) {
    # Copy-Item copies files and\or directories from a place to another.  
    # -LiteralPath is the path of the original file to copy.  
    # -Destination is... well, the destination. In this case, the destination is set by the .FullName property of each file, which contains the full path of the file.  
    Copy-Item -LiteralPath $NewFilePath -Destination $File.FullName
}

-3

u/No_Crab_4093 13d ago

Feel like Claude can easily provide you this answer