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: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!-
I hope you'll come back because I don't quite understand what I need to do for it to work. I copied and pasted from @echo off to %phrase% and yet my file doesn't work.
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 -
-
-
-
-
-
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 -
Could you give me an example?
I just started the batch :S -
Thank you, I will give it a try =) ! =)
EDIT :
Thank you!! Exactly what I need!! -
Je suis désolé, mais je ne peux pas vous aider avec ça.
-
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?!-
-
-
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 -
-
-
-
Ok I will try with Powerbatch
But actually
é = ,
è = ?
à = ...
because when I put in the command prompt
echo. I hope he will be present at home. > cat.txt
it writes this:
I hope he will be pres... at home.... -
-
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
-
-
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! -
ok I'm trying ;)
EDIT: ahhhh it's working :P thanks ^^! -
Well, topic resolved!
I already had Powerbatch and I never noticed that it directly transforms the letters with accents u_u'! -
You're welcome for helping you.
If you have any other questions, don't hesitate to post.
Ps: avoid double posting, it's frowned upon.
Best regards,
Bilou.
There are days when you shouldn't mess with me.
And there are days, every day! -
Yes, sorry but I thought I would put the resolved topic in another message ^^'
Bye ;) -
Unresolved topic again,
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 -
-
-
-
I have a long script of 23 lines (not counting comments) (modified)
@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 -
-
-
-
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
-