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

随机插入字符.bat
  1. @echo off
  2. for /f "tokens=1 delims=:" %%A in ('findstr /n "#######*" %0') do more +%%A %0 >"%~dpn0.ps1"
  3. powershell.exe -ExecutionPolicy Bypass -File "%~dpn0.ps1"
  4. pause
  5. exit /b
  6. ##################################################################
  7. # filePath,relative to the script directory or absolute path.设置文件路径,相对或绝对
  8. $filePath = '10.txt'
  9. # inserted string list.设置插入字符串列表
  10. $strInsert = @('AA', 'BB', 'CC', 'DD', 'EE')
  11. if (![System.IO.Path]::IsPathRooted($filePath)) {
  12.   $filePath = [System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Definition), $filePath)
  13. }
  14. function getInsertPosition {
  15.   param (
  16.     [string]$str,
  17.     [string[]]$arrInsertStr
  18.   )
  19.   $a = [System.Array]::CreateInstance([int], $arrInsertStr.Count)
  20.   $len = $str.Length + 1
  21.   for ($i = $a.Count - 1; $i -ge 0 ; $i--) {
  22.     $a[$i] = Get-Random -Minimum 0 -Maximum $len
  23.   }
  24.   $a = $a|Sort-Object
  25.   return $a
  26. }
  27. (Get-Content -LiteralPath $filePath -Encoding Default|ForEach-Object {
  28.   $arr = getInsertPosition -str $_ -arrInsertStr $strInsert
  29.   $index = 0
  30.   $s = ''
  31.   for ($i = 0; $i -lt $arr.Count; $i++) {
  32.     $s += $_.Substring($index, $arr[$i] - $index) + $strInsert[$i]
  33.     $index = $arr[$i]
  34.   }
  35.   $s += $_.Substring($index)
  36.   $s
  37. })|Set-Content -LiteralPath $filePath -Encoding Default
复制代码

TOP

本帖最后由 flashercs 于 2018-12-18 14:42 编辑

回复 5# wwwstwyb2


    空行跳过?还是空格?
不添加到行首/尾?如果一行一共1个字符怎么不添加到行首尾?
你的举例也是添加到行首了....

TOP

返回列表