How to use a batch file to delete a line of text in a bunch of text files?

℡╲_俬逩灬. 提交于 2019-12-08 12:02:31

问题


I have a bunch of txt files in my D drive which are placed randomly in different locations. Some files also contain symbols. I want a batch file so that I can delete their specific lines completely at the same time without doing it one by one for each file and please refer to a code which does not create a new text file at some other location with the changes being incorporated i.e. I do not want the input.txt and output.txt thing. I just need the original files to be replaced with the changes as soon as I click the batch file.

e.g D:\abc\1.txt D:\xyz\2.txt etc

I want both of their 3rd lines erased completely with a single click and the new file must be saved with the same name in the same location i.e. the new changed text files must replace the old text files with their respective lines removed. Maybe some sort of *.txt thing i.e i should be able to change all the files with the .txt extensions in a drive via a single batch file perhaps in another drive,not placing my batch file into each and every folder separately and then running them. Alternatively a vbs file is also welcomed.


回答1:


This uses a helper batch file called findrepl.bat from - http://www.dostips.com/forum/viewtopic.php?f=3&t=4697

Place findrepl.bat in the same folder as the batch file below.

It will search for every *.txt file on drive d: and remove line 3.

@echo off
for /r "d:\" %%a in (*.txt) do (
   echo processing "%%a"
   type "%%a"|findrepl /v /o:3:3 >"%%a.tmp"
   move "%%a.tmp" "%%a" >nul
)
pause


来源:https://stackoverflow.com/questions/19424728/how-to-use-a-batch-file-to-delete-a-line-of-text-in-a-bunch-of-text-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!