How to create batch files with variable

1

Hello, I do not understand anything about programming. I have discovered how to create .bat scripts and I am using it to make my life easier. Every day I have to open several internet pages in my work, so I created a .bat file using the START command to open all the pages I need at once. It's working. It turns out that two of these pages I need to open only on Fridays. I need to create a script with a list of 8 pages, but for two of them I want it to check if the current day is Friday and just 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 it?

    
asked by anonymous 30.11.2018 / 00:48

2 answers

1

Batch face is very old, does not have many features. So I recommend you see PowerShell, you'll be able to automate a lot with it. I'm even using PowerShell to get 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 , which will only execute a block of commands 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 within 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 since today is Friday, the output:

    
30.11.2018 / 20:36
0

The same solution with PowerShell:

First you will run PowerShell as an administrator and enter the command

Set-ExecutionPolicy Unrestricted

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

Then you will open the Windows PowerShell ISE , an editor that comes 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"

Greenlittlegirlrunstotest,savesinoneplaceandtorunjustusetherightbuttonandkeeprunningasPowerShell

    
30.11.2018 / 21:04