Creating NFO style xml files with filenames included inside the file.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
sabelstrom
Posts: 18
Joined: 08 Oct 2016 07:42

Re: Creating NFO style xml files with filenames included inside the file.

#16 Post by sabelstrom » 09 Oct 2016 16:41

foxidrive wrote:
sabelstrom wrote:It does not create nfo files for those files that uses å Å ä Ä ö Ö é anymore.


Can you copy one of those files to a folder somewhere else, and then run the script for that new folder?

Make sure you open a cmd window and then start the batch script from that cmd window. Any error messages should remain visible and you can copy and paste the error messages here.



Absolutely nothing happens.
Attachments
Namnlös.jpg
Absolutely nothing happens when i ran the batch file with only one file in the folder containing a Ã…
Namnlös.jpg (41.23 KiB) Viewed 8483 times

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Creating NFO style xml files with filenames included inside the file.

#17 Post by aGerman » 09 Oct 2016 17:20

Saving the file with the BOM inside seems to be a little tricky.
Attached you'll find a ZIP archive that contains penpens script. I also included an exetended version of my script that reads the file properties, uses the title property (if any), and also calculates minutes for the runtime node out of the duration property.
create_nfo.zip
(1.73 KiB) Downloaded 304 times


Steffen

sabelstrom
Posts: 18
Joined: 08 Oct 2016 07:42

Re: Creating NFO style xml files with filenames included inside the file.

#18 Post by sabelstrom » 10 Oct 2016 05:17

Thanks.

The create_nfo_aGerman.bat create all nfofiles but uses something like "å Å ä Ä ö Ö é" in the title.

The create_nfo_penpen create all nfofiles that dont contain å Å ä Ä ö Ö é and then create a empty nfofile for the first move that contain å Å ä Ä ö Ö é in the filename.

And one more thing, i rather use *.* instead of *.avi *.mkv because i have other formats as well.

sabelstrom
Posts: 18
Joined: 08 Oct 2016 07:42

Re: Creating NFO style xml files with filenames included inside the file.

#19 Post by sabelstrom » 10 Oct 2016 06:12

Got this answer in another forum, is there somthing to do with that:

"The answer is code pages, my friend! See:
chcp /?
Useful list of code pages here
Try using
chcp 1252
Which is the Windows West European Latin 1 code page. If it doesn't work, check if any of the other ones work.
You can also try UTF-7 and 8 code pages.
chcp 65000
chcp 65001
"Microsoft recommends new applications use UTF-8 or UCS-2/UTF-16 instead of these code pages", as it says in the link I provided (though I've experienced occasions where UTF wouldn't work, but Windows code pages did work)
Don't worry if plan A fails, there are 25 more letters in the alphabet ;)"

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

Re: Creating NFO style xml files with filenames included inside the file.

#20 Post by Squashman » 10 Oct 2016 06:45

sabelstrom wrote:Got this answer in another forum, is there somthing to do with that:

"The answer is code pages, my friend! See:
chcp /?
Useful list of code pages here
Try using
chcp 1252
Which is the Windows West European Latin 1 code page. If it doesn't work, check if any of the other ones work.
You can also try UTF-7 and 8 code pages.
chcp 65000
chcp 65001
"Microsoft recommends new applications use UTF-8 or UCS-2/UTF-16 instead of these code pages", as it says in the link I provided (though I've experienced occasions where UTF wouldn't work, but Windows code pages did work)
Don't worry if plan A fails, there are 25 more letters in the alphabet ;)"

Have you bothered to read any of the code or any of the comments that have been so carefully thought out and given to you.

sabelstrom
Posts: 18
Joined: 08 Oct 2016 07:42

Re: Creating NFO style xml files with filenames included inside the file.

#21 Post by sabelstrom » 10 Oct 2016 09:12

Squashman wrote:
sabelstrom wrote:Got this answer in another forum, is there somthing to do with that:

"The answer is code pages, my friend! See:
chcp /?
Useful list of code pages here
Try using
chcp 1252
Which is the Windows West European Latin 1 code page. If it doesn't work, check if any of the other ones work.
You can also try UTF-7 and 8 code pages.
chcp 65000
chcp 65001
"Microsoft recommends new applications use UTF-8 or UCS-2/UTF-16 instead of these code pages", as it says in the link I provided (though I've experienced occasions where UTF wouldn't work, but Windows code pages did work)
Don't worry if plan A fails, there are 25 more letters in the alphabet ;)"

Have you bothered to read any of the code or any of the comments that have been so carefully thought out and given to you.


of course i have, but I do not understand most of it.

If the code have a chcp line i can try to change the numbers.

I edit all codes in notepad and i can olny chose unicode, UTF-8 and ANSI.

of course I can change the *.avi to *.* but not when I can't save the code with the right format.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Creating NFO style xml files with filenames included inside the file.

#22 Post by aGerman » 10 Oct 2016 11:01

I use a different text editor (PSPad) to edit the file. Notepad++ ist more common and will probably be able to do the same.
create_nfo_penpen.zip
(778 Bytes) Downloaded 287 times


You do not need to change the code page additionally because that's already included in the code.

Things like "å Å ä Ä ö Ö é" are the single byte expressions of the related UTF-8 characters. They are expected to show up in Notepad. If you use another editor (like PSPad) they show up as the UTF-8 characters. The file content is the same in both cases. While Notepad needs the BOM (Byte Order Mark) at the beginning of the file to display the right characters, PSPad automatically detects that the content is UTF-8 encoded.

XML is neither required to have a BOM nor is it even expected. The character set is declared in the "encoding" property. Even if this declaration is left out it would automatically default to UTF-8.

Steffen

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Creating NFO style xml files with filenames included inside the file.

#23 Post by penpen » 10 Oct 2016 14:29

sabelstrom wrote:Can not save it as ANSI, it tells me the file uses symbols in Unicode-format and all those symbols will be lost if saving as ANSI. Didnt work when i saved any way, tried to save as unicode and UTF-8, didnt work when i ran the file.
You can save this file in ANSI using windows notepad:
Just Select "OK." and ignore that message.
(It is a liitle bit tricky, if you open the file with bom under Windows 10, because it autoconverts this file to utf-8 (well it does on my pc), so you have to add it every time you change the file).

But, it seems the bom is not shown in my post anyway (using Internet Explorer 11.187...), so:
Thanks to aGerman for the zipfile.
Edit: But the bom is stored using utf-8, so you have to open it in windows default notepad, and select "Save as", change the encoding to ANSI, and select "OK." on the "encoding warning dialogue".

This should happen:

Code: Select all

Z:\>chcp
Aktive Codepage: 850.

Z:\>create_nfo_penpen.bat
process: "create_nfo_penpen" "C:\Users\Kidutus\Downloads\create_nfo_penpen\create_nfo_penpen"
process: "Neues å Å ä Ä ö Ö é Textdokument.txt" "C:\Users\Kidutus\Downloads\create_nfo_penpen\Neues å Å ä Ä ö Ö é Textdokument.txt"
Drücken Sie eine beliebige Taste . . .
If your initial Codepage - 850 on my pc - difffers, you may change the Environment variable "cp" to "850" (instead of the default value of "%restoreCp%");
although that should be no issue because internally the filenames are stored using UCS2 (a superset of UTF-16); maybe except if you are using a file system other than ntfs:
For example fat32, ext2, ... .


penpen

sabelstrom
Posts: 18
Joined: 08 Oct 2016 07:42

Re: Creating NFO style xml files with filenames included inside the file.

#24 Post by sabelstrom » 10 Oct 2016 15:54

Naild it :-)

Did some google on the chcp, and i turned out i needed to use chcp 1252. Used Notpad++ to change in the file, UTF-8 (no BOM) format.

Thank you very much every one for your help :-)

Now i feel dumb when asking more of u, but is it possible to get the playtime in the nfo to?

The final code i used to get it to do what i want is this:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
if /I "%~1" == "ansi" goto :main
cmd /A /e:ON /v:OFF /c ^""%~fs0" "ansi"^"
goto :eof

:main
for /F "tokens=2 delims=:." %%a in ('chcp') do set "restoreCp=%%a"
set "restoreCp=%restoreCp: =%"

:: maybe you have to toggle between your systems codepage and UTF-8 and not the actual ANSI codepage
:: you have to try it out
set "cp=%restoreCp%"
>nul chcp %cp%

for /r %%a in (*.mkv *.avi *.mp4 *.mpg *.m2ts *.m4v) do (
   set "filename=%%~na"
   set "filepath=%%~dpna"
   call :createFile  filename filepath
)


:: restore initial codepage
>nul chcp %restoreCp%
pause
goto :eof

:: this function may not work on all windows versions (for example XP)
:createFile
:: %~1   filename to set
:: %~2   full path of the filename
setlocal disableDelayedExpansion
>nul chcp 850
<nul >"%filepath%.nfo" set /P "="
>nul chcp 1252
call set "filename=%%%~1%%"
call set "filepath=%%%~2%%"
set "filename=%filename:&=^&amp;%"
set "filename=%filename:'=^&apos;%"

echo process: "%filename%" "%filepath%"

(
echo ^<?xml version="1.0" encoding="UTF-8" standalone="yes"?^>
echo ^<movie^>
echo ^<title^>%filename%^</title^>
echo ^<runtime^>^</runtime^>
echo ^</movie^>
) >>"%filepath%.nfo"

>nul chcp %cp%
:: restored working codepage
endlocal
goto :eof

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Creating NFO style xml files with filenames included inside the file.

#25 Post by aGerman » 10 Oct 2016 15:58

My code should already do that.
I could add the status output like in penpen's code but it will still not prepend the BOM (for the reasons I already mentioned above).

Code: Select all

@if (@a)==(@b) @end /*

@echo off &setlocal
for /r %%i in (*.mkv *.avi *.wmv) do (
  cscript //nologo //e:jscript "%~fs0" "%%~fi"
)
pause
exit /b

*/

var objXmlDoc = new ActiveXObject('Microsoft.XMLDOM'),
    objXsltDoc = new ActiveXObject('Microsoft.XMLDOM'),
    objShell = new ActiveXObject('Shell.Application'),
    objFSO = new ActiveXObject('Scripting.FileSystemObject'),
    objADOS = new ActiveXObject('ADODB.Stream');
objADOS.Type = 2; // adTypeText
var strFullName = WScript.Arguments(0),
    strPath = objFSO.GetParentFolderName(strFullName),
    strBaseName = objFSO.GetBaseName(strFullName),
    objItem = objShell.Namespace(strPath).ParseName(objFSO.GetFileName(strFullName)),
    strTitle = getProperty(objItem, 'DocTitle'),
    strRuntime = (Math.round(Number(getProperty(objItem, 'Duration')) / 600000000)).toString(),
    strS25 = '                         ',
    objNode;

WScript.StdOut.Write((strBaseName + strS25).substr(0, 25) + '  ' + (strTitle + strS25).substr(0, 25) + '  ' + ('    ' + strRuntime).slice(-4) + ' min.\n');
objXmlDoc.loadXML('<?xml version="1.0" encoding="UTF-16"?><movie><title /><runtime /></movie>');
objXsltDoc.loadXML(
  '<?xml version="1.0"?>\n' +
  '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">\n' +
  ' <xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes" />\n' +
  ' <xsl:template match="node()|@*">\n' +
  '  <xsl:copy>\n' +
  '   <xsl:apply-templates select="node()|@*" />\n' +
  '  </xsl:copy>\n' +
  ' </xsl:template>\n' +
  '</xsl:stylesheet>');
objXmlDoc.transformNodeToObject(objXsltDoc, objXmlDoc);
objNode = objXmlDoc.documentElement.selectSingleNode('title');
if (strTitle == '') {
  objNode.text = strBaseName;
} else {
  objNode.text = strTitle;
}
objNode = objXmlDoc.documentElement.selectSingleNode('runtime');
objNode.text = strRuntime;
objXmlDoc.save(objFSO.BuildPath(strPath, strBaseName + '.nfo'));

function getProperty(objShFI, strPropName) {
  objADOS.Open();
  objADOS.WriteText(objShFI.ExtendedProperty(strPropName));
  objADOS.Position = 0;
  var value = objADOS.ReadText();
  objADOS.Close();
  return value;
}


Steffen

sabelstrom
Posts: 18
Joined: 08 Oct 2016 07:42

Re: Creating NFO style xml files with filenames included inside the file.

#26 Post by sabelstrom » 10 Oct 2016 16:28

aGerman wrote:My code should already do that.
I could add the status output like in penpen's code but it will still not prepend the BOM (for the reasons I already mentioned above).

Code: Select all

@if (@a)==(@b) @end /*

@echo off &setlocal
for /r %%i in (*.mkv *.avi *.wmv) do (
  cscript //nologo //e:jscript "%~fs0" "%%~fi"
)
pause
exit /b

*/

var objXmlDoc = new ActiveXObject('Microsoft.XMLDOM'),
    objXsltDoc = new ActiveXObject('Microsoft.XMLDOM'),
    objShell = new ActiveXObject('Shell.Application'),
    objFSO = new ActiveXObject('Scripting.FileSystemObject'),
    objADOS = new ActiveXObject('ADODB.Stream');
objADOS.Type = 2; // adTypeText
var strFullName = WScript.Arguments(0),
    strPath = objFSO.GetParentFolderName(strFullName),
    strBaseName = objFSO.GetBaseName(strFullName),
    objItem = objShell.Namespace(strPath).ParseName(objFSO.GetFileName(strFullName)),
    strTitle = getProperty(objItem, 'DocTitle'),
    strRuntime = (Math.round(Number(getProperty(objItem, 'Duration')) / 600000000)).toString(),
    strS25 = '                         ',
    objNode;

WScript.StdOut.Write((strBaseName + strS25).substr(0, 25) + '  ' + (strTitle + strS25).substr(0, 25) + '  ' + ('    ' + strRuntime).slice(-4) + ' min.\n');
objXmlDoc.loadXML('<?xml version="1.0" encoding="UTF-16"?><movie><title /><runtime /></movie>');
objXsltDoc.loadXML(
  '<?xml version="1.0"?>\n' +
  '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">\n' +
  ' <xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes" />\n' +
  ' <xsl:template match="node()|@*">\n' +
  '  <xsl:copy>\n' +
  '   <xsl:apply-templates select="node()|@*" />\n' +
  '  </xsl:copy>\n' +
  ' </xsl:template>\n' +
  '</xsl:stylesheet>');
objXmlDoc.transformNodeToObject(objXsltDoc, objXmlDoc);
objNode = objXmlDoc.documentElement.selectSingleNode('title');
if (strTitle == '') {
  objNode.text = strBaseName;
} else {
  objNode.text = strTitle;
}
objNode = objXmlDoc.documentElement.selectSingleNode('runtime');
objNode.text = strRuntime;
objXmlDoc.save(objFSO.BuildPath(strPath, strBaseName + '.nfo'));

function getProperty(objShFI, strPropName) {
  objADOS.Open();
  objADOS.WriteText(objShFI.ExtendedProperty(strPropName));
  objADOS.Position = 0;
  var value = objADOS.ReadText();
  objADOS.Close();
  return value;
}


Steffen


I missed that, sorry. It works, but i think it is the wrong chcp, still getting the wrong symbols and i do not find a line with chcp in your code, i can add it on my own, but im not sure where in the code i would put it.

I do find your coding more complex, i can not just copy and paste. I can not read out much of it yet.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Creating NFO style xml files with filenames included inside the file.

#27 Post by aGerman » 10 Oct 2016 16:36

You didn't read my comments about XML content, UTF-8 and BOM.
XML is neither required to have a BOM nor is it even expected. The character set is declared in the "encoding" property. Even if this declaration is left out it would automatically default to UTF-8.


The file contains the right characters.
Things like "å Å ä Ä ö Ö é" are the single byte expressions of the related UTF-8 characters. They are expected to show up in Notepad. If you use another editor (like PSPad) they show up as the UTF-8 characters. The file content is the same in both cases. While Notepad needs the BOM (Byte Order Mark) at the beginning of the file to display the right characters, PSPad automatically detects that the content is UTF-8 encoded.


There is no CHCP because the JScript part of the code converts the characters.

Steffen

sabelstrom
Posts: 18
Joined: 08 Oct 2016 07:42

Re: Creating NFO style xml files with filenames included inside the file.

#28 Post by sabelstrom » 12 Oct 2016 15:57

Now i get it all :-)

I had to change the interface langue in KODI to Swedish for it to use the right symbols.

But the java script dose not use the filename as title if the movie already have a title in it.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Creating NFO style xml files with filenames included inside the file.

#29 Post by aGerman » 12 Oct 2016 16:36

sabelstrom wrote:But the java script dose not use the filename as title if the movie already have a title in it.
correct
aGerman wrote:I also included an exetended version of my script that reads the file properties, uses the title property (if any)...
I actally thought it was a feature rather than a failure. If you don't want it replace

Code: Select all

if (strTitle == '') {
  objNode.text = strBaseName;
} else {
  objNode.text = strTitle;
}
with

Code: Select all

objNode.text = strBaseName;
only.

Steffen

sabelstrom
Posts: 18
Joined: 08 Oct 2016 07:42

Re: Creating NFO style xml files with filenames included inside the file.

#30 Post by sabelstrom » 12 Oct 2016 16:45

Thank you. :-)

The title can be in any language and contain more than the title.

Your thoughts is nice, but I put some effort in to rename all movies and episode to Swedish.

Post Reply