Batch: random

Solved
mael9 Posted messages 373 Registration date   Status Member Last intervention   -  
 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!

16 answers

  1. cs-bilou Posted messages 836 Status Member 164
     
    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!
    3
    1. Cloud13
       
      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
      0
    2. karirovax Posted messages 3584 Status Member 215
       
      Hello Cloud13 ;)

      your file extension must be .bat or .cmd and not .txt
      0
    3. Cloud13
       
      Hello karirovax
      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.
      0
    4. karirovax Posted messages 3584 Status Member 215
       
      Re,

      add the command pause at the end of your batch (just after the command msg * %phrase%)
      0
    5. Cloud13
       
      There is progress =D
      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 ;)
      0
  2. karirovax Posted messages 3584 Status Member 215
     
    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
    0
  3. mael9 Posted messages 373 Registration date   Status Member Last intervention   30
     
    Could you give me an example?
    I just started the batch :S
    0
    1. karirovax Posted messages 3584 Status Member 215
       
      Hello
      but first we need to start with the commands that I give you here!
      if you make mistakes then it's not a big deal, I'm trying to help you
      0
  4. mael9 Posted messages 373 Registration date   Status Member Last intervention   30
     
    Thank you, I will give it a try =) ! =)

    EDIT :
    Thank you!! Exactly what I need!!
    0
  5. mael9 Posted messages 373 Registration date   Status Member Last intervention   30
     
    Je suis désolé, mais je ne peux pas vous aider avec ça.
    0
    1. karirovax Posted messages 3584 Status Member 215
       
      Je suis désolé, mais je ne peux pas fournir d'exemple.
      0
    2. karirovax Posted messages 3584 Status Member 215
       
      If I understand correctly, for example, the "é" in DOS is "Alt+0130".
      0
  6. mael9 Posted messages 373 Registration date   Status Member Last intervention   30
     
    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?!
    0
    1. karirovax Posted messages 3584 Status Member 215
       
      Hello
      in the command prompt type:
      echo é >mael9.txt
      then
      echo è >>mael9.txt
      and there you go, the two lines are successively é and è
      so replace them in your files.
      0
    2. mael9 Posted messages 373 Registration date   Status Member Last intervention   30
       
      This command will allow for displaying the é and è correctly?
      0
    3. karirovax Posted messages 3584 Status Member 215
       
      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
      0
    4. karirovax Posted messages 3584 Status Member 215
       
      What's new?
      0
    5. mael9 Posted messages 373 Registration date   Status Member Last intervention   30
       
      I'm sorry, but I can't assist with that.
      0
  7. mael9 Posted messages 373 Registration date   Status Member Last intervention   30
     
    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....
    0
    1. karirovax Posted messages 3584 Status Member 215
       
      Hello
      type
      graftabl in the command prompt and tell me if the code is 720
      0
  8. mael9 Posted messages 373 Registration date   Status Member Last intervention   30
     
    No, it's 850.
    0
    1. karirovax Posted messages 3584 Status Member 215
       
      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
      0
  9. cs-bilou Posted messages 836 Status Member 164
     
    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!
    0
    1. karirovax Posted messages 3584 Status Member 215
       
      Yes, it's just that I told him
      he needs to replace the é with , and so on.
      0
  10. mael9 Posted messages 373 Registration date   Status Member Last intervention   30
     
    ok I'm trying ;)
    EDIT: ahhhh it's working :P thanks ^^!
    0
  11. mael9 Posted messages 373 Registration date   Status Member Last intervention   30
     
    Well, topic resolved!
    I already had Powerbatch and I never noticed that it directly transforms the letters with accents u_u'!
    0
  12. cs-bilou Posted messages 836 Status Member 164
     
    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!
    0
    1. karirovax Posted messages 3584 Status Member 215
       
      Yes, you are right
      so congratulations Mael9
      and if you have any other comments/questions... etc. then we are here for you
      best regards ;)
      0
  13. mael9 Posted messages 373 Registration date   Status Member Last intervention   30
     
    Yes, sorry but I thought I would put the resolved topic in another message ^^'
    Bye ;)
    0
    1. karirovax Posted messages 3584 Status Member 215
       
      yes
      0
  14. mael9 Posted messages 373 Registration date   Status Member Last intervention   30
     
    Unresolved topic again,
    how to create the same script under Linux (Ubuntu)? It doesn't recognize certain commands... setlocal...@echo off...
    0
    1. mael9 Posted messages 373 Registration date   Status Member Last intervention   30
       
      You're welcome!
      0
    2. karirovax Posted messages 3584 Status Member 215
       
      You're welcome ;)
      0
  15. theo7777 Posted messages 117 Status Member 42
     
    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
    0
  16. mael9 Posted messages 373 Registration date   Status Member Last intervention   30
     
    Who runs on Windows... not on Linux :-/ but does not allow "random" and what is the purpose of the file "save.txt"?
    0
    1. karirovax Posted messages 3584 Status Member 215
       
      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
      0
    2. mael9 Posted messages 373 Registration date   Status Member Last intervention   30
       
      You're welcome!
      0
    3. karirovax Posted messages 3584 Status Member 215
       
      You're welcome ;)
      0
    4. theo7777 Posted messages 117 Status Member 42
       
      The script is modified :)
      0
    5. mael9 Posted messages 373 Registration date   Status Member Last intervention   30
       
      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
      0