Batch: random
Solved
mael9
Posted messages
373
Registration date
Status
Member
Last intervention
-
CARCY -
CARCY -
Bonjour,
I would like a .bat file to automatically open on Windows startup and write a phrase stored in this .bat randomly (different each startup if possible).
I would like an example like this:
::::: bat file editing ::::::
phrases:
"my phrase 1"=1; "my phrase 2"=2; "my phrase 3"=3...
startup: display 1
next startup: previous phrase +1
following startup: previous phrase +1 (I think it should be done this way)
if it's not possible to do that (one different each startup, the random function would work for me)
Thank you for your help!
I would like a .bat file to automatically open on Windows startup and write a phrase stored in this .bat randomly (different each startup if possible).
I would like an example like this:
::::: bat file editing ::::::
phrases:
"my phrase 1"=1; "my phrase 2"=2; "my phrase 3"=3...
startup: display 1
next startup: previous phrase +1
following startup: previous phrase +1 (I think it should be done this way)
if it's not possible to do that (one different each startup, the random function would work for me)
Thank you for your help!
16 answers
Here’s something I made quickly.
So the file phrase.txt contains a lot of sentences:
And then the .bat file that contains the commands.
You need to put the phrase.txt and the .bat in the folder, next to each other.
Sincerely,
Bilou.
Some days you shouldn't mess with me.
And there are days every day!
So the file phrase.txt contains a lot of sentences:
Hello, how are you? Hello to you Hello man !! BlaBlaBli Carrot?
And then the .bat file that contains the commands.
@echo off setlocal enableDelayedExpansion ::Total number of phrases. set phraseTotal=0 for /f %%i in ('type "phrase.txt"') do set /a phraseTotal+=1 ::Randomly drawing one of the phrases. call set /a LinePhrase=%%random%%%%%%%phraseTotal% ::Extracting the phrase. set line=0 for /f "delims=""" %%i in ('type "phrase.txt"') do ( if !line!==!LinePhrase! set phrase=%%i set /a line+=1 ) ::Displaying the phrase msg * %phrase% You need to put the phrase.txt and the .bat in the folder, next to each other.
Sincerely,
Bilou.
Some days you shouldn't mess with me.
And there are days every day!
Hello
I don't think a single batch can display messages at each startup, but it becomes possible if the batch itself is modified
for the commands
AT /?
For /?
if /?
echo /?
Goto /?
so you still have to improve the commands
I don't think a single batch can display messages at each startup, but it becomes possible if the batch itself is modified
for the commands
AT /?
For /?
if /?
echo /?
Goto /?
so you still have to improve the commands
Alt+130 yes, but in my file phrase.txt, there are accents for example:
There is no theory of evolution. Just a list of species that Chuck Norris allows to survive.
theory: é
species: è
(Chucknorrisfacts)
Well, the message that appears does not display the é or the è or the à... or ë ä â ê....
it puts a U with an accent instead.... how to fix this?!
There is no theory of evolution. Just a list of species that Chuck Norris allows to survive.
theory: é
species: è
(Chucknorrisfacts)
Well, the message that appears does not display the é or the è or the à... or ë ä â ê....
it puts a U with an accent instead.... how to fix this?!
yes
if I only do echo é then you see the results directly in the prompt, but adding > is for redirection to a file (in your case it's mael9.txt) to, for example, see the results on your desktop under the name coucou.txt so type echo é >Desktop\coucou.txt
in the second line the double redirection (>>) is to avoid overwriting the first result with the second
I think you understand
if I only do echo é then you see the results directly in the prompt, but adding > is for redirection to a file (in your case it's mael9.txt) to, for example, see the results on your desktop under the name coucou.txt so type echo é >Desktop\coucou.txt
in the second line the double redirection (>>) is to avoid overwriting the first result with the second
I think you understand
so it's good
I noticed that there is no problem
you say:
echo. I hope he will be home. > cat.txt
he writes this
I hope he will be home ...
so it's good because another execution of your 1st example on the file cat.txt instead of phrase.txt works very well
I think you don't quite understand what I said
I noticed that there is no problem
you say:
echo. I hope he will be home. > cat.txt
he writes this
I hope he will be home ...
so it's good because another execution of your 1st example on the file cat.txt instead of phrase.txt works very well
I think you don't quite understand what I said
Reuh mael9,
With the code I gave you, the characters should normally display correctly as they appear in a dialog box.
What you're doing is writing to a text file using DOS, for that you need to do:
In PowerBatch when you type an é it gives you an ,
So copy the , to replace the é in your bat file opened with Notepad.
Or you can save directly with PowerBatch
Best regards,
Bilou.
Some days you shouldn't mess with me.
And some days are every day!
With the code I gave you, the characters should normally display correctly as they appear in a dialog box.
What you're doing is writing to a text file using DOS, for that you need to do:
In PowerBatch when you type an é it gives you an ,
So copy the , to replace the é in your bat file opened with Notepad.
Or you can save directly with PowerBatch
Best regards,
Bilou.
Some days you shouldn't mess with me.
And some days are every day!
Well, topic resolved!
I already had Powerbatch and I never noticed that it directly transforms the letters with accents u_u'!
I already had Powerbatch and I never noticed that it directly transforms the letters with accents u_u'!
Unresolved topic again,
how to create the same script under Linux (Ubuntu)? It doesn't recognize certain commands... setlocal...@echo off...
how to create the same script under Linux (Ubuntu)? It doesn't recognize certain commands... setlocal...@echo off...
Re, hi
in Ubuntu there is the bash shell (there is no @)
for more info, check out the Zero site
https://openclassrooms.com/fr/courses
in Ubuntu there is the bash shell (there is no @)
for more info, check out the Zero site
https://openclassrooms.com/fr/courses
I have a long script of 23 lines (not counting comments) (modified)
Between start modified part and end modified part the old code is
if not exist save.txt echo 1>save.txt
set /p number=<save.txt
@echo off set phrase1=First phrase set phrase2=Second phrase set phrase3=Third phrase set phrase4=blablabla set phrase5=Hello world set phrase6=... set phrase7=blablabla set phrase8=blablabla set phrase9=xox set phrase10=ten ::start modified part set /a number=(%random%%%10)+1 ::end modified part if %number%==1 echo %phrase1% if %number%==2 echo %phrase2% if %number%==3 echo %phrase3% if %number%==4 echo %phrase4% if %number%==5 echo %phrase5% if %number%==6 echo %phrase6% if %number%==7 echo %phrase7% if %number%==8 echo %phrase8% if %number%==9 echo %phrase9% if %number%==10 echo %phrase10% pause >nul
Between start modified part and end modified part the old code is
if not exist save.txt echo 1>save.txt
set /p number=<save.txt
Who runs on Windows... not on Linux :-/ but does not allow "random" and what is the purpose of the file "save.txt"?
Hello mael9
The random number in bash linux is the command RANDOM, so try using the help of the command RANDOM --help
I respond for the file save.txt
In the line:
if not exist save.txt echo 1>save.txt
If the file save.txt does not exist, then display a "1" and save it to the file save.txt
set /p nombre= <save.txt
The command set /p nombre= allows waiting for input from the keyboard, but there is after this command the <save.txt in this case we know that the number 1 is stored in this file and while there is a reverse redirection (the < less than) so the content of this file is that of the input for the command SET /P nombre= i.e. as if we directly typed the number from the numeric keypad of our keyboard 1
The random number in bash linux is the command RANDOM, so try using the help of the command RANDOM --help
I respond for the file save.txt
In the line:
if not exist save.txt echo 1>save.txt
If the file save.txt does not exist, then display a "1" and save it to the file save.txt
set /p nombre= <save.txt
The command set /p nombre= allows waiting for input from the keyboard, but there is after this command the <save.txt in this case we know that the number 1 is stored in this file and while there is a reverse redirection (the < less than) so the content of this file is that of the input for the command SET /P nombre= i.e. as if we directly typed the number from the numeric keypad of our keyboard 1
Thank you, I will try
EDIT :
With this code, here is the error :
/home/ubuntu/Desktop/fichier.bat: line 1: @echo: command not found
/home/ubuntu/Desktop/fichier.bat: line 12: Syntax error near unexpected token « ( »
/home/ubuntu/Desktop/fichier.bat: line 12: 'set /a nombre=(%random%%%10)+1 '
ubuntu@ubuntu:~$ /home/ubuntu/Desktop/fichier.bat
EDIT :
With this code, here is the error :
/home/ubuntu/Desktop/fichier.bat: line 1: @echo: command not found
/home/ubuntu/Desktop/fichier.bat: line 12: Syntax error near unexpected token « ( »
/home/ubuntu/Desktop/fichier.bat: line 12: 'set /a nombre=(%random%%%10)+1 '
ubuntu@ubuntu:~$ /home/ubuntu/Desktop/fichier.bat
I have already replaced the total number of phrases value but I don't see what else I need to do. (both files are indeed in the same folder)
Thank you in advance =D
your file extension must be .bat or .cmd and not .txt
It was already the case, but when I run it, it shows me:
"Enter message to send; CTRL+Z on new line to end message, then ENTER"
And whatever I write, cmd.exe closes once I press ctrl+z then enter, and I see no changes anywhere.
add the command pause at the end of your batch (just after the command msg * %phrase%)
But it still doesn't work ^^'
Now it shows me "Error 1702 while retrieving session names"
I'm going to do a Google search for this famous error but I would appreciate some insights here too ;)