简体   繁体   中英

Remove content from text file using command line

I have already done many searches and have not found a clear way to do it. there are many variations of how people are removing contents in a text file but not the way I need it to be done.

I have a watch folder that has about 22 text file dropped every 30 minutes. I need to be able to run something that deletes the first 3000 lines in those text file.

I have other task performing to those files and using task scheduler to trigger batch files at a certain time.

So running it on schedule is no issue.

My issue is I cannot find a way to remove 3000 lines from each of the text files, without copying, deleting or changing the other content.

Does anyone know how to remove 3000 lines from a text file? I can get the loop to find all the text file no issue, jut doesn't know the command to remove lines.

I would just use powershell for this. This example removes the first 3000 lines from a single test.txt file.

$file = "test.txt"
$remo = get-content $file
$remo[3000..$remo.count] | set-content $file

If you feel comfortable with batch file, you could just loop the above powershell into batch to do this to each file..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM