本帖最后由 flashercs 于 2018-12-18 06:43 编辑
随机插入字符.bat- @echo off
- for /f "tokens=1 delims=:" %%A in ('findstr /n "#######*" %0') do more +%%A %0 >"%~dpn0.ps1"
- powershell.exe -ExecutionPolicy Bypass -File "%~dpn0.ps1"
- pause
- exit /b
- ##################################################################
- # filePath,relative to the script directory or absolute path.设置文件路径,相对或绝对
- $filePath = '10.txt'
- # inserted string list.设置插入字符串列表
- $strInsert = @('AA', 'BB', 'CC', 'DD', 'EE')
- if (![System.IO.Path]::IsPathRooted($filePath)) {
- $filePath = [System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Definition), $filePath)
- }
- function getInsertPosition {
- param (
- [string]$str,
- [string[]]$arrInsertStr
- )
- $a = [System.Array]::CreateInstance([int], $arrInsertStr.Count)
- $len = $str.Length + 1
- for ($i = $a.Count - 1; $i -ge 0 ; $i--) {
- $a[$i] = Get-Random -Minimum 0 -Maximum $len
- }
- $a = $a|Sort-Object
- return $a
- }
- (Get-Content -LiteralPath $filePath -Encoding Default|ForEach-Object {
- $arr = getInsertPosition -str $_ -arrInsertStr $strInsert
- $index = 0
- $s = ''
- for ($i = 0; $i -lt $arr.Count; $i++) {
- $s += $_.Substring($index, $arr[$i] - $index) + $strInsert[$i]
- $index = $arr[$i]
- }
- $s += $_.Substring($index)
- $s
- })|Set-Content -LiteralPath $filePath -Encoding Default
复制代码
|