Delete/add lines in txt file with cmd

November 6, 2010 at 14:16:32
Specs: Windows Vista
Hi there,

I'm writing a pretty basic script with the 'attrib' command to hide/show files and folders.
Every time I hide a file it should write the pathname (+ date) into a .txt file.
But it also should delete the pathnames of files and objects in the .txt file if they are restored.

If an user enters a wrong pathname then I want to enter a custom error message and being able to go back to :A

Finaly, I was wondering if it was possible to make an perfect box with ANY username, time and date.

Here is the script:

@echo off

title File Hider
color 02
cd C:\Users\Myname\Documents\Myname\
:: (I do this because otherwise it won't find test.txt)
cls

echo.
echo.

echo ----------------------
echo ^| ^|
echo ^| Welcome %username% ^|
echo ^| ^|
echo ^| It is %time% ^|
echo ^| %date% ^|
echo ^| ^|
echo ----------------------

echo.
echo.

pause

cls

:A

echo.
echo.

ECHO Choose Option "1" if you want to hide an object.
ECHO Choose Option "2" if you want to make an object visible again.
ECHO Choose Option "3" to display a list of hidden objects.
ECHO Choose Option "4" to quit.

echo.
echo.

choice.exe /C 12345 /N /M "Your choice:"

IF ERRORLEVEL == 255 GOTO FAIL
IF ERRORLEVEL == 5 GOTO TEST
IF ERRORLEVEL == 4 GOTO END
IF ERRORLEVEL == 3 GOTO OPEN
IF ERRORLEVEL == 2 GOTO SHOW
IF ERRORLEVEL == 1 GOTO HIDE

echo.
echo.

pause

:HIDE
ECHO Give the pathname:
set /p n=

attrib +s +r +h %n%

echo Object succesfull hidden!

%n% > test.txt

echo The pathname is saved in test.txt
pause
cls

GOTO A

:SHOW
ECHO Enter the path:
echo Tip: Check test.txt
set /p SHOW=

attrib -s -r -h %SHOW%

Echo Object restored !

pause
cls

GOTO A

:OPEN

test.txt

pause
cls

GOTO A

:TEST

::
::setlocal
::for /F "delims=" %%j in ('type "%*"') do set line=%%j
::echo.%line%> "%*"
::

echo test

pause
cls

Goto A

:FAIL

echo Error ! Impossible choise.
echo CMD closes automatically in

Sleep 10

cls

end


See More: Delete/add lines in txt file with cmd

Report •

#1
November 19, 2010 at 13:34:38
you can use findstr command and recreate the test file.

Findstr /v prints all the lines which does not contain the match.
Pipe the output of findstr to a temp file, delete the original test.txt and rename the temp file to test.txt which does not have the match (%N) variable in your case.


findstr /v %n test.txt >test_temp.txt
erase /f test.txt
ren test_temp.txt test.txt

Subhash Chandra.


Report •
Related Solutions


Ask Question