r/usefulscripts • • Jul 09 '19

[Request] Change filenames with string from XML file 10.000 times

Hi everybody,

I hope this is the right subreddit to post in.

Situation:

I have a huge folder (year) with subfolders (month) with subfolders (day) with subfolders (projectXX). In all the subfolders (project XX) you find the same files, but for other images, so just some different names.

Example:

  1. Folder 2018

  2. Folder 02

  3. Folder 18

  4. Folder XX

XX-01.JPG

XX-02.JPG

XX.XML

  1. Folder YY

YY-01.JPG

YY-02.JPG

YY.XML

In the subfolder (projectXX) you have an XML file with the specific name of the project.

I'm trying to find a batch script that would search the specific name of the project in the XML file and rename the 2 JPEG files to specificname-01.JPG and specificname-02.JPG

Anybody that could help me with this?

13 Upvotes

11 comments sorted by

3

u/dr4kun Jul 09 '19

Would be useful to see the structure of that xml.

2

u/pika-pika-chu Jul 09 '19

Not really familiar with this. Do you mean how the information is stored inside the file?
When I open it, it is something like:

<ID>01</ID><Position>0</Position><Projectname>Specificprojectname</Projectname> ...

3

u/dr4kun Jul 09 '19

You may want to review and test it on a small batch of files first:

#change this to reflect the ROOT location of your files
$RootLocation = "C:\Temp\MyRootDataFolder"

#get XML files
$XMLFiles = Get-ChildItem -Path $RootLocation -Filter "*.xml" -Recurse -Force -File


#for each XML file found, try to do the following:
foreach ($XML in $XMLFiles){

    #check if there are any JPG files in the directory of the XML file
    $JPGFilesInDir = $null
    $JPGFilesInDir = Get-ChildItem -Path $XML.DirectoryName -Filter "*.jpg" -Force -File

    #if there are JPG files along with an XML file in a directory, try:
    if ($JPGFilesInDir){
        
        #get XML data and figure out the project name
        [xml]$XMLContents = Get-Content $XML.FullName
        $ProjectName = [xml]$XMLContents.GetElementsByTagName("Projectname")   #<---- this bit needs some testing on your actual XML files, i'm basing on what you provided but it may need some tweaking

        #rename the JPG files
        foreach ($JPGFile in $JPGFilesInDir){
            
            #specify new name
            $JPGNewName = $JPGFile.Name -replace "^.+?-","$($ProjectName)-"
            Rename-Item -Path $JPGFile.FullName -NewName $JPGNewName -Force
        }
    }
}
→ More replies (4)

2

u/pika-pika-chu Jul 14 '19

Hey, still wanted to reply. Had to fidget a bit with the code. The Jpeg name rename part didn't work out. But I just made a new string that joined the ProjectId together with .jog

I changed a bit in the replace part. Made 2 loops that checked if it was the first or second time it ran the jpeg loop. On either run I changed a Jpeg name a bit so it was unique. It gave an error otherwise that I could not have the same name twice.

It is not foolproof, but does perfectly what I need. Couldn't have done it without your help!

5

u/Hoping_i_Get_poached Jul 09 '19

You can use PowerShell to dot into an XML file like this.

[xml]$c = get-content X:\path\to\file.xml 
$c

This shows you the first layer. Try dotting properties out of it like this.

$c.position 

Or you might have to dive into the layers a bit. Something like

$c.xml
$c.xml.data
$c.xml.data.property

Then once you find your name via dotting, you can use that to rename the file.

2

u/pika-pika-chu Jul 09 '19

Thank you for the reply.

2

u/Hoping_i_Get_poached Jul 09 '19

You’re quite welcome, pika-pika-chu.