How to create batch files with variable

Asked

Viewed 413 times

2

Hello, I don’t know anything about programming. I found out how to create scripts . bat and I’m using it to make my life easier. Every day I have to open several web pages in my work, so I created a file. bat using the START command to open all the pages I need at once. It’s working. Turns out two of those pages I need to open only on Fridays. I need to create a script with an 8-page list, but for two of them I want him to check if the current day is Friday and only open the page if it is Friday, otherwise it will open only the other pages that do not have this condition. Does anyone know how I can do?

  • Please edit the question and add the code you already have today.

  • you want to know how to make the variable or use the value of the variable?

2 answers

1


Face batch is very old, does not have many features. So recommend you see the Powershell, you will be able to automate a lot of things with this. I’m even using Powershell to catch the day of the week:

FOR /F "delims=" %%i IN ('powershell "[Int] (Get-Date).DayOfWeek"') DO set dia=%%i

What you need is a conditional command IF, that will only execute a command block if a condition is true:

@ECHO OFF

FOR /F "delims=" %%i IN ('powershell "[Int] (Get-Date).DayOfWeek"') DO set dia=%%i

IF %dia%==5 (
    ECHO Hoje e Sexta!
    ECHO Abrir mais duas paginas...
)

ECHO Tudo o que preciso todos os dias!

You can also use the IF-ELSE that will execute whatever is inside the ELSE if the condition is false:

@ECHO OFF

FOR /F "delims=" %%i IN ('powershell "[Int] (Get-Date).DayOfWeek"') DO set dia=%%i

IF %dia%==5 (
    ECHO Hoje e Sexta!
    ECHO Abrir mais duas paginas...
) ELSE (
    ECHO Hoje nao e Sexta!
    ECHO Caso que queira abrir alguma coisa...
)

ECHO Tudo o que preciso todos os dias!

And as today is Friday, the way out:

saída

0

The same solution with Powershell:

First you will run Powershell as administrator and enter the command

Set-ExecutionPolicy Unrestricted

This will allow you to run the scripts on your computer.

Then you’ll open the Windows Powershell ISE, that an editor coming in windows for scripts:

And the code is:

$dia = [Int] (Get-Date).DayOfWeek
if ($dia -eq 5) {
    Start "https://www.google.com/search?q=happy%20hour%20promoção"
} else {
    Start "https://jornaldoempreendedor.com.br/destaques/10-dicas-para-relaxar-seu-cerebro-enquanto-trabalha/"
}
Start "http://fazenda.gov.br"

psise

Green arrow runs to test, saved in one place and to run just use the right button and mantar run as Powershell

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.