Help with a batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ArieS
Posts: 8
Joined: 25 Oct 2012 11:58

Help with a batch file

#1 Post by ArieS » 17 Feb 2016 03:58

Hello all, you have helped me in the past and more help would be greatly appreciated yet again :D

I have my movie collection on a NAS that I use with Kodi. Each movie is in a seperate folder and it contains an .nfo file named like the movie.
I would like the final result to be:
<rating>0</rating>
<votes>0</votes>

But right now it's something like this (as an example):

<rating>7.400000</rating>
<votes>12</votes>

The problem is, they obviously all have different ratings and votes, currently.

I have over 1500 movies so I really don't see myself editing each .nfo one by one :?

Let me know if I didn't provide enough info...
Thanks in advance!

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Help with a batch file

#2 Post by ShadowThief » 17 Feb 2016 04:14

The easiest thing to do would just be recreate the .nfo files from scratch.

Code: Select all

@echo off

for /f "delims=" %%A in ('dir /s /b *.nfo') do (
   (
      echo ^<rating^>0^</rating^>
      echo ^<votes^>0^</votes^>
   )>%%A
)


And just save this in the directory where all your movie folders are.

ArieS
Posts: 8
Joined: 25 Oct 2012 11:58

Re: Help with a batch file

#3 Post by ArieS » 17 Feb 2016 13:56

ShadowThief, the problem is rating and votes are not the only infos in the nfo.
Here's an example:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<movie>
<title>Die Hard</title>
<originaltitle>Die Hard</originaltitle>
<sorttitle>Die Hard 1</sorttitle>
<rating>8.400000</rating>
<epbookmark>0.000000</epbookmark>
<year>1988</year>
<top250>0</top250>
<votes>77</votes>
<outline></outline>
<plot>NYPD cop John McClane&apos;s plan to reconcile with his estranged wife, Holly, is thrown for a serious loop when minutes after he arrives at her office, the entire building is overtaken by a group of pitiless terrorists. With little help from the LAPD, wisecracking McClane sets out to single-handedly rescue the hostages and bring the bad guys down.</plot>
<tagline>Twelve terrorists. One cop. The odds are against John McClane... That&apos;s just the way he likes it.</tagline>
<runtime>132</runtime>
<mpaa>Rated R</mpaa>
<playcount>1</playcount>
<lastplayed>2012-08-24</lastplayed>
<id>tt0095016</id>
<genre>Action</genre>
<genre>Thriller</genre>
<genre>HD</genre>
<country>United States of America</country>

So I really need only the lines rating and votes to be modified and the rest to be left intact.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with a batch file

#4 Post by foxidrive » 17 Feb 2016 14:12

This uses a native Windows batch script called Jrepl.bat written by dbenham, which uses jscript to make it robust and swift.
viewtopic.php?f=3&t=6044

Place Jrepl.bat in the same folder as the batch file, or in a folder that is on the system path.

Code: Select all

@echo off
for /f "delims=" %%A in ('dir /s /b *.nfo') do (
   call jrepl "<rating>.*</rating>" "<rating>0</rating>"  /f "%%A" /o -
   call jrepl "<votes>.*</votes>"   "<votes>0</votes>"   /f "%%A" /o -
)
pause

ArieS
Posts: 8
Joined: 25 Oct 2012 11:58

Re: Help with a batch file

#5 Post by ArieS » 17 Feb 2016 14:53

Wow, you guys are something else... :mrgreen:

foxidrive, it worked.
Thank you both very much for your time and help!

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Help with a batch file

#6 Post by Aacini » 17 Feb 2016 18:08

I would like to complete the information you have about the possible solutions for this problem.

In this site there is another program similar to Jrepl.bat that allows to replace strings inside text files; it is called FindRepl.bat and you may download it from this link. These programs are extensive applications with advanced capabilities that can perform this replacement and many more, much more complex ones.

However, the use of anyone of these programs to perform a task as simple as just replace a couple strings from several small files is certainly a waste of resources that may be achieved in much simpler ways. You don't need hundreds of lines of code to do the same thing, just a small Batch-JScript hybrid script specifically written to solve this problem that would be as robust and fast as anyone of Jrepl.bat or FindRepl.bat.

As a matter of fact, the code below should run faster than the proposed Jrepl.bat solution because the program is much smaller and each data file is processed just one time, that is, the two replacements are completed in the same processing pass instead of process each file one time for each replacement.

Code: Select all

@if (@CodeSection == @Batch) @then

@echo off
for /F "delims=" %%a in ('dir /S /B *.nfo') do (
   cscript //nologo //E:JScript "%~F0" < "%%a" > "%%a.new"
   move /Y "%%a.new" "%%a"
)
goto :EOF

@end

WScript.Stdout.Write(WScript.Stdin.ReadAll().replace(/<rating>.*<\/rating>|<votes>.*<\/votes>/g,
   function (A){return {rating:"<rating>0</rating>", votes:"<votes>0</votes>"}[A.slice(1,A.indexOf(">"))]}));
   

As an added bonus, this code is simple enough so you are encouraged to review it, modify it and use it for "what if" testing purposes, something that certainly you can't do with Jrepl.bat or FindRepl.bat original code!

Antonio

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Help with a batch file

#7 Post by ShadowThief » 17 Feb 2016 20:00

ArieS wrote:ShadowThief, the problem is rating and votes are not the only infos in the nfo.

ArieS wrote:I would like the final result to be:
<rating>0</rating>
<votes>0</votes>


I just gave you what you asked for.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Help with a batch file

#8 Post by Squashman » 17 Feb 2016 20:49

ArieS wrote:ShadowThief, the problem is rating and votes are not the only infos in the nfo.

You did not feel that information was pertinent when you posted your question?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with a batch file

#9 Post by foxidrive » 17 Feb 2016 20:49

ShadowThief wrote:
ArieS wrote:I would like the final result to be:
<rating>0</rating>
<votes>0</votes>


I just gave you what you asked for.


hehe I was going to rant about that again - people ask for a solution for something they aren't doing. Inaccurate descriptions.
It's crazy! ;)

Post Reply