在批处理正则表达式搜寻替换 [英] regex search replace in batch

查看:430
本文介绍了在批处理正则表达式搜寻替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做搜索/在Windows批处理一组文件的正则表达式替换。这将是这样的:

I want to do search/replace with a regex pattern in windows batch on a set of files. It will be something like:

if the regex matches a line matches then replace it with a new line.

我也需要在正则表达式中添加变量只需更换价值。
有谁知道如何处理批处理脚本?我没那么熟悉。一些例子也许有帮助。

also I need to add a variable in the regex to just replace the value. Does anyone know how to deal with the batch script? I'm not that familiar with it. Some examples maybe helpful.

推荐答案

您可以从<一个受益href=\"http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/findstr.mspx?mfr=true\"相对=nofollow> FINDSTR 命令。它支持有限的语法规律EX pressions。我说,你有文件

You can benefit from findstr command. It supports regular expressions with limited syntax. Say, you have files

ignore_me.txt
rename_me01.txt
rename_me02.txt
rename_me03.txt
rename_me04.txt

一个命令

dir /b | findstr "rename_me[0-9][0-9]"

将输出

rename_me01.txt
rename_me02.txt
rename_me03.txt
rename_me04.txt

OK,不是很好的例子,因为你可以做到这一点用好老通配符。我的观点是 FINDSTR 该模式是一个普通的前pression。

OK, not very good example, because you can do this with good old ? wildcard. My point is that pattern for findstr is a regular expression.

最近,我有一个类似的问题,但我无聊试图找出如何为替换正则表达式模式。我不知道如果它甚至有可能只有本地的Windows命令。因此,对于更简单,但还是原生解决方案,我提到的 Windows脚本宿主。我的任务是发现,在自己的名字格式的日期的所有文件的 DD.MM.YYYY 的和替换使用当前最新的。

Recently I had a similar problem, but I bored trying to find out how to replace regex patterns. I'm not sure if it even possible with only native Windows commands. So, for more simple, but still native solution I referred to Windows Scripting Host. My task was to find all files that have in their names a date in format dd.mm.yyyy and replace that date with current.

有关的脚本是:

<job>
    <script language="JavaScript">
        var d = new Date();
        // get current date for replacement
        var currDate = (d.getDate() + "." + (d.getMonth() + 1) + "." + d.getFullYear())
            // add leading zeros to date and month
            .replace(/^([0-9])\./g, '0$1.').replace(/\.([0-9])\./g, '.0$1.');
        var fso = new ActiveXObject("Scripting.FileSystemObject");  
        var files = new Enumerator(fso.getFolder(".").files);
        var count = 0;
        for (; !files.atEnd(); files.moveNext())
        {
            var file = ""+files.item(); // make it string
            if (file.match(/[0-9]{2}\.[0-9]{2}\.[0-9]{4}/)) {
                var newName = file.replace(/[0-9]{2}\.[0-9]{2}\.[0-9]{4}/, currDate);
                fso.moveFile(file, newName);
                count++;
            }
        }
        WScript.echo("Renamed "+count+" files");
    </script>
</job>

在名称保存它u.wsf ,放入文件夹,这些文件。扩展与 W脚本相关联的,所以当它在GUI模式下运行,但还可以在命令行模式下运行该文件双击:

Saved it under name u.wsf and put into the folder to those files. The extension is associated with wscript so when double clicking on the file it runs in GUI mode, but can also run in command line mode:

cscript u.wsf

这篇关于在批处理正则表达式搜寻替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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