写一个指定路径下删除notepad文本指定多少行并保存替换原文件,文件名不变的批处理
时间: 2023-12-26 11:07:01 浏览: 22
您好,以下是一个示例批处理脚本,用于删除指定路径下的notepad文本文件中的前n行,并将修改后的内容保存到原文件中。请将其中的"path/to/file.txt"替换为您需要处理的文件路径,将"10"替换为您需要删除的行数。
```
@echo off
setlocal EnableDelayedExpansion
set "file=path/to/file.txt"
set "tempfile=%file%.temp"
rem count the number of lines in the file
set "lines=0"
for /f %%i in ('type "%file%"^|find /v /c ""') do set "lines=%%i"
rem calculate the number of lines to keep
set /a "keeplines=%lines%-10"
rem read the file and write the modified content to a temporary file
set "count=0"
(for /f "usebackq delims=" %%i in ("%file%") do (
set /a "count+=1"
if !count! gtr %keeplines% echo %%i
)) > "%tempfile%"
rem overwrite the original file with the modified content
move /y "%tempfile%" "%file%" > nul
```
注意:请谨慎使用此脚本,尤其是对于重要的文件。建议先备份原文件,以免数据丢失。