Informasi

Cara menulis "batch file" dulu dan masih sama

2016/09/13

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

Automatic Program Update "Batch File"

"Batch file" adalah file teks dengan ekstensi bat, yang telah ada sejak masa Dos lama, dan digunakan untuk memanggil "batch file" untuk menjalankan proses yang akan dijalankan oleh shell Windows (command prompt ).

Di perusahaan sistem tempat saya masuk setelah lulus, saya menulis proses untuk menyalin (xcopy) dua atau lebih folder seluruhnya di Windows NT, yang merupakan pengalaman pertama saya tentang "batch file". Ini adalah praktik standar dalam pengoperasian sistem bisnis.

Sebagai praktik standar "batch file" dalam operasi sistem bisnis, saat modul klien diluncurkan, modul klien akan memeriksa versi terbaru di server dan secara otomatis mengupdatenya jika masih baru, yang mencakup iterasi (untuk), percabangan bersyarat (jika) dan perluasan variabel lingkungan malas (jika). (enabledelayedexpansion), yang mencakup cara kerja perintah shell yang sulit.

  • 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.

Bagaimana menulis command

Shell command untuk memanipulasi kernel Unix (inti) dimasukkan dari terminal (terminal), seperti cygwin, sedangkan command prompts (cmd.exe) untuk memanipulasi kernel Windows dimasukkan dari konsol (bukan terminal).

environment variable

Dengan mengatur direktori program yang menjalankan perintah ke jalur perwakilan variabel lingkungan sistem Windows, Anda dapat menjalankan perintah dari prompt perintah tanpa meletakkan jalur lengkap, tetapi karena variabel lingkungan sistem ini umum di lingkungan Windows, Anda dapat menggunakan Anda dapat menjalankan "batch file" di PC orang lain.

Di sisi lain, Anda juga dapat menentukan variabel lingkungan Anda sendiri, yang dapat Anda tentukan dengan perintah "SET" dan merujuk ke "batch file" dengan "% Environment%".

Mendapatkan tanggal dan waktu

Format tanggal untuk diambil dari "batch file" diatur dari Wilayah dan bahasa di Panel Kontrol.

Untuk format tanggal Jepang (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

Perintah ping ditentukan dengan sekumpulan opsi dan argumen, "-n 1" menentukan satu percobaan, "-w 1000" menentukan waktu tunggu 1000 milidetik (1 detik), tetapi mengembalikan nilai berikut ke tingkat kesalahan variabel lingkungan, tergantung hasilnya.

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

Jika server berada di LAN perusahaan yang tidak melalui router, tidak boleh ada "tidak ada rute", jadi Anda dapat menentukan apakah tingkat kesalahan adalah 0 (responsif) atau 1 (tidak responsif).

escape sequence

Saat menetapkan karakter khusus ke variabel lingkungan, karakter melarikan diri (^) harus ditambahkan, misalnya, jika Anda ingin memasukkan string "& TOKYO" ke dalam variabel lingkungan pengguna LOGO, SET LOGO = ^ & TOKYO.

© 2021 BAHTERA HISISTEM Powered by STINGER