重命名基于文档文本字符串多个文件 [英] Renaming Multiple Files Based on Document Text String

查看:366
本文介绍了重命名基于文档文本字符串多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到有东西在那里叫批量重命名工具,但数据文件,我想改名是相对保密的话,或许不合理,我持谨慎态度使用它。同时,我不能肯定它会允许我寻求的方式,当涉及到重命名文件的特定字符串我想它。因此,我在想,如果有那就是几乎一样好,并有责任确保一切某种程度上是保密的替代方法。如果没有,我将开放给听到任何人使用批量重命名实用经验。谢谢!

I have already seen that there is something out there called the Bulk Rename Utility but the data files I would like to rename are relatively confidential and so, perhaps unreasonably, I am wary about using it. As well, I am not certain it will allow me to seek out specific strings in the way I would like it to when it comes to renaming the files. As such, I was wondering if there is an alternative method that is nearly as good and has some way of ensuring everything remains confidential. If not, I would be open to hearing about anyone's experiences with Bulk Rename Utility. Thanks!

编辑:我也应该注意到,虽然我是新手,当涉及到编程,我到编程基础的解决方案非常开放。我还要指出,有关的所有文件都是 .html文件。

I should also note that while I am a novice when it comes to programming I am very open to programming-based solutions. I should also note that all the files in question are .html files.

更新:

应该是这样的工作?

 for file in directory:
 f = fopen(file, 'r')
 line = f.readLine();
 while(line):
  if(line.strip() == '<th style="width: 12em;">Name:</th>'):
   nextline = f.readLine().strip();
   c = nextline.find("</td>")
   name = nextline[4:c]
   os.commandline(rename file to name)
   break
  line = f.readLine()

有什么我失踪,使这个在Python运行良好?

Is there anything I'm missing to make this run well in Python?

更新:示例HTML

&LT; HTML和GT; &LT;身体GT; &LT; H1风格=文本对齐:权利;&GT; [标题] LT; / H1&GT; &LT; D​​IV的风格=FONT-family:宋体;&GT; &LT; D​​IV的风格=浮动:左;&GT; [NUMBER] LT; / DIV&GT; &LT; D​​IV的风格=浮动:权利;&GT; [日期]&LT; / DIV&GT; &LT; D​​IV的风格=明确:既; /&GT; &LT; / DIV&GT; &LT;小时/&GT; &LT; H3&GT; [信息] LT; / H3 GT&; &LT;表类=TEXTCELLSPACING =0的cellpadding =0BORDER =0&GT; &所述; TR&GT;百分位风格=宽度:12em;&GT;名称:LT; /第i &LT; TD&GT; [NAME] LT; / TD&GT; &LT; / TR&GT; &LT; /身体GT; &LT; / HTML&GT;

推荐答案

对不起,你的问题是不太清楚。读你的问题了几次后,我以为你想一个方法(这可能是Windows批处理.bat文件)是:

Excuse me, your question is not clear enough. After read your question several times, I assumed that you want a method (that may be a Windows batch .bat file) that:


  • 允许用户输入搜索字符串。

  • 过程中的几个文件,扩展名为.html。

  • 查找每个文件的℃之间的价值; TD&GT; &LT; / TD&GT; 标记出现在搜索字符串的第一个匹配的下一行,并用它来命名文件。

  • Allows the user to input the search string.
  • Process several files with .html extension.
  • Look in each file for the value between <td> and </td> tags that appear in the next line after the first match of the search string, and use it to rename the file.

下面的批处理文件做这样一个过程:

The Batch file below do such a process:

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

@echo off

set /P "search=Enter search string: "
for /F "delims=" %%a in ('dir /B /A-D *.html') do (
   for /F "delims=" %%b in ('cscript //nologo //e:jscript "%~F0" "%%a"') do (
      if "%%b" neq "NoNameFound" (
         ECHO ren "%%a" "%%b"
      ) else (
         echo ERROR: No name found in file "%%a"
      )
   )
)
goto :EOF

@end

var file = new ActiveXObject("Scripting.FileSystemObject").OpenTextFile(WScript.Arguments.Item(0)),
    search = WScript.CreateObject("WScript.Shell").Environment("Process")("search"),
    re = new RegExp(search+".+\n.+<td>(.+)<\/td>",""), result;

if (result = re.exec(file.ReadAll())) {
   WScript.Stdout.WriteLine(result[1]);
} else {
   WScript.Stdout.WriteLine("NoNameFound");
}
file.Close();

这是你的榜样数据的输出:

This is the output with your example data:

C:\> test
Enter search string: <th style="width: 12em;">Name:</th>
ren "input.html" "[NAME]"

请注意,此批处理文件只是显示的的命令,但它不执行呢!你需要为了执行它之前删除 ECHO 部分仁

Note that this Batch file just display the ren command, but it does not execute it! You need to remove the ECHO part before ren in order to execute it.

如果我错过了什么,请输入经过修改的评论我的规格。

If I missed something, please enter a comment with modifications to my specifications.

这篇关于重命名基于文档文本字符串多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆