0

I found out that my batch script is not working properly. I wanted to write a function which deletes specific line in a file, this is my code:

:unmark
    type %MARKPATH% | findstr /B /V %1 > %MARKPATH_COPY%
GOTO:EOF

Yes, it works, but it writes new content (without specific lines) in a new file. But instead of it, I want to overwrite an existing file. Like that:

:unmark
    type %MARKPATH% | findstr /B /V %1 > %MARKPATH%
GOTO:EOF

But it's not working, file is empty after code execution. Can you help me figure out what is a problem and how to solve it?

0

2 Answers 2

1

This style should work:

@echo off
set "MARKPATH=%userprofile%\desktop\input.txt"
set "MARKPATH_COPY=%userprofile%\desktop\searchfile.txt"

findstr /B /V "%~1" <"%markpath%" > "%MARKPATH_COPY%"
move /y "%MARKPATH_COPY%" "%markpath%" >nul
Sign up to request clarification or add additional context in comments.

2 Comments

Great solution, but what does >nul do? EDIT: I just check it out, and it does not work properly. It writes lines if it contains string provided in findstr. Not delete it.
The >nul hides the message that MOVE prints to keep the screen clear. The findstr command uses the same switches as your example. Yours must be wrong too then. :D
0

I think I found a solution to my problem (maybe not practical, but it worked)

:unmark
    set LASTPATH=%CD%
    cd %MARKPATH%
    type %MARKFILE% | findstr /B /V %1 > %MARKFILECOPY%
    del %MARKFILE%
    ren %MARKFILECOPY% %MARKFILE%
    cd %LASTPATH%
GOTO:EOF

Comments

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.