Home > Net >  Kodi batch file for renaming and copying my NAS images after scraping
Kodi batch file for renaming and copying my NAS images after scraping

Time:11-24

I wrote a batch script to help assist in images and posters that Kodi uses in parallel with a program called Media companion. I am having issues in one particular area after an unexpected condition showed up during the development.

My script is used for TV Shows which are broken out as ROOT > Series > Seasons 1 - XX. Within the season folders is a filed typically called folder.jpg. My script takes a copy of that, and moves it up a level and renames it as Seasonxx-thumb and poster for Kodi to use the posters that I specify.

This worked great prior to me scraping the folders with the Media Companion tool...before the Media companion tools I had within each season folder all the MP4 files for that season....and a single folder.jpg. My script worked fine. Now that I've scraped the info properly, I have all the MP4 files and now corresponding NFO and thumbnail files for each episode.....which is what it is supposed to do....but now causes havoc with my script as there are now more than 1 jpg file present....

The nice thing is that each .jpg ends in XXXXXXXXX-thumb.jpg. So I need my script to rename the only remaining file that would be there with any RANDOM filename which will be shown in my screenshot below. The random filename is most likely from the filename it has from sites like IMDB or themoviedb which would previously be the only jpg and renamed folder.jpg automatically keeping everything as I'd expect. The reason I do this is that I have a certain file size (1440x960) for the poster rendering across plex, serviio, and Kodi...I have found that they are all various sizes...so this script deals with the replication.

So what I need is the batch to do is ignore all files that end in *-thumb.jpg and only renaming the one remaining random file name to folder.jpg accordingly. The sizing is something I handle manually, it's more of getting the folder.jpg in place and letting the rest of the script run accordingly.

Here is a code snippet I am using to rename the previously solo .jpg file:

echo.
if exist folder.jpg (
  echo File rename not needed
) else (
  ren *.jpg folder.jpg
  echo ~~~~ WARNING: File renamed as folder.jpg in Season %SeasonCounter% folder. ~~~~
)

echo.
echo ~~~~~~~~~~~~~ Copy File Operation ~~~~~~~~~~~~~
echo.
echo Copying "folder.jpg & Creating Files Season Poster & Thumbnail:
echo.
echo File copy source: "folder.jpg >> "%CurrDirName%-thumb.jpg"
copy "folder.jpg" "%CurrDirName%-thumb.jpg"

Here is what the folder structure would typically look like:

Folder Structure Kodi

Not entirely sure how to handle this as the batch script runs one line at time and I can't use compound IF statements (from what I've seen in some examples when searching Google and here). Anyone have any thoughts on how I can resolve this?

Thanks

Update 1:

With the assist of Anon Coward below, I folded his answer into my code with the additional check of if the folder.jpg exists to not do anything, otherwise execute:

echo.
if exist folder.jpg (
  echo File rename not needed
) else (
  setlocal enabledelayedexpansion
for %%a in (*.jpg) do (
    set "fn=%%a"
    if "!fn:~-10!" neq "-thumb.jpg"  (
        echo Using "!fn!"
        ren "!fn!" folder.jpg
        goto :done_with_rename
    )
)
:done_with_rename
    echo ~~~~ WARNING: File renamed as folder.jpg in Season %SeasonCounter% folder. ~~~~
    echo all done
)

echo.

CodePudding user response:

If I understand you correctly, you want to rename the first jpg file you find that doesn't end in "-thumb.jpg". If that's the case, then you can look at all of the possible .jpg files, and ignore any that end in "-thumb.jpg", renaming whatever's left:

setlocal enabledelayedexpansion
for %%a in (*.jpg) do (
    set "fn=%%a"
    if "!fn:~-10!" neq "-thumb.jpg"  (
        echo Using "!fn!"
        ren "!fn!" folder.jpg
        goto :done_with_rename
    )
)
:done_with_rename
echo all done
  • Related