Solved CMD/Batch/Script create text file with specific filname in.

October 8, 2016 at 07:11:31
Specs: Windows 7
Hi.

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

<?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:
<?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?


See More: CMD/Batch/Script create text file with specific filname in.

Reply ↓  Report •


#1
October 9, 2016 at 15:49:36
Well i found something on the way so far. A CMD command that do what i want, but it dose not use Ää Åå Öö Éé in the titles in the nfo file.

So little more time and maybe it is some one out there that can solve the last issue.

Here is the working CMD command:

@echo off
for /r %a in (*.*) 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"
)


Reply ↓  Report •

#2
October 9, 2016 at 16:50:25
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. Place the command as early in the script as you can. If it doesn't work, check if any of the other code pages 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 ;)

message edited by RainBawZ


Reply ↓  Report •

#3
October 10, 2016 at 15:01:55
✔ Best Answer
Solved :-)

This is the batch code that worked for me to get Swedish to work. Saved as as a .bat file using Notpad++, and the UTF-8 (no BOM) format.

@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:&=^&%"
set "filename=%filename:'=^'%"

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


Reply ↓  Report •

Related Solutions

#4
October 12, 2016 at 16:06:54
And here is a java script that can be paste in a .bat file.

If you got all movies in one folder you could run this and it will create a nfo that uses the the filename as title, and add duration to the nfo. And it will create a folders with the filenames names and move the movies and the nfofiles to those folders.

[code]@if (@a)==(@b) @end /*

@echo off &setlocal
for /r %%i in (*.mkv *.avi *.mp4 *.mpg *.m2ts *.m4v) do (
cscript //nologo //e:jscript "%~fs0" "%%~fi"
)
for %%i in (*.mkv *.avi *.mp4 *.mpg *.m2ts *.m4v) do md "%%~ni" && move "%%~i" "%%~ni"
for %%i in (*.nfo) do move "%%~i" "%%~ni"
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');

objNode.text = strBaseName;

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;
}[/code]


Reply ↓  Report •


Ask Question