I'm trying to edit a txt file by removing certain line numbers from it. i would like to search for all files in the folder containing .txt, remove some lines and than save the file using the inputfilename with -edit.txt
like: input.txt > input-edit.txt.
this is what i have at the moment:
@echo off (for /f "tokens=1,* delims=[]" %%a in ('type *.txt^|find /v /n ""') do (
echo/|findstr /x "34 35 36 37 38 39 40 41 42 43 80 81 82 83 84 85 86 87 88 89" >nul || echo/%%b
))>%%~na-edit.txt
But the filename that's created is %~na-edit.txt
Can anyone help me?
Thanks!
Here's a quick example of how I might consider doing it. Just replace the name test
, (the source directory), on line 4, and optionally the extension for the source files, (replacing .txt
), on line 5. I used a relative source directory name, but you can use .
for the current directory, or an absolute path too.
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "DirPath=test"
Set "FileExt=.txt"
For /F "Delims=" %%G In ('(Set "PathExt="^) ^& %SystemRoot%\System32\where.exe
"%DirPath%":"*%FileExt%" 2^>NUL ^| %SystemRoot%\System32\findstr.exe /VRI
"\-edit.txt$"') Do If Not Exist "%%~dpnG-edit%%~xG" (
For /F "Delims=" %%H In ('%SystemRoot%\System32\findstr.exe /RN "$" "%%G" ^|
%SystemRoot%\System32\findstr.exe /VR /C:"^3[456789]:" /C:"^4[0123]:"
/C:"^8[0123456789]:"') Do (Set "_=%%H"
SetLocal EnableDelayedExpansion
Echo(!_:*:=!
EndLocal)) 1>"%%~dpnG-edit%%~xG"
I have added some code to prevent parsing or overwriting existing -edit
files, to ignore 8.3 naming for the target file extensions, and to ensure that empty lines are maintained, as well as not adding leading or trailing spaces to those lines. Additionally this method should not ignore any lines beginning with ;
, and should not omit leading characters, (particularly :
or ]
), and any !
characters should also be maintained.
Please note that I decided, due to your example code to use the range abilities of findstr.exe
, but I suppose for less specific patterns you could probably use, for example, /VR "^21: ^35: ^78:"
.
I should mention, as with almost everything I submit here, this is completely untested.