找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 31867|回复: 4

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

[复制链接]
发表于 2024-4-27 21:20:41 | 显示全部楼层 |阅读模式
本帖最后由 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

评分

参与人数 1PB +2 收起 理由
Batcher + 2 感谢给帖子标题标注[已解决]字样

查看全部评分

发表于 2024-4-28 09:04:57 | 显示全部楼层
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 ')
复制代码
 楼主| 发表于 2024-4-28 11:08:28 | 显示全部楼层
回复 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


请按任意键继续. . .
发表于 2024-4-28 13:41:54 | 显示全部楼层
回复 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技术 +1 收起 理由
a574045075 + 1 谢谢啦

查看全部评分

发表于 2024-4-29 10:36:01 | 显示全部楼层
回复 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技术 +1 收起 理由
a574045075 + 1 谢谢

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-18 02:48 , Processed in 0.018158 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表