how to detect change in .txt file using batch? - file

I made a simple LAN chat batch file, and i would like to know if
there is an command that checks if a specific txt file is updated.
The chat history is stored in log.dat and i want to have a sound notification or something like that when theres a new message.
#echo off
title LAN chat reader
call Color.bat
:read
call Color.bat
cls
type log.dat
timeout /t 3 /nobreak >nul
goto read
(im a noob, please tell me if this is possible)

To check the file date/time use for and %%~t prefix:
#echo off
title LAN chat reader
setlocal enableDelayedExpansion
:read
call Color.bat
cls
type log.dat
for %%a in (log.dat) do set filetimesize=%%~tza
:checkupdate
ping -n 1 -w 100 localhost >nul
for %%a in (log.dat) do if "!filetimesize!"=="%%~tza" goto checkupdate
echo updated
goto read

wOxxOm already gave a solution to check for an updated file.
Here is a way to produce a Sound:
copy con bell.txt
Then press the ALT-key enter 007 while keeping ALT pressed, then release the ALT key. ^G should appear on your Screen (= 0x07, which is a Bell), then press Ctrl-Z. This gives you a textfile with lenght = 1 Byte
Type bell.txt
will then beep.
EDIT an easier way to produce bell.txt: on commandline, enter echo ^G>bell.txt (to produce ^G press CTRL-G). This will create a three-byte-file (instead of the one-byte-file with the copy trick) (but that's only a line feed and should not disturb).

Related

Giving an input inside a batch file which replaces the input with a specific string from a other file

Basically I just want to code a batch script that ask me for a input after running it, hitting enter after the text the Text that I wrote will replace it with the old text in a mentioned lined in the sc. I am using windows11 and never tried smth like this, it would be nice if someone can show me how to do it and explain the commands too so that I won't ask again for stuff like this. This is my current code, I just want to add a option 3 which asks for a input and replaces my input in a other file at a specific string or behind a specific words (max_items: [my input] in my case).
#echo off
title Changing Configs..
color B
:main
echo 1 = grind time
echo 2 = trade time
set /p ibo=
if %ibo% == 1 goto grind
if %ibo% == 2 goto trade
:grind
ren "config.yml" "trading"
ren "grinding" "config.yml"
exit
:trade
ren "config.yml" "grinding"
ren "trading" "config.yml"
exit

batch-file - creating a file with file extension

I am a student and new here in Stack Overflow.
I am currently creating a project for my Operating System class. One of the menu would be this: The user can create, rename and delete a certain file anywhere in the directory. I already know how to rename and delete a file but I had a hard time finding the right command to create a file wherein the user can determine what file extension that certain file would be.
It is sort of like this:
//this is to show the user what available drives the computer has
echo Here are the available drives:
echo.
wmic logicaldisk get name
echo.
echo.
set /p ch2=Which drive do you want to access?
for /f "skip=1 delims=" %%x in ('wmic logicaldisk get caption') do #echo %%x >>drive%%x.txt
if exist D:\drive!ch2! (
goto specificview
)
else (
goto Invalid
)
//here the chosen drive will be viewed with its saved files and the user is asked what to do next
:specificview
cls
set ch3=
dir !ch2!:\ /w /a
pause
echo.
echo.
echo What do you want to do in this directory?
echo ================================================================
echo Press [X] to go back to choose directory
echo Press [w] to Exit
echo Press [C] to create File
echo If none of the choices, type down the name of the directory file
echo NOTE: Just press [1] to go back to the main MENU
echo ================================================================
//let's say the user will choose C to create file
set /p ch3= Please Choose:
if /i !ch3!==c (
set /p crt=Input the name of the filename:
set /p ext=Input the desired extension:
Now, I don't know what command I should use in order to complete the code. I would like to ask your help so that I can finish my project.
Thanks a lot in advance.
Not really comment, not really answer so I will make it CW!
As this question is not really a duplicate I will add a few things to change it to your needs.
I assume that your file is going to be empty and not using some special formatting or encoding.
As already visible in the answers to above question, there are a lot of ways to do that. I would suggest the answer with the most upvotes that copies NUL to a new file making it empty.
The copy command as you can see is quite simply structured: copy firstFile.something secondFile.somethingElse or in this case copy NUL fileName.ext
So in your case you would go ahead and add the following line:
copy NUL !crt!.!ext!

Batch script to move to another section of the script if there is no input from user

I am trying to get this script to jump to another section of the script if there is no input from the user.
Down at the if %input%== area.
What I'm trying to do is skip to the section where the script checks for .mp4 files and moves them if they are there. Am I supposed to set a variable or loop for that section? Thanks for any replies
#echo off
echo Checking for youtube-dl updates.
pause
youtube-dl -U
rem Enter the url or urls that you want to download from
set /p input="Enter the url(s) you want to download:"
rem Uses the youtube-dl continue (-c) option to download multiple files if not in a playlist
youtube-dl -c "%input%"
rem pause
if %input%=="" GOTO:EOF
cls
echo Download complete, please wait while files are transfered to appropiate folder
pause
for %%o in (.mp4) do move "*%%o" "E:\Documents\scripts\videos\"
if not exist do echo .mp4 files are no longer in this directory
pause
How about following script? This script waits for 3 seconds while "file.mp4" doesn't exist. It keeps to wait until the file exists. About "filename", you can change for your script.
#echo off
set waittime=3
set filename=file.mp4
:loop
if not exist %filename% (
Timeout /t %waittime% /nobreak > nul
goto loop
)
echo Find %filename%
When doing string comparison in batch you have to make sure, that both parts are equal, which in your case will never happen! In most languages strings have double quotes around them. In batch they usually do not.
To solve your problem enclose %input% in double quotes as well.
Note that it can be useful to do something like "x%input%"=="x" to prevent certain characters like <>|to be at the beginning of the comparison string.
You can check this on your own with these few lines:
#echo off
set /p input="Input something or nothing here "
echo %input%
echo "%input%"
pause
If you are hitting Return without any input you will see that only the bottom one will output "" which is the string you are comparing to.

Catching keystroke during loop in batch

As a small training for my batch programming I originally followed the idea to make up a small slotmachine. You can give your coins, the numbers rush through and when you hit a button, the numbers should stop at their current value. If they are all the same, boom, you won the jackpot!
My problem right now is the keystroke capture during the loop. I already thought something like a choice command, but then the program would stop at each loop waiting for keyboard input, not making the game just quite annoying to wait all the time, but as well boring as you could check if you want to hit a specific button to stop.
Another thought was putting
set /p foobar=
and then simulate an Enter-Stroke with !SendKeys! (with everything neccessary in the code), forgetting that the enter yould have been sended after input...
Is there a way to accomplish that in ONE batch file? Or do I have to come up with another one to simulate the keystrokes or is there anything else I have missed?
EDIT: To clearify: Is there any command that changes something on keystroke, but runs through if nothing is touched?
Thanks for help in advance!
Greetings
geisterfurz007
#echo off
setlocal EnableDelayedExpansion
rem Create an empty file
cd . > key.txt
rem Start a parallel process that wait for Enter key
rem and add a line to the empty file
start "" /B cmd /C "set /P = & echo line >> key.txt"
set "key="
:wait
cls
set "number=%random:~-4%"
echo %number%
echo/
echo Press Enter key to stop the numbers...
set /P key=< key.txt
if not defined key goto wait
echo The last number is: %number%

Problems in my simple chat program

I made a simple batch chat that write the message to an txt file.
I need help with print the file every time it changed and with hidding output.
I used the type, delay and cls to print the file but it didnt work, it didnt printed the file.
launcher.bat:
start cmd /k
call room.bat
call chat.bat
using the launcher photo
room.bat(the problem):
:chat1
cls
TYPE room.txt
timeout /t 0.5
goto chat1
chat.bat(working but show extra info about the os and the file):
#echo off
cls
set D=%Date%
cls
echo enter your name
SET /P name=[name]
pause
:room
cls
SET /P chatpublic=[everyone]
SET "
echo %name%: %chatpublic% |%D%|>> room.txt
pause
goto room
without the launcher photo
Here's the culprit:
echo %name%: %chatpublic% |%D%|>> room.txt
That's because the | vertical line has a special meaning in Windows command line interpreter: commandA | commandB: pipe the output from commandA into commandB.
If you do want use a | vertical line in another sense (e.g. display it with an echo command), then you should escape it as follows:
echo %name%: %chatpublic% ^|%D%^|>> room.txt

Resources