Ex:
:delLine file linen
:: file -- file you want to modify.
:: linen -- line number you wish to delete.
...
<Help with this part, hahaha!>
Someone can help me.
![Rolling Eyes :roll:](./images/smilies/icon_rolleyes.gif)
Thanks!
![Embarassed :oops:](./images/smilies/icon_redface.gif)
Moderator: DosItHelp
Code: Select all
@echo off
call:DelLine file.txt 10
exit
:DelLine %file% %line_no%
SETLOCAL DisableDelayedExpansion
set "$TMP=%TEMP%\%RANDOM%%RANDOM%.tmp"
(for /f "tokens=1* delims=]" %%a in ('^<%~1 find /v /n ""') do (
if not "%%a"=="[%~2" echo.%%b))>"%$TMP%"
copy "%$TMP%" "%~1"&& del "%$TMP%"
ENDLOCAL
GoTo:EOF
amel27 wrote:via native CMD, text lines must not begin with "]"
leading "[" supported, only leading "]" stripped from lineghostmachine4 wrote:I wouldn't want to use it if my lines ever were to unknowingly contain a beginning "[".
amel27 wrote:leading "[" supported, only leading "]" stripped from lineghostmachine4 wrote:I wouldn't want to use it if my lines ever were to unknowingly contain a beginning "[".
Code: Select all
@echo off &setlocal
call :DelLine "file.txt" 10
goto :eof
:DelLine file_name line_no
>nul move "%~f1" "%temp%\%~n1.tmp" ||goto :eof
for /f "tokens=* delims=1234567890" %%a in ('type "%temp%\%~n1.tmp"^|findstr /n "^"') do (
set "line=%%a"
set /a n+=1
setlocal enabledelayedexpansion
if "!n!" neq "%~2" >>"%~f1" (echo(!line:~1!)
endlocal
)
goto :eof
akol wrote:Hey, don't worry. The lines of my document doesn't will begin with "]". They will begin with ", any problems?
--- EDITED ---
I tested and had no problem starting the lines with ".
Code: Select all
C:\test>more file
1 this is line 1
2 this is line 2
3 this is line 3
C:\test>set num=2
C:\test>gawk -vn=%num% "NR!=n" file
1 this is line 1
3 this is line 3
Nice tip, aGerman, thanks. But your SUB required DisableDelayedExpansion too. If main script use EnableDelayedExpansion mode, exclamations cut out from text.aGerman wrote:This is my proposal
Code: Select all
:DelLine file_name line_no
setlocal disabledelayedexpansion
set /a n=0
if exist "%temp%\%~n1.tmp" del "%temp%\%~n1.tmp"
for /f "tokens=* delims=1234567890" %%a in ('findstr /p /n "^" "%~f1"') do (
set "line=%%a"
set /a n+=1
setlocal enabledelayedexpansion
if "!n!" neq "%~2" >>"%temp%\%~n1.tmp" (echo(!line:~1!)
endlocal
)
if %n%==0 (endlocal &exit /b 1)
>nul move /y "%temp%\%~n1.tmp" "%~f1" ||(endlocal &exit /b 1)
endlocal &exit /b 0