Page 1 of 2

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

Posted: 08 Oct 2016 08:13
by sabelstrom
Mod edit: This topic became complicated due to foreign character sets being using in the filenames.

The text editors that are used when saving the batch file are also factors.


Hi.

I need a CMD/Batch/Script command to create a txt or nfo file with this text:

Code: Select all

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<movie>
<title></title>
<runtime></runtime>
</movie>


And the filename of the file should be the same as every file or folder in the path.

In the middle of <title></title> the filename should be put.

Exampel:

I have a file or folder (take the one that easiest) Aristocats.mkv.
I want to have a file that has the name Aristocats.nfo or Aristocats.txt.
In the file i want this:

Code: Select all

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<movie>
<title>Aristocats</title>
<runtime></runtime>
</movie>


I have over 1000 folders or files that need this nfo file, is it possible?

I found this:
for %i in (*) do md "%~ni" && move "%~i" "%~ni"
It creates a folder for each file and moves it in to it.

I have googled for hours but i dont find a software or a command that do this.

Is it even possible or should i give up?

Re: CMD/Batch/Script create text file with specific filname in the text?

Posted: 08 Oct 2016 10:17
by foxidrive
Test this on some test folders - it seems ok here in a brief test.
It is creating plain text files of course.

Code: Select all

@echo off
for /r %%a in (*.mkv *.avi) do (
(
echo ^<?xml version="1.0" encoding="UTF-8" standalone="yes"?^>
echo ^<movie^>
for %%b in ("%%~na") do echo ^<title^>%%~b^</title^>
echo ^<runtime^>^</runtime^>
echo ^</movie^>
)>"%%~dpna.nfo"
)

Re: CMD/Batch/Script create text file with specific filname in the text?

Posted: 08 Oct 2016 13:13
by sabelstrom
Runs nice :-) thank you.

Is it possible for it to use Swedish? Many names contains Å Ä Ö letters and som symbols like é in pokémon, the file name is right but the title uses „ instead of ä, space instead of Å, ‚ instead of é, ” instead of ö, and so on.

Re: CMD/Batch/Script create text file with specific filname in the text?

Posted: 08 Oct 2016 13:33
by aGerman
You can't use pure batch for this task. The letters you mentioned have to be encoded in UTF-8 and other ASCII characters are invalid in XML text (e.g. "&" must have been replaced with "&amp;").
Either use a language that supports the usage of DOM or at least use a batch hybrid like that:

Code: Select all

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

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

*/

var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM'), oXsltDoc = new ActiveXObject('Microsoft.XMLDOM'), oNode;
oXmlDoc.loadXML('<movie><title /><runtime /></movie>');
oXsltDoc.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>');
oXmlDoc.transformNodeToObject(oXsltDoc, oXmlDoc);
oNode = oXmlDoc.documentElement.selectSingleNode('title');
oNode.text = WScript.Arguments(0);
oNode = oXmlDoc.documentElement.selectSingleNode('runtime');
oNode.text = '';
oXmlDoc.save(WScript.Arguments(1) + WScript.Arguments(0) + '.nfo');

Steffen

Re: CMD/Batch/Script create text file with specific filname in the text?

Posted: 08 Oct 2016 14:53
by penpen
In common you are right aGerman, but in this case (if the above is the full task then) i think this should be possible to do that in batch.

You could ensure to use "cmd/A" (to not generate unwanted NUL characters).
Then you may change the codepage to 65001 (~=UTF_8).
Then there are only 5 problematic characters, that needed to be replaced by ist escape sequence:

Code: Select all

"   &#34;   &quot;
&   &#38;   &amp;
'   &#39;   &apos;
<   &#60;   &lt;
>   &#62;   &gt;
(3 of them are forbidden characters in file/path names: '<', '>', and '"'.)
Should be easy to replace the others.

Extension of foxidrive's code (Tested on Win 10):

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) do (
   set "filename=%%~na"
   set "filepath=%%~dpna"
   call :createFile  filename filepath
)


:: restore initial codepage
>nul chcp %restoreCp%
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 65001
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


penpen

Edit: Removed the "percentage bug", and replaced the hardcoded name of the batch file: Thanks @aGerman.
Edit: Another bugfix (replaced "%%~fa" with "%%~dpna"): Another "Thanks" to aGerman.

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

Posted: 08 Oct 2016 16:12
by aGerman
Yes in this case it should work. I'd rather use

Code: Select all

cmd /A /e:ON /v:OFF /c "%~fs0 "ansi""

instead of a hardcoded name. And percent signs in the passed arguments would cause problems if you CALL the subroutine.

Steffen

Re: CMD/Batch/Script create text file with specific filname in the text?

Posted: 08 Oct 2016 16:29
by sabelstrom
penpen wrote:In common you are right aGerman, but in this case (if the above is the full task then) i think this may be possible to do that in batch.

You could ensure to use "cmd/A" (to not generate unwanted NUL characters).
Then you may change the codepage to 65001 (~=UTF_8).
Then there are only 5 problematic characters, that needed to be replaced by ist escape sequence:

Code: Select all

"   &#34;   &quot;
&   &#38;   &amp;
'   &#39;   &apos;
<   &#60;   &lt;
>   &#62;   &gt;
(3 of them are forbidden characters in file/path names: '<', '>', and '"'.)
Should be easy to replace the others.

Untested:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
if /I "%~1" == "ansi" goto :main
cmd /A /e:ON /v:OFF /c "test.bat "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 actuial ANSI codepage
:: you have to try it out
set "cp=%restoreCp%"
>nul chcp %cp%

for /r %%a in (*.mkv *.avi) do call :createFile "%%~na" "%%~fa"


:: restore initial codepage
>nul chcp %restoreCp%
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

>nul chcp 65001
set "filename=%~1"
set "filename=%filename:&=^&amp;%"
set "filename=%filename:'=^&apos;%"
set "file=%~2"

echo process: "%filename%" "%~2"

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

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


penpen


I do not understand all of it, but i will give it a try, never used scripts befor.

First i made a shortcut for CMD and added the /A "C:\Windows\System32\cmd.exe /A"

Than i made all double %% to single % so it can run on CMD.

I get my errors in Swedish but i will try to translate them.

First i get that test.bat is not a command, program or file.

Then after the line "for /r %%a in (*.mkv *.avi) do call :createFile "%%~na" "%%~fa"" i get this:
"Invalid attempt to call batch label outside of batch script"

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

Posted: 08 Oct 2016 17:05
by aGerman
Copy the code into a batch file (that is, a text file with extension .bat) in order to run it properly.
  • open Notepad (Press [Windows]+[R], type notepad, and click on OK)
  • paste the code into the editor window
  • menu "File" -> "Save as ..."
  • navigate to the folder you want to save the script
  • select "All Files (*.*)" in the File Type box
  • enter test.bat in the File Name box
  • make sure the encoding is set to "ANSI" and click on Save

Steffen

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

Posted: 09 Oct 2016 02:27
by penpen
I've updated the above code, removing the bugs aGerman has found (thanks :D ):
Hope it now works without errors.


penpen

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

Posted: 09 Oct 2016 04:48
by sabelstrom
aGerman wrote:Copy the code into a batch file (that is, a text file with extension .bat) in order to run it properly.
  • open Notepad (Press [Windows]+[R], type notepad, and click on OK)
  • paste the code into the editor window
  • menu "File" -> "Save as ..."
  • navigate to the folder you want to save the script
  • select "All Files (*.*)" in the File Type box
  • enter test.bat in the File Name box
  • make sure the encoding is set to "ANSI" and click on Save

Steffen


Now i feel stupid :oops:

Thank you, the batch file works.

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

Posted: 09 Oct 2016 04:55
by sabelstrom
penpen wrote:I've updated the above code, removing the bugs aGerman has found (thanks :D ):
Hope it now works without errors.


penpen



Tested it, now the nfo files have the file extension to, aristocats.avi.nfo it it was right the first time when the name was aristocats.nfo

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

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

Posted: 09 Oct 2016 06:10
by aGerman
sabelstrom wrote:Now i feel stupid :oops:
Don't worry. I didn't want to make you feel stupid. Just wanted to enable you to run the script :wink:


sabelstrom wrote:it was right the first time when the name was aristocats.nfo
Replace the 18th line in penpen's code with

Code: Select all

   set "filepath=%%~dpna"


sabelstrom wrote:It dose not create nfo files for those files that uses å Å ä Ä ö Ö é anymore.
Hmm. I can't reproduce that failure. Works for me :?
If you want you could run this code and post the result.
http://www.dostips.com/forum/viewtopic.php?f=3&t=7420&p=49133#p49133
Maybe we are able to figure out why it doesn't work for you.

Steffen

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

Posted: 09 Oct 2016 07:11
by penpen
sabelstrom wrote:It dose not create nfo files for those files that uses å Å ä Ä ö Ö é anymore.
If the above batch replaces "å Å ä Ä ö Ö é" by something like "Ã¥ Ã… ä Ä ö Ö é", then this is intended:
It is the UTF-8 encoding.
Some text editors may have problems to interpret an UTF-8 text file if it has no bom (byte order mark).
Some xml applications have problems with the bom, so it is recommended to not use them.
If you want your files to use the UTF-8 bom, then just add "" as the first output; so the ":createFile" function should look like this:

Code: Select all

:: 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
chcp 850
<nul >"%filepath%.nfo" set /P "="
>nul chcp 65001
chcp
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
Note: Check if ANSI is chosen (some text editors automatically try to save such a file as UTF-8), when saving this file.


penpen

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

Posted: 09 Oct 2016 10:11
by foxidrive
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.

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

Posted: 09 Oct 2016 16:34
by sabelstrom
penpen wrote:
sabelstrom wrote:It dose not create nfo files for those files that uses å Å ä Ä ö Ö é anymore.
If the above batch replaces "å Å ä Ä ö Ö é" by something like "Ã¥ Ã… ä Ä ö Ö é", then this is intended:
It is the UTF-8 encoding.
Some text editors may have problems to interpret an UTF-8 text file if it has no bom (byte order mark).
Some xml applications have problems with the bom, so it is recommended to not use them.
If you want your files to use the UTF-8 bom, then just add "" as the first output; so the ":createFile" function should look like this:

Code: Select all

:: 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
chcp 850
<nul >"%filepath%.nfo" set /P "="
>nul chcp 65001
chcp
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
Note: Check if ANSI is chosen (some text editors automatically try to save such a file as UTF-8), when saving this file.


penpen



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.

When i ran your earlier batch i get no errors, the files with ÅÄÖé never shows up in the text in CMD.