How to delete text from a text file via batch - batch-file

Have looked about the web but cant find anything that can help. I am trying to make a page that can delete something of a page. Have managed to make a search that can tell you if a word is on the .txt file. How would I put it into this?
cls
set /p SEARCH= Please enter what you want to look for?
for /f "Delims=" %%a in (test.txt) do (
set FIND=%%a
)
if %SEARCHS%==%FIND% goto FLIGHT HAS ARRIVED
echo We were not able to find %SEARCH%
pause
goto start
Does anybody know what I should do?
Thanks,
Alex

type file|find "string in lines to remove" /i /v >newfile
might help.
set /p reg=text:for /f "delims=" %%i in (file.txt) do if %%a'==%reg%' goto found
I can't tell what you want to do though so if this doesn't help please elaborate.

try this:
#ECHO OFF &SETLOCAL
set /p "SEARCH= Please enter what you want to look for? "
for /f "Delims=" %%a in ('find "%SEARCH%" test.txt') do set "FOUND=%%a"
if DEFINED FOUND goto FLIGHT_HAS_ARRIVED
echo We were not able to find %SEARCH%
pause
goto start
:FLIGHT_HAS_ARRIVED
ECHO %SEARCH% found: %FOUND%
goto:eof
You shouldn't use cmd command names for variable names (%FIND%).
Jump labels do not work as expected, if it contain spaces: FLIGHT HAS ARRIVED

Related

List Directory and number of files with specific extension, build a specific menu

I am trying to create a menu with submenus which their names without extension come from the directory. However, I am unable to make a variable for choice as number. This code does not work anyhow. I would also want to display a number at the beginning of each file name in the menu; in fact, number of the files will also be one of the number that user selects as input. I could not overcome the problem.
#echo off
cd C:\Users\Murray\Documents\ConfigFiles\
for /f %%A in ('dir /a-d-s-h /b *conf ^| find /v /c ""') do set count=%%A
echo File count = %count%
for %%F in ("C:\Users\Murray\Documents\ConfigFiles\*.conf") do echo %%~nxF
set choice=
set /C /N="Please choice: "
if "%choice%" == "%count%" goto SUBMENU
if NOT EXIST "C:\Users\Murray\Documents\ConfigFiles\%choice%" goto NOFILE
:SUBMENU
Echo You are here
goto end
:NOFILE
echo %choice% could not be found.
goto END
:end
Any help will be appreciated.
Here's a quick example of a method touted in the answer by on no. It will print a number to the screen followed by the names of each file matching file extension, so will allow a large number of matching files, (although the end user may have to scroll a long list). Once a file is chosen, your end user then just types its corresponding number. The code should not proceed beyond the input request until a number matching one in the list is ENTERed.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "src=C:\Users\Murray\Documents\ConfigFiles"
Set "ext=.config"
If Not Exist "%src%\*%ext%" Echo No file matches.& GoTo End
For /F "Delims==" %%G In ('"(Set #) 2>NUL"') Do Set "%%G="
For /F "Delims==" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe OS Call /? ^|
%SystemRoot%\System32\find.exe "=="') Do Set "HT=%%G"
For /F "Tokens=1* Delims=:" %%G In ('Dir /B /A:-D "%src%\*%ext%" 2^>NUL ^|
%SystemRoot%\System32\findstr.exe /E /I /L /N "%ext%"'
) Do Set "#%%G=%%H" & Echo( %%G.%HT:~-1%%%H
If Not Defined #1 Echo No file matches.& GoTo End
:Opt
Echo(
Set "HT=" & Set "opt="
Set /P "opt=Enter the number for your chosen file>"
If Not Defined opt (GoTo Opt) Else Set "opt=%opt:"=%"
Set # | %SystemRoot%\System32\findstr.exe /B /L "#%opt%=" 1>NUL || GoTo Opt
SetLocal EnableDelayedExpansion
For %%G In ("!#%opt%!") Do EndLocal & Set "opt=%%~G"
#Rem Your code goes below here
Echo(& Echo You Selected %opt%
#Rem Your code ends above here
:End
Setlocal EnableDelayedExpansion & For /F "Tokens=1,2" %%G In ("!CMDCMDLINE!"
) Do Endlocal & If /I "%%~nG" == "cmd" If /I "%%~H" == "/c" Echo(& Echo Press^
any key to exit.&Pause 1>NUL
All you need to do is to modify the variables values on lines 4 and 5, if necessary, in order to test it. I will not be supporting changes or additions beyond that. Once you have tested the code, you may insert your code between lines 25 and 29, replacing the example line I left there for your test.
Batch's limitations are to blame for sub-par corrections to this question.
Here's the best I could whip up in a few seconds:
#echo off
:one
cls
cd C:\Users\Murray\Documents\ConfigFiles\
for %%F in ("C:\Users\Murray\Documents\ConfigFiles\*.conf") do echo %%~nxF
echo.
echo Enter Configuration Name:
set/p "prompt=>"
if exist %prompt%.txt goto :two
if exist %prompt% goto :two
cls
echo File not found
pause >NUL
goto :one
:two
cls
REM When file is found, this code will run
pause >NUL
The set command does not prompt for user input unless specified with the /p switch. To make your code more friendly, i'd also recommend to prompt for the filepath earlier on in the code.
EDIT: A few alternative solutions: Declare the 9th option of the prompt of the choice command as a "second page" or "more" option. This would really be a pain for the user in a directory with tens or hundreds of files. Another; you can assign an integer to each file that matches your quasi-query and echo them before each filename on the screen, then allow the user to input the number to get that file. That seems fairly efficient, and if you'd like to explore one or both of those alternates I can help (if you need it).

Error when using findstr on file directory

So I'm trying to learn the ins and outs of findstr since it's come up a few times on other batch script questions I've had. I'm trying to have it look for a word (in this case 'webview') through multiple files in a directory, ideally it would pull the line it was found as well as the file name. However, the program gets stuck in this sort of infinite loop with it and I have to force an exit. Any help on if its being caused by the findstr or what is causing it would be amazing as I've been staring at it for several hours now. My current code is below:
ECHO off
SETLOCAL enabledelayedexpansion
ECHO Please input the path to the app directory you'd like scanned
SET /p directorypath=
CD %directorypath%
ECHO Scanning files for Webview
(
FOR /F "delims=" %%a in ('findstr /I /S /M "webview" *.json') DO (
SET "line=%%a"
SET "line=!line:*webview=!"
FOR /F "delims=<" %%b in (!line!) DO ECHO %%b
)) > WebviewScanResults.txt
:eof
UPDATE: Code updated and functional for use as a reference. I pretty much just run the above code a couple of time with different file types replacing *.json and it works fine.
Just an untested try, too late for me:
#ECHO off
SETLOCAL enabledelayedexpansion
ECHO Please input the path to the app directory you'd like scanned
SET /p directorypath=
PushD "%directorypath%"
ECHO Scanning files for Webview
(
FOR /F "tokens=1*delims=:" %%a in ('findstr /I /S "webview" *.html') DO (
SET "line=%%b"
SET "line=!line:*webview=!"
FOR /F "delims=<>" %%b in ("!line!") DO ECHO %%a:%%b
)) > WebviewScanResults.txt

Batch - For /F with IF/File/Comparison

Dears,
I'm hoping you can help me, because I lost my faith.
Let me show you what I have done, and then explain what I need to achive.
#echo off
echo "Which Site do you want to check?"
set /p ans="Site: "
echo.
for /f "tokens=1,2,3 delims=," %%A IN (animals.csv) DO if %%A==%ANS% (ping %%B AND ping %%C)
echo.
pause
echo.
cls
goto menu
User should put something animal name which will be stored in ANS variable.
Script should check in animals.csv what IP adresses are assigned for that animal.
After matching, script should ping IP adresses in next columns, so user could see results.
PS: All both files are in the same folder.
Problem is that animals.txt has 3 columns and a lot of records.
I was able make script work till point of comparing variables, then I stopped working.
Let's say that animals file looks like that:
cat,111.111.111.111,122.122.122.122
dog,222.222.222.222,233.233.233.233
horse,333.333.333.333,344.344.344.344
Help me, because I stuck and I have no damn clue how to do it.
Your script works, when you replace AND with & (see SS64.com)
But let me suggest a slightly different logic:
#echo off
echo "Which Site do you want to check?"
set /p ans="Site: "
echo.
for /f "tokens=1,2,3 delims=," %%A IN ('type animals.csv^|findstr /b "%ans%,"') DO (
ping %%B
ping %%C
)
echo.
pause
findstr parameter /b means "string should start with" - prevents maddog to be recognized as dog.
The trailing comma in the searchstring prevents catfish to be recognized as cat.
You might want to add /i to make the search case-insensitive (cat and Cat will be recognized).

batch script to compare list files

I have a list in text file
abc
abc_1
abc_2
cbd
cbd_1
cbd_2
And theres alot more to the lines as well like dates and other randomness. What I want to do is for a script to like for each file, but when it looks for a file it will compare to another text for for that same file name. And then if it doesn't find it, write it to that text and continue with a full search. The part of the script I am stuck on...says do was unexpected at this time.
for /f "delims=" %%a in (C:\conflicts\vids.txt) DO for /f "tokens=1 delim=_" %%i ("%%a") do (
find %%i in (C:\conflicts\exists.txt) do (
if exist goto :end
echo %%i >> C:\conflicts\exists.txt
) else if exist \\networklocation\%%i.mpg (
ECHO FOUND %%i IN NETWORKLOCATION >> C:\conflicts\vids1.txt
... DO for /f "tokens=1 delim=_" %%i ("%%a") do
missing 'IN' probably generating your 'unexpected do'
... if exist goto :end
If exist what goto end?
if exist looks for a match. This would look for a file named 'goto' and if found would execute `:end'
You've only posted a partial statement - there are missing round-brackets and who-knows-what else.
Please give examples to demonstrate what you are trying to achieve. Your torrent of pronouns is unfortunately, indecipherable.
try this out .. maybe it does what you want ..
for /f "tokens=*" %%c in (your text file here) do (
cd\
for / r %%f in (*) do (
if %%c==%%~nf echo file name match found %%f
pause
)
)
after the if statement you can put any command you wish depending on what you wanna do with the results ....
hope this helps out ..

How to input batch variable information into a website?

I am working on a decoder, and as of now, I have the main part. I was wondering if you could set a variable in batch (via set /p) and put that variable into a website's input box. Then how would I continue to decode.
#echo off
setlocal EnableDelayedExpansion
Set /P Input=[Enter Input Here]
for /F "tokens=1,2 delims=()" %%a in ("%input%") do (
set bit=%%a
set "output="
for /F "delims=" %%c in ('cmd /D /U /C echo %%b^| find /V ""') do (
for /L %%i in (1,1,%%c) do set "output=!output!!bit!"
set /A "bit=^!bit"
)
)
echo %output%
How would I put output into the "binary to decode" section and put click the "to text" button from batch, then echo the txt output from the website. If this includes vb or vbs, you would have to include the program for me. I don't know any vb/vbs. Here is the site: http://www.roubaixinteractive.com/PlayGround/Binary_Conversion/Binary_to_Text.asp
Why are you even asking this question? It isn't possible. I know everything in batch and HTML. If you want to do that, you can do it in PHP.

Resources