Information

How to write a batch file was and still is the same

2016/09/13

昔も今も変わらぬバッチファイルのお作法

Automatic Program Update Batch File

A batch file is a text file with the bat extension, which has existed since the days of the old Dos, and it is used to call a batch file to run a process to be executed by the Windows shell (command prompt).

In the system company where I entered after graduation, I wrote the process to copy (xcopy) two or more folders entirely on Windows NT, which was my first experience of the batch file. This is a standard practice in business system operation.

As a standard practice of batch files in business system operation, when a client module is launched, it checks the latest version on the server and automatically updates it if it is new, which includes iteration (for), conditional branching (if) and lazy environment variable expansion (if). (enabledelayedexpansion), which includes the manners of a difficult shell command.

  • Row1:"@" doesn't show own command, "echo off" doesn't show subsequent commands.
  • Row2:Get the directory where the batch file is running (current directory) from the %CD% environment variable and set it to a variable.
  • Row3:Set a variable with the path of the server directory where the program is located.
  • Row5:Ping command: 1 attempt, 1 second timeout
  • Row6:Jump to the error routine if the environment variable errorlevel is 1
  • Row9:for (optional) %% alphabetic character in (for looping) do command
    in (%SVRDIR%SVRDIR%setting.ini): in the setting.ini in the server directory where the program is located.
    /f:Extract a token (the smallest unit of a string, usually a tab-separator) from a text file and assign it to it
    "tokens=1,2 delims==" %%a: get variable %%a for the first token separated by "=" ("=") and variable %%b for the second
  • Row10:If the value of variable %%a is "version", then assign the value of variable %%b to the variable version
  • Row13:for (optional) %% alphabetic character in (for looping) do command
    in (ver.ini):In ver.ini in the current directory
    /f:Extract a token (the smallest unit of a string, usually a tab-separator) from a text file and assign it to it
    "tokens=1,2 delims==" %%a: get variable %%a for the first token separated by "=" ("=") and variable %%b for the second
  • Row14:If the value of variable%%a is "ver", then assign the value of variable%%b to the variable ver
  • Row16:If Ver is smaller than Version, jump to upd routine
  • Row17:Jump to process routine on successful update
  • Row18:Move to the end of the batch file.
@echo off
SET CDIR=%CD%
SET SVRDIR=\\192.168.1.100\Share_Folder

Ping 192.168.1.100 -n 1 -w 1000
if errorlevel 1 goto:error

rem Compare the version of setting.ini on the server with the version of ver.ini in your folder
for /f "tokens=1,2 delims==" %%a in (%SVRDIR%\setting.ini) do (
if %%a==version set version=%%b
)

for /f "tokens=1,2 delims==" %%a in (ver.ini) do (
if %%a==ver set ver=%%b
)
if %Ver% LSS %Version% goto :upd
goto:process
goto:eof


rem update process routine
:upd
xcopy /s/y "%SVRDIR%" %CDIR%
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (ver.ini) do (
  set var=%%a
  if not {!var!}=={!var:ver=!} set var=ver=%version%
  echo.!var! >> output.ini
)
del ver.ini
rename "output.ini" "ver.ini"
endlocal
goto:process
goto:eof


rem Processing routines for a successful update
:process
setlocal enabledelayedexpansion
start AccountPRO.lnk AccountPRO
endlocal
exit
goto:eof


rem Processing routines for ping errors
:error
echo you are not conected to server please check network ...
pause

The setting.ini file on the server

[settings]
version=1.23

ver.ini file in the current directory

ver=1.01
  • Row23:Copy the entire server directory to the current directory
    /s option: copy the whole directory if the file exists
    /y option: Do not check for overwriting if the file with the same name exists
  • Row24:Localization of defined variables (does not affect outside of setlocal to endlocal)
    enabledelayedexpansion option: variable values are changed during execution
  • Row25:for (optional) %% alphabetic character in (for looping) do command
    in (ver.ini):In ver.ini in the current directory
    /f:Extract a token (the smallest unit of a string, usually a tab-separator) from a text file and assign it to it
    "tokens=*" %%a: get all strings in variable %%a
  • Row27:Set variable var to "var=version".
  • Row28:Write the variable ver to the output.ini file.
  • Row30:Remove ver.ini.
  • Row31:Rename output.ini to ver.ini
  • Row40:Open the AccountPRO link with the title AccountPRO
    The first argument of the start command is a string that sets the title of the application
  • Row42:Quit the batch file midway.
  • Row43:Move to the end of the batch file.
  • Row49:Please press any key to continue..." and stop the operation until some key is entered. and stop the operation until some key is pressed.

How to write commands

Shell commands to manipulate the Unix kernel (core) are entered from a terminal (terminal), such as cygwin, while command prompts (cmd.exe) to manipulate the Windows kernel are entered from a console (not a terminal).

environment variable

By setting the directory of the program that executes the command to the representative path of the Windows system environment variable, you can execute the command from the command prompt without putting the full path, but since these system environment variables are common in the Windows environment, you can use You can run the batch file on someone else's PC.

On the other hand, you can also define your own environment variables, which you can define with the "SET" command and refer to in the batch file with "%Environment%".

Getting the date and time

The format of the date to be retrieved from the batch file is set from the Region and language in the Control Panel.

In the case of the Japanese date format (yyyy/MM/dd)

%DATE:~-10,4% : The tenth digit from the back (- is escaped by ^) to the fourth digit is the year.
%DATE:~-5,2% : The fifth to second digit from the back is the month.
%DATE:~-2% : Second digit from the back to the end is day
%TIME:~0,2% :Two digits from the front is the time.
%TIME:~3,2% : The third to second digits from the front
%TIME:~6,2% : Sixth to second digits from the front

In English (M/d/yyyy)

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"

Error level of ping

The ping command is specified with a set of options and arguments, "-n 1" specifies one attempt, "-w 1000" specifies a timeout time of 1000 milliseconds (1 second), but returns the following values to the environment variable errorlevel, depending on the result.

  • can't find a host:1
  • No response.:1
  • Response.:0
  • No route.:0

If the server is on a company LAN that doesn't go through a router, there can't be a "no route", so you can determine if the errorlevel is 0 (responsive) or 1 (unresponsive).

escape sequence

When assigning a special character to an environment variable, the escape character (^) must be added, e.g., if you want to insert the string "&TOKYO" into the user environment variable LOGO, SET LOGO=^&TOKYO.

© 2021 バテラハイシステム Powered by STINGER