@echo
off
for
/f
"delims="
%%i
in
('dir
/s/b
*.txt')
do
(
for
/f
"delims="
%%a
in
('type
"%%~fi"')
do
(
set
"foo=%%a"
call,set
foo=%%foo:1234=4311%%
call,echo/%%foo%%>>"%%~fi._"
)
move
"%%~fi._"
"%%~fi"
)
exit
以上为批处理代码,保存为bat文件,双击执行,使用前备份文件。另给出VBS方案:
Set
WshShell
=
CreateObject("Wscript.Shell")
WshShell.Run
"cmd
/c
dir
/s/b
*.txt
>
list.txt",vbHide
Wscript.Sleep
1000
sFile
=
"list.txt"
Set
objFSO
=
CreateObject("Scripting.FileSystemObject")
Set
oFile
=
objFSO.OpenTextFile(sFile,1)
Do
While
Not
oFile.AtEndOfStream
strLine
=
oFile.ReadLine
If
Len(strLine)
>
0
Then
Set
File
=
objFSO.OpenTextFile(strLine,
1)
aryLines
=
File.ReadAll
File.Close
aryLines
=
Replace(aryLines,
"1234",
"4311")
Set
File
=
objFSO.OpenTextFile(strLine,
2)
File.Write
aryLines
File.Close
End
If
Loop
oFile.Close
objFSO.DeleteFile
sFile
Set
objFSO
=
Nothing
保存为VBS文件,放到TXT文件目录,双击执行。
----------------------------------------
两段代码都可以实现批量替换,无论在该文件夹的深度是多少。不用打开TXT直接替换。不懂可以hi我。
@echo off
set work_path=D:\Program
set in_data=4321
pushd %work_path%
for /f "delims=" %%a in ('"dir /a-d /b *.txt"') do (
if exist %%a (
if %%~za equ 0 (
echo %%a写入%in_data%
echo %in_data% > %%a
)
)
)
pause & exit
思路是搜索路径下的.txt文件,如果文件大小为0 则写入数据