Help with a script!

Soldato
Joined
14 Mar 2005
Posts
16,998
Location
Here and There...
I need a script that will do the following.

-Delete all the JPG's in every folder on a drive EXCEPT 'Copy of Folder.JPG'
-then make a copy of 'Copy of Folder.JPG' called Folder.jpg in each folder.

I'm sure it's pretty straight forward and I've done some similar stuff before but was wandering of anyone had anything which would do the job already.

For the inevitable question of why, thats simple because it is impossible to prevent Windows Media Player occasionally overwriting my custom Album art and replacing them manually is boring.
 
Something like these two commands should work from a CMD prompt...


FOR /F "delims=" %A IN ('DIR "*.jpg" /B /S^|FIND /V /I "Copy of"') DO (DEL "%A")

FOR /F "delims=" %A IN ('DIR "Copy of Folder.jpg" /B /S') DO (COPY "%A" "%~pAFolder.jpg" /Y)


Replace the %A with %%A if putting in a bat/cmd file.

The usual, use at own risk, please test before doing a live run etc, disclaimers apply. :)

Let me know if you need any of it explaining as to what it is doing.
 
Completely forgot I started this thread! Thanks for the reply!

I've just had recourse to use it, bloomin windows media player and doing just as it pleases!

What I forgot to specifiy in my request was that all the JPG's in the folders that I need to delete are hidden files can anyone tell me how to change the first command so it works on hidden files?
 
This should do what you want using PowerShell. I don't use that ugly batch rubbish :P

Code:
#Customise Vars
$FolderToCheck = "C:\TEMP"
$FileFilter = "*.jpg"
$TextString = "Copy of"

#Get all .jpg files from the folder. -Force ensures we get Hidden items. -Recurse ensures all subfolders are checked.
[array]$FolderItems = gci $FolderToCheck\* -Recurse -Include $FileFilter -Force

if ($FolderItems.Count -gt 0)
{
	#Delete all files that don't match the text string. Needs force becuase the file may be hidden.
	$FolderItems -notmatch $TextString | Remove-Item -Force
	
	#Gets all matching files
	$MatchingFiles = $FolderItems -match $TextString
	
	#Loop through the files
	foreach ($File in $MatchingFiles)
	{
		#Concat new path
		$NewPath = $File.DirectoryName + "\" + (($File.Name -Replace $TextString).Trim())
		
		#Make a copy of the file in the new path
		Copy-Item $File $NewPath
	}
}
 
Blast from the past I know!

Just wanted to say thanks for the above script, I had cause to use it yesterday and it worked perfectly apart from not getting on with directory names that contained [] which have now all been removed!
 
Back
Top Bottom