9

我有一个 .HTML 文件,其中附加了许多照片以显示照片库。我正在用新照片替换旧照片,并且我认为必须有一种比简单地复制和粘贴重复文件名更智能的方法。

我希望能够在每次替换时替换和增加一个文件名,从我选择的基本文件名开始。例如...

...images/69thStreet010.jpg
...images/69thStreet011.jpg
...images/69thStreet012.jpg

基本上执行 CTRL+F 并用...替换'69thStreet010.jpg'

...images/xyz001.jpg
...images/xyz002.jpg
...images/xyz003.jpg

依此类推,直到我想要它停止。这里有人有什么建议吗?非常感谢!

更新:我还应该补充一点,我正在使用 Notepad++ 来编辑我的文件。

4

7 回答 7

12

查看Notepad++中的列编辑器功能。您可以在要增加的区域上进行块选择,然后为其指定开始/结束范围,一切就绪。

我最近也有类似的情况。我做了一些正则表达式搜索/替换,以将我的文件名变成我的“默认”格式,并在我希望开始递增的地方添加一些填充的“000”。之后,我使用Shift+ALT来阻止选择 000 并使用编辑器完成其余的工作。

于 2012-10-04T15:20:42.023 回答
8

好的,我想出了一个 Notepad++ 解决方案。
它确实需要您安装 Notepad++ 插件 PythonScript。这是一个类似于 TextPad 的增量替换。


在字符串中搜索和替换整数值,
从 1 开始递增替换(0 是默认起始值​​):

在您的情况下,它将是
Search: ...images/69thStreet[0-9]+.jpg
替换: ...images/xyz00\i(1).jpg

安装 PythonScript 插件。
将下面的代码保存为 PythonScript 并关联一个快捷方式(即 Ctrl+i):
然后在 Notepad++ 中打开您的 html 文件并在您的 html 文件上运行下面的脚本。
当搜索替换对话框提示时,输入以下 2 行:
...images/69thStreet[0-9]+.jpg
...images/xyz00\i(1).jpg

现在,所有
...images/69thStreet010.jpg
...images/69thStreet011.jpg
...images/69thStreet012.jpg 的实例

将变为
...images/xyz001.jpg
...images/xyz002.jpg
...images/xyz003.jpg

这是 PythonScript 代码

from Npp import *
import re, string

# First we'll start an undo action, then Ctrl-Z will undo the actions of the whole script
editor.beginUndoAction()

expression     = notepad.prompt("Enter the search string on the first line, followed by Ctrl+Enter, \n" +
                                "followed by the replace string on second line",
                                "Incremental Search/Replace" ,
                                "")

expressionList = re.split(r"[\n\r]+", expression)

if len(expressionList) == 2:
    foundCount = [0]

    def incrementalReplace(parmReplaceStr,parmFoundCount):
        varPatternI  = r'\\i\((?P<starting>[0-9]+)\)'
        varPatternIi = r'\\i'
        varPatternISearch = re.search(varPatternI , parmReplaceStr)

        if varPatternISearch != None:
            varFoundCount    = int(varPatternISearch.group('starting')) + parmFoundCount[0]
            varReplaceString = re.sub(varPatternI ,str(varFoundCount    ),parmReplaceStr)
        elif re.search(varPatternIi, parmReplaceStr) != None:
            varReplaceString = re.sub(varPatternIi,str(parmFoundCount[0]),parmReplaceStr)

        parmFoundCount[0] = parmFoundCount[0] + 1    
        return varReplaceString

    # Do a Python regular expression replace
    editor.searchAnchor()
    while editor.searchNext(0x00200000,expressionList[0]) != -1:
        editor.replaceSel(incrementalReplace(expressionList[1],foundCount))
        editor.lineDown() 
        editor.searchAnchor()

    # End the undo action, so Ctrl-Z will undo the above two actions
    editor.endUndoAction()
于 2011-08-03T23:15:55.900 回答
6

如果您的 S&R 支持 Regex,请使用它。搜索词:

images/69thStreet([0-9]+).jpg

替换术语:

images/xyz$1.jpg
于 2009-07-09T18:16:12.180 回答
4

我只使用 Excel & Notepad++

在 Excel 中创建一列递增数字。然后在下一个(或上一个)列中,输入您希望与递增数字合并的行。例如:

file00  |  1  |  .jpg
file00  |  2  |  .jpg

等等。

然后将内容复制/粘贴到 Notepad++ 中(如果工作简单,则使用 E​​xcel 的 CONCATENATE 函数)并进行替换。

于 2010-04-14T18:59:33.733 回答
1

是时候学习脚本语言了。Python/Ruby/Perl 可以通过使用简单的正则表达式和目录列表在几行中完成此操作。

于 2009-07-09T18:14:40.217 回答
0

notepad++ 允许您进行多行编辑。但这里的主要问题是尽可能快地从它们所在的目录中获取所有文件名。

过去,我遇到过类似的挑战,我以这种方式解决了这个问题。这几乎适用于所有获胜版本。

要列出目录中的所有文件,请在目录本身中创建一个 bat 文件。

复制并粘贴以下批处理代码

dir /b > filelist.txt

用名字保存文件

makefilelist.bat

你给它的名字并不重要:重要的是文件扩展名 .bat

双击刚刚保存的 bat 文件:批处理脚本在目录中执行一个干净的 dir 命令,它被执行,将输出重定向到文件 filelist.txt

在不到一秒钟的时间里,您将输出保存到 filelist.txt 中,女巫将类似于以下内容:

filelist.txt
Image File Name 01.gif
Image File Name 02.gif
Image File Name 03.gif
Image File Name 04.gif
Image File Name 05.gif
Image File Name 06.gif
Image File Name 07.gif
Image File Name 08.gif
Image File Name 09.gif
Image File Name 10.gif
Image File Name 11.gif
Image File Name 12.gif
Image File Name 13.gif
Image File Name 14.gif
Image File Name 15.gif
Image File Name 16.gif
Image File Name 17.jpg
Image File Name 18.jpg
Image File Name 19.jpg
Image File Name 20.jpg
Image File Name 21.jpg
Image File Name 22.jpg
Image File Name 23.jpg
Image File Name 24.jpg
Image File Name 25.jpg
Image File Name 26.jpg
Image File Name 27.jpg
Image File Name 28.jpg
Image File Name 29.jpg
Image File Name 30.jpg
Image File Name 31.jpg
Image File Name 32.jpg
Image File Name 33.PNG
Image File Name 34.PNG
Image File Name 35.PNG
Image File Name 36.PNG
Image File Name 37.PNG
Image File Name 38.PNG
Image File Name 39.PNG
Image File Name 40.PNG
Image File Name 41.PNG
Image File Name 42.PNG
Image File Name 43.PNG
Image File Name 44.PNG
Image File Name 45.PNG
Image File Name 46.PNG
Image File Name 47.PNG
Image File Name 48.PNG
makelist.bat

当然,记录到文件列表中的名称可能会有所不同......脚本记录每个文件,无论它是否按顺序或它的名称是随机名称还是包含日期时间(时间戳)等:它记录一切都在女巫被执行的同一个目录中......

现在您可以使用 notepad++ 打开 filelist.txt:首先删除列表中报告不需要的文件的行:

制作清单.bat

文件列表.txt

此操作是必要的,因为脚本还列出了自身和 filelist.txt。

然后使用多行编辑工具一次在所有行周围添加标签,或者构建照片库所需的所有其他内容,甚至是 javascript 数组等......最后将完成的工作复制并粘贴到编码下的图像库文件. 完毕。像喝一杯水一样容易:解释比做更难!的确。;-)


改进:

可以通过修改批处理命令来获取仅包含 jpg 文件的列表,如下所示:

dir *.jpg /b > filelist.txt
Image File Name 17.jpg
Image File Name 18.jpg
Image File Name 19.jpg
Image File Name 20.jpg
Image File Name 21.jpg
Image File Name 22.jpg
Image File Name 23.jpg
Image File Name 24.jpg
Image File Name 25.jpg
Image File Name 26.jpg
Image File Name 27.jpg
Image File Name 28.jpg
Image File Name 29.jpg
Image File Name 30.jpg
Image File Name 31.jpg
Image File Name 32.jpg

与其他文件扩展名相同。

于 2016-10-05T21:21:27.837 回答
-1

有。使用 python 脚本创建您的照片列表,该脚本查看您想要的任何目录,并重命名或仅列出所有文件名。然后从它转到生成所需 HTML 的 python 脚本(此时应该很容易)。然后复制并粘贴。

Python 非常易于使用,您将能够在 5 分钟内下载它并在另外 10 分钟内找到读取/更改文件名的教程。

于 2009-07-09T18:16:18.107 回答