Renaming Multiple Files Based on Document Text Str

2019-07-17 05:33发布

问题:

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!

EDIT: 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.

UPDATE:

Would something like this work?

 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()

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

UPDATE: Sample html

<html> <body>
   <h1 style="text-align: right;">[TITLE]</h1>
    <div style="font-size: 12pt;">
      <div style="float: left;">[NUMBER]</div>
      <div style="float: right;">[DATE]</div>
      <div style="clear: both;" />
    </div>
    <hr />
    <h3>[INFO]</h3>
    <table class="text" cellspacing="0" cellpadding="0" border="0">
      <tr>
        <th style="width: 12em;">Name:</th>
        <td>[NAME]</td>
      </tr> </body> </html>

回答1:

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:

  • 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]"

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.



回答2:

Change names using powershell and cmd like a boss just refer the link

http://www.howtogeek.com/111859/how-to-batch-rename-files-in-windows-4-ways-to-rename-multiple-files/

Dont come to conclusion before reading whole page.