Board logo

标题: [文件操作] [已解决]批处理如何把文本内容替换到文件里? [打印本页]

作者: a574045075    时间: 2023-2-20 15:40     标题: [已解决]批处理如何把文本内容替换到文件里?

本帖最后由 a574045075 于 2023-2-21 11:42 编辑

这里有个01.txt文本,内容如下图

如何用批处理命令把Using title id的0005000000012400和generated encrypted的e734ae04fb23bf6de77a6e21646c95a6替换到如下图的位置,另外Using title id的值和generated encrypted的值是随机的,不一定是0005000000012400和e734ae04fb23bf6de77a6e21646c95a6

最终想要的结果如下图

作者: 23618342    时间: 2023-2-20 17:59

这个论坛图片上传不了
作者: a574045075    时间: 2023-2-20 18:28

回复 2# 23618342


    看到图片吗?
作者: yyz219    时间: 2023-2-20 18:55

回复  23618342


    看到图片吗?
a574045075 发表于 2023-2-20 18:28



    有图片
作者: czjt1234    时间: 2023-2-20 20:03

本帖最后由 czjt1234 于 2023-2-21 10:15 编辑

vbs
  1. file1 = "01.txt"
  2. file2 = "basetik.tik"
  3. Set oFSO = CreateObject("Scripting.FileSystemObject")
  4. Set oTextStream = oFSO.OpenTextFile(file1)
  5. s = oTextStream.ReadAll()
  6. oTextStream.Close()
  7. Set oRegExp = CreateObject("VBScript.RegExp")
  8. oRegExp.IgnoreCase = True
  9. oRegExp.Pattern = "Using title id\: (.+)?\r\n"
  10. For Each i In oRegExp.Execute(s)
  11.     t = i.SubMatches.Item(0)
  12. Next
  13. With CreateObject("Msxml2.DOMDocument").CreateElement("hex")
  14.     .DataType = "bin.hex"
  15.     .Text = t
  16.     arrByte = .NodeTypedValue
  17. End With
  18. With CreateObject("ADODB.Stream")
  19.     .Type = 1    'adTypeBinary
  20.     .Mode = 3    'adModeReadWrite
  21.     .Open()
  22.     .LoadFromFile file2
  23.     .Position = &H01DC
  24.     .Write arrByte
  25.     .SaveToFile file2, 2
  26.     .Close()
  27. End With
  28. oRegExp.Pattern = "generated encrypted\: (.+)?\r\n"
  29. For Each i In oRegExp.Execute(s & vbCrLf)
  30.     t = i.SubMatches.Item(0)
  31. Next
  32. With CreateObject("Msxml2.DOMDocument").CreateElement("hex")
  33.     .DataType = "bin.hex"
  34.     .Text = t
  35.     arrByte = .NodeTypedValue
  36. End With
  37. With CreateObject("ADODB.Stream")
  38.     .Type = 1    'adTypeBinary
  39.     .Mode = 3    'adModeReadWrite
  40.     .Open()
  41.     .LoadFromFile file2
  42.     .Position = &H01BF
  43.     .Write arrByte
  44.     .SaveToFile file2, 2
  45.     .Close()
  46. End With
  47. MsgBox "ok"
复制代码

作者: terse    时间: 2023-2-20 20:51

你们看到图片?
作者: 77七    时间: 2023-2-20 21:04

回复 6# terse

可以看到顶楼图片,刷新等待一下,试试
作者: WHY    时间: 2023-2-20 21:37

本帖最后由 WHY 于 2023-2-22 20:04 编辑

PowerShell 脚本
2023/02/22 简化脚本。我测试没有发现问题
  1. $file1 = '01.txt';
  2. $file2 = 'basetik.tik';          #修改前的文件名
  3. $file3 = 'basetik_001.tik';      #修改后的文件名
  4. $text  = [IO.File]::ReadAllText($file1, [Text.Encoding]::Default);
  5. $bytes = [IO.File]::ReadAllBytes($file2);
  6. Function Set-ByteValue($key, [int]$offSet){
  7.     $reg = '(?i)(?<=' + $key + ' *)[a-f0-9]+';
  8.     $s = ([regex]::Match($text, $reg)).Value;
  9.     for ($i = 0; $i -lt $s.Length; $i+=2) {
  10.         $x = $i / 2 + $offSet;
  11.         $bytes[$x] = [byte][int]('0x' + $s.SubString($i, 2));
  12.     }
  13. }
  14. Set-ByteValue -key 'Using title id:' -offSet 0x01DC;
  15. Set-ByteValue -key 'generated encrypted:' -offSet 0x01BF;
  16. [IO.File]::WriteAllBytes($file3, $bytes);
  17. echo 'Done';
  18. [Console]::ReadLine();
复制代码

作者: a574045075    时间: 2023-2-21 06:50

回复 8# WHY


    中午回来试试.
作者: 23618342    时间: 2023-2-21 09:17

看到了,我上次就没有成功,奇怪了
作者: terse    时间: 2023-2-21 12:23

奇怪 看不了图
作者: qixiaobin0715    时间: 2023-2-21 12:31

回复 11# terse
可能是浏览器的事,Edge可以看到。
作者: czjt1234    时间: 2023-2-21 13:32

回复 9# a574045075


   测试的结果能发一下吗
如果不成功的,也希望看到错在哪里的
作者: terse    时间: 2023-2-21 13:59

回复 12# qixiaobin0715

Edge  chrome  Firefox都试了 唉! 算了
作者: cnnetstar    时间: 2023-2-21 14:15

回复 1# a574045075


    请问你的图片是怎么上传的? 我试了好像上传不了图片呢.
作者: a574045075    时间: 2023-2-21 15:08

回复 15# cnnetstar


    论坛好像上传不了图片,一上传就失败,我用网络图片.
作者: Batcher    时间: 2023-2-21 15:33

回复 11# terse


    有可能是楼主使用的那个图床的网络问题
作者: Batcher    时间: 2023-2-21 15:34

回复 15# cnnetstar


如果需要上传文件,可以用阿里云盘或百度网盘。

如果需要上传截图,可以找个图床,例如:
http://bbs.bathome.net/thread-60985-1-1.html
作者: idwma    时间: 2023-2-21 16:14

回复 14# terse


    把广告过滤插件关了试试
作者: WHY    时间: 2023-2-21 16:43

HexTool.exe下载
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set "file1=01.txt"
  4. set "file2=basetik.tik"
  5. for /f "tokens=1-2 delims=:" %%i in ('type "%file1%"') do (
  6.     if /i "%%i" == "Using title id" (
  7.         set "HexNum1=%%j"
  8.     ) else if /i "%%i" == "generated encrypted" (
  9.         set "HexNum2=%%j"
  10.     )
  11. )
  12. set "tmp1=%HexNum1: =%"
  13. set "tmp2=%HexNum2: =%"
  14. for %%i in (256, 128, 64, 32, 16, 8, 4, 2, 1) do (
  15.     if "!tmp1:~%%i!" NEQ "" (set "tmp1=!tmp1:~%%i!" & set /a Len1 += %%i)
  16.     if "!tmp2:~%%i!" NEQ "" (set "tmp2=!tmp2:~%%i!" & set /a Len2 += %%i)
  17. )
  18. set /a "offSet1=0x01DC, offSet2=0x01BF, Len1=(Len1+1)/2, Len2=(Len2+1)/2"
  19. HexTool "%file2%" %offSet1% %Len1% %HexNum1%
  20. HexTool "%file2%" %offSet2% %Len2% %HexNum2%
  21. pause
复制代码

作者: terse    时间: 2023-2-22 11:32

  1. <# : PS
  2. @echo off
  3. powershell -noprofile -NoLogo "iex (${%~f0} | out-string)"
  4. pause & exit
  5. #>
  6. $filename = ls '.\01.txt'
  7. $file = ls 'basetik.tik';
  8. $offset = @(0x01DC,0x01BF);
  9. $text = [IO.File]::ReadAllText($filename);
  10. [regex]$reg = '(?i)(?<=Using title id: *|generated encrypted: *)([0-9a-f]{2})+';
  11. $group = $reg.Matches($text);
  12. $fs=new-object io.filestream $file,open;
  13. (0..($group.Count-1)).ForEach({
  14.      [void]$fs.seek($offset[$_],0);
  15.      $Group[$_].Groups[1].Captures.Value.ForEach({$fs.writebyte( "0x$_" )});
  16. })
  17. $fs.flush();
  18. $fs.close();
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2