Windows Scripting: Difference between revisions

From Andreida
Line 133: Line 133:
echo.
echo.
echo Normal 2
echo Normal 2

=== Creating files with current date/time in the name ===
==== Get the current date/time in European format into a variable ====
Create the file "SetDateTimeToVariables-Europe.cmd":
<pre>
@echo off
rem v2 (2022-11-23), use this for dates like "2022-11-23"
for /f "tokens=1-3 delims=- " %%a in ('date /T') do set year=%%a
for /f "tokens=1-3 delims=- " %%a in ('date /T') do set month=%%b
for /f "tokens=1-3 delims=- " %%a in ('date /T') do set day=%%c
set TODAY=%year%-%month%-%day%
rem @echo today: %TODAY% year: %year% month: %month% day: %day%

for /f "tokens=1 delims=: " %%h in ('time /T') do set hour=%%h
for /f "tokens=2 delims=: " %%m in ('time /T') do set minutes=%%m
set NOW=%hour%-%minutes%
rem @echo %NOW%
</pre>

Revision as of 06:14, 28 February 2024

directory of current script

change to the directory of the executed script:

chdir /D %~dp0

The /D allows to change drives too.

save the current directory and return to it

set DIR_START=%cd%
cd whereEver/AndDoStuff
cd %DIR_START%

functions

@echo off
echo 1
call :func_1 2
call :func_1 3
echo 4
exit /B

:func_1
echo %1 (called in a function)
exit /B 0

Output:

>test
1
2 (called in a function)
3 (called in a function)
4

parameters

  • Parameters to a script are %1 to %9
  • %0 is the call and should contain the script name
  • %10 does not exist
  • %* "is" all parameters, even if there are 15
  • if you want to have more than 9 parameters, google for shift or use something like
FOR %%A IN (%*) DO (
  echo xxx %%A xxx
)

Output:

>test 1 2 3 4 5 6 7 8 9 10 11 12 13
x 1 x
x 2 x
x 3 x
x 4 x
x 5 x
x 6 x
x 7 x
x 8 x
x 9 x
x 10 x
x 11 x
x 12 x
x 13 x

test for parameters

@echo off

if [%1]==[] (
  echo Parameter 1 missing!
) else (
  echo Paramater 1 found: %1
)
  • '[' and ']' around "%1" are just for the case it is empty. You can use anything you want:
 if xx%1xx==xxxx (
  • you need a space between "[]" and '(' in the if condition
  • the "(" must be on the same line as the if
  • the ") else (" can not be on multiple lines

text color

The documentation from MS is here: https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences?redirectedfrom=MSDN

You will need to output a certain character which is just called ESC. It is the "character" with the number 27 in the ASCII table, so you can create it on the console if you hold ALT and enter with the numlock digits "027". When you release ALT afterwards you should get "^[". You can try it but it will be of not much use. It would be:

  • ALT + 027
  • [92m
  • <return>

Now your prompt should be green. To get back to normal:

  • ALT + 027
  • [0m
  • <return>

Try the following script: (create a test.cmd and execute it from the command line)

@echo off

for /F %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"
set GREEN=%ESC%[92m
set DEFAULT=%ESC%[0m
set ERROR=%ESC%[101;93m

call :out Dies ist ein erster Test.
call :out Dies ist ein zweiter Test.
call :outError This is not a test, please leave this script immediately!

set ESC=
set ERROR=
SET GREEN=
exit /B
:out
  echo %GREEN% %* %DEFAULT%
  exit /B
  
:outError
  echo %ERROR% %* %DEFAULT%
  exit /B  

You can create the ESC variable with

set ESC=

then use ALT+0+2+7 to get the correct "character" at that spot. It will look like:

set ESC=ESC

but the second ESC will look different, because it "is" just one character. Copy/paste will be a burden and just looking at such a script will not tell you how to recreate it. That is the reason the variant where you create the ESC with the for (I got the idea from someone called Aacini in a forum) is much much better in my opinion.

output empty line

Put a period immediately after the echo. No space between echo and period.

@echo off
echo Normal 1
echo.
echo Normal 2

Creating files with current date/time in the name

Get the current date/time in European format into a variable

Create the file "SetDateTimeToVariables-Europe.cmd":

@echo off
rem v2 (2022-11-23), use this for dates like "2022-11-23"
for /f "tokens=1-3 delims=- " %%a in ('date /T') do set year=%%a
for /f "tokens=1-3 delims=- " %%a in ('date /T') do set month=%%b
for /f "tokens=1-3 delims=- " %%a in ('date /T') do set day=%%c
set TODAY=%year%-%month%-%day%
rem @echo today: %TODAY% year: %year% month: %month% day: %day%

for /f "tokens=1 delims=: " %%h in ('time /T') do set hour=%%h
for /f "tokens=2 delims=: " %%m in ('time /T') do set minutes=%%m
set NOW=%hour%-%minutes%
rem @echo %NOW%