0

Is there any solution to this question? I see a lot of questions about deleting first line of a text file but all of them need create a new text file. I need this because my text file is constantly updating with new lines (by a second batch file) so if the script creates a new text file it can accidently delete some new lines with the old text file.

Can i delete first line of a text file without creating a new one? If not, why?

  • Using only batch.
  • Blank lines shouldn't be preserved (If possible).
  • Especial characters like ! can be deleted.
8
  • Should blank lines be preserved? Commented Feb 6, 2016 at 3:58
  • 1
    How long is the file? Does it contain blank line or lines that contain characters to which batch shows sensitivity? Commented Feb 6, 2016 at 4:09
  • Specifically, do you care if the output file doesn't have !s anymore? Commented Feb 6, 2016 at 4:18
  • @Magoo See edit, please. Commented Feb 6, 2016 at 4:28
  • 1
    Oh, now that I don't have to preserve blank lines or special characters, this is going to be way easier. Commented Feb 6, 2016 at 4:32

3 Answers 3

3

IMO this question makes no sense. There is no way to avoid to preserve the new file contents in a place different than the original file; this is true even in any advanced programming language, that would require to read from second line to end of file and copy each line to the beginning of the file. However, at end of the process it would be necessary to truncate the file in order to eliminate the last bytes in the file (with the number of bytes that the first line had).

A Batch file certainly can not perform this type of process, so the lines of the file (from second one up to the end of file) must necessarily be stored in a place different than the original file. One of the answers store the lines in memory variables, but this method is inefficient specially if the file is large.

So, if the question is: "what tricks can be used in order to not use a new file to eliminate the first line in a data file?", then this is a more efficient method:

@echo off

for /F "skip=1 delims=" %%a in ('type input.txt ^& del input.txt') do >> input.txt echo %%a

If you want to preserve empty lines, use this method instead:

@echo off

for /F "skip=1 tokens=1* delims=:" %%a in ('findstr /N "^" input.txt ^& del input.txt') do >> input.txt echo(%%b

You should note that when the for /F command start execution it blocks the file for an exclusive access, so any attempt to modify the file while the for /F is reading it would be avoided with "access denied" error.

Sign up to request clarification or add additional context in comments.

1 Comment

Although the second script fails in case a line starts with one or more :, I like this answer (+1) because I don't like copying the file content to variable(s) due to lack of performance for huge files and my understanding of the question is the same as yours...
2

If you don't care about preserving blank lines, !, (, ), or ^, you can run the input file through a for loop, storing each line in a separate variable, then merging the variables with a newline character at the end of each one.

@echo off
setlocal enabledelayedexpansion
cls

set counter=0
for /f "delims=" %%A in (input.txt) do (
    set line[!counter!]=%%A
    set /a counter+=1
)
set /a counter-=2

set LF=^


for /L %%A in (1,1,!counter!) do set sheet=!sheet!!line[%%A]!!LF!
set /a counter+=1
for /L %%A in (!counter!,1,!counter!) do set sheet=!sheet!!line[%%A]!
echo !sheet!>input.txt

However, if you want to preserve blank lines and special characters, there are a few tricks you can throw in, but the overall idea is the same.

@echo off
setlocal enabledelayedexpansion
cls

:: findstr /n puts line numbers at the start of each line, which will allow us to preserve blank lines
for /f "tokens=1 delims=:" %%A in ('findstr /n "^" input.txt') do set line_counter=%%A

::set /p preserves special characters
<input.txt (
    for /L %%A in (1,1,!line_counter!) do set /p line[%%A]=
)
set /a line_counter-=1

set LF=^


:: Do NOT delete the two blank lines above this line.
for /L %%A in (2,1,!line_counter!) do set sheet=!sheet!!line[%%A]!!LF!
set /a line_counter+=1
for /L %%A in (!line_counter!,1,!line_counter!) do set sheet=!sheet!!line[%%A]!

echo !sheet!>input.txt

5 Comments

Wow! Thats sech a pretty answer! But before o i accept it, what happens if a new line is added during the execution of the script? What happens to the script? What happens to the line?
@Damonki - The script runs too fast for you to add lines during its execution, unless your input file is extremely long. But based on what usually happens when you mess with thing in batch while the script is running, best case scenario is works exactly as intended, worst case scenario is that everything completely and irreparably breaks, most likely case is that you can't possibly predict what will happen so avoid messing with the input file while the script is running if at all possible.
Hmmm, it can be a problem, but i can solve this. Btw, what can i consider "extremely long"?
Oh, and thanks for the answer, it helped a lot! (^-^)
@DaMonki - any number that means you have enough time to edit your input file and save it. I just tested a file with 1500 lines and it still ran too quickly for me to have time to change it. I imagine as long as your input file is under 1 MB, you should be fine.
0

In general a new file is needed because the requested operation requires that you READ and WRITE. So with only 1 file you would be modifying the file that you are reading! If you are simply concerned about the resultant file being a different file, that can be resolved by deleting the original and renaming the new file when done. The PUSHD and POPD are optional... use if applicable. ** Untested **

pushd "YourFolderName"
set "FileName=YourInFileName.txt"
set "TempFile=SomeTempFileName.txt"
if exist "%TempFile% del /f /q "%TempFile%"
more "%FileName%" +1 > "%TempFile%"
del /f /q "%FileName%"
ren "%TempFile%" "%FileName%"
popd

3 Comments

I'm not entirely sure this answers the question, which is asking if you can do that without making a temp file. You answer makes a temp file.
@SomethingDark. Correct.. That is why I added the statement regarding that concern. Post did not provide any details about the underlying reason for that request.
I think the concept is flawed. You are attempting to have 2 entities modifying the same file (this batch file is modifying it while another bat file is adding to it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.