本帖最后由 ivor 于 2016-2-16 17:50 编辑
回复 1# Hamelin
bat:- @echo off&setlocal enabledelayedexpansion
- for /f "delims=" %%a in ('dir /b *.txt') do (
- set /a a+=1
- for /f "delims=" %%b in ('findstr /n .* "%%a"') do (
- set "line=%%b"
- set line=!line:*:=!
- (if [!line!] neq [] (echo !line:old=new!) else (echo,))>>"%%a.bak"
-
- )
- )
- echo 统计处理了%a%个TXT文本
- pause
复制代码 PowerShell:- foreach ($file in (dir *.txt)){Get-Content($file) | %{$_.Replace("old","new")} | Out-File "$file.bak"}
复制代码 python3.5 :- import os
- a = 0
- items = os.listdir(".")
- newlist = []
- for names in items:
- if names.endswith(".txt"):
- a += 1
- fOpen = open(names).readlines()
- wFile = open("%s.bak" % names, "w")
- for line in fOpen:
- wFile.write(line.replace("old", "new"))
-
- print("处理了%d个TXT文本" % a)
复制代码
|