查找/替换但增加值

时间:2009-07-09 18:12:51

标签: html notepad++

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

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

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

基本上执行CTRL + F和REPLACE'69thStreet010.jpg'与......

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

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

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

7 个答案:

答案 0 :(得分:9)

查看Notepad ++中的Column Editor功能。您可以在要增加的区域上进行块选择,然后给它一个开始/结束范围,然后就可以了。

我最近处于类似情况。我做了一些正则表达式搜索/替换,让我的文件名变成我的“默认”格式,加上一些填充的“000”,我希望我的增量开始。之后,我使用 Shift + ALT 来阻止选择000s并使用编辑器完成剩下的工作。

答案 1 :(得分:5)

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

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

替换术语:

images/xyz$1.jpg

答案 2 :(得分:4)

我只使用Excel&记事本++

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

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

等。

然后将事物复制/粘贴到Notepad ++中(如果作业很简单,则使用Excel的CONCATENATE函数)并进行替换。

答案 3 :(得分:4)

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

即。
搜索并替换字符串中的整数值
从1开始逐步替换(0是默认的起始值):

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

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

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

将成为
...图像/ xyz001.jpg
...图像/ 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()

答案 4 :(得分:1)

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

答案 5 :(得分:0)

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

在过去,我遇到了类似的挑战,我这样解决了。 这几乎适用于所有的胜利。

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

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

dir /b > filelist.txt

使用名称

保存文件
makefilelist.bat

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

双击刚保存的bat文件:批处理脚本在目录中执行clean 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:首先删除报告不需要的文件的列表行:

makelist.bat

Filelist.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

与其他文件扩展名相同。

答案 6 :(得分:-1)

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

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

相关问题