[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文件操作] [已解决]如何用批处理把文本指定内容替换到文本内其它内容?

本帖最后由 a574045075 于 2024-4-30 19:49 编辑

有个index.txt文本,我想把<title>Index of /和</title>之间的0-9(注意:这里的值不一定是0-9,是随机的,也会有英文的)替换到alt="[   ]"> <a href="并在后面加入/符号,应该怎么做? 谢谢!
图一



图二


index.txt文本
https://pan.baidu.com/s/1vEqOXhNVQFYZ95NgOhRMXQ?pwd=6666
1

评分人数

    • Batcher: 感谢给帖子标题标注[已解决]字样PB + 2

powershell
  1. <# :
  2. @echo off
  3. powershell -noprofile -NoLogo "iex (${%~f0} | out-string)"
  4. pause
  5. exit
  6. #>
  7. $regex = '<title>Index\s*of\s*/([^<]+)'
  8. $pan = 'alt=\"\[\s*\]\">\s*<a href=\"'
  9. $content = [IO.File]::ReadAllText('index.txt ')
  10. $title = [regex]::matches($content, $regex).Groups[1].Value + '\'
  11. [regex]::Replace($content,$pan, $title)|Out-File $('new_'+'index.txt ')
复制代码

TOP

回复 2# terse


运行出现错误.
    无法对空数组进行索引。
所在位置 行:10 字符: 52
+ $title = [regex]::matches($content, $regex).Groups[ <<<< 1].Value + '\'
    + CategoryInfo          : InvalidOperation: (1:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

使用“3”个参数调用“Replace”时发生异常:“值不能为空。
参数名: evaluator”
所在位置 行:11 字符: 17
+ [regex]::Replace <<<< ($content,$pan, $title)|Out-File $('new_'+'index.txt ')
    + CategoryInfo          : NotSpecified: ( [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException


请按任意键继续. . .

TOP

回复 3# a574045075
区分一下大小写
  1. <# :
  2. @echo off
  3. powershell -noprofile -NoLogo "iex (${%~f0} | out-string)"
  4. pause
  5. exit
  6. #>
  7. $regex = '(?i)<(title)>index\s*of\s*/([^<]+)</\1>'
  8. $pan = '(?i)alt=\"\[\s*\]\">\s*<a href=\"'
  9. $content = [IO.File]::ReadAllText('index.txt ')
  10. $title = [regex]::matches($content, $regex).Groups[2].Value + '\'
  11. [IO.File]::WriteAllText('new_index.txt', [regex]::Replace($content,$pan, $title))
复制代码
1

评分人数

TOP

回复 1# a574045075

第3方工具gawk( http://bcn.bathome.net/tool/4.1.0/gawk.exe )的实现方式如下:
  1. gawk "/[ \t]*<title>Index of \/(.+)<\/title>$/{r=gensub(/[ \t]*<title>Index of \/(.+)<\/title>$/,\"\\1/\",\"g\")}/alt=\"\[   \]\"> <a href=\"/{if(r){sub(/(alt=\"\[   \]\"^> ^<a href=\")/,r)}}1" index.txt>out.txt
复制代码
1

评分人数

TOP

返回列表