多个文本文件 - 需要提取某些信息

时间:2015-12-04 17:45:23

标签: batch-file text extract

系统:Win7

大家好,

我有大约60,000个htm文件从text_1.htm到text_60000.htm。这些文件中的每一个都包含一行,表示可以通过搜索以下唯一定位的标题:

title="*"></a></div>

有很多例子

title="*"

在每个文件中,因此单独搜索该表达式是不可能的。

我想将文件重命名为相应的标题。我用来重命名文件的程序可以使用.txt文件批量重命名文件,该文件包含原始文件名和用分号分隔的新文件名,如下所示:

text_1.htm:TitleA.htm

text_2.htm:TitleB.htm

...

text_60000.htm:TitleABCDE.htm

-

如果你们中的一个人能够提供解决方案以便我获得这个.txt文件我需要重命名我的文件,我们将非常感激。

我期待着您的回复。

1 个答案:

答案 0 :(得分:0)

StackOverflow不是免费的编码服务。通常我们不会根据请求编写脚本。我们只帮助您修复自己脚本的问题,并提供改进代码的提示和技巧。

尽管如此,我发现你的挑战很有趣,并决定接受。在这里,使用.bat扩展名保存它,看看它是否符合您的要求。

更简单的方法:

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

@echo off
setlocal

for %%I in (*.htm) do for /f "delims=" %%A in (
    'cscript /nologo /e:JScript "%~f0" ^< "%%~fI"'
) do echo ren "%%~fI" "%%~A%%~xI"
rem // Remove "echo" above when satisfied that the files
rem // will be renamed correctly.

goto :EOF

@end // end Batch / begin JScript hybrid chimera

var DOM = WSH.CreateObject('htmlfile');
DOM.write(WSH.StdIn.ReadAll());

for (var el = DOM.getElementsByTagName('*'), i = el.length; --i;)
    if (el[i].title)
        if (el[i].parentNode.nodeName == 'A')
            if (el[i].parentNode.parentNode.nodeName == 'DIV') break;

DOM.close(WSH.Echo(el[i].title));

编辑:由于你有60,000个文件需要重命名,所以最好让Wscript FSO对象枚举文件,只启动cscript一次(而不是每个文件一次)。

更快的方法:

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

@echo off & setlocal
cscript /nologo /e:JScript "%~f0"
goto :EOF

@end // end Batch / begin JScript hybrid chimera

var DOM = WSH.CreateObject('htmlfile'),
    fso = WSH.CreateObject('Scripting.FileSystemObject'),
    dir = fso.GetParentFolderName(WSH.ScriptFullName);

function getTitle() {
    for (var el = DOM.getElementsByTagName('*'), i = el.length; --i;)
        if (el[i].title)
            if (el[i].parentNode.nodeName == 'A')
                if (el[i].parentNode.parentNode.nodeName == 'DIV') break;
    return el[i].title;
}

for (var col = new Enumerator(fso.GetFolder(dir).files); !col.atEnd(); col.moveNext()) {
    var from = col.item();
    if (/\.html?$/i.test(from)) {
        var htmlfile = fso.OpenTextFile(from, 1);
        DOM.write(htmlfile.ReadAll());
        htmlfile.Close();
        var to = dir + '\\' + getTitle() + '.' + fso.GetExtensionName(from);
        WSH.Echo(from + ' -> ' + to);
        // fso.MoveFile(from, to);
        // Uncomment the previous line when you're ready to rename.
    }
}

DOM.close();
相关问题