We solve practical problems on body shirts
Batnikov is not a powerful programming language, but at the same time, it can solve many routine tasks. The purpose of the article is not just to talk about the capabilities of the Windows console, but to show them, solving practical problems that I had to face.
We set the answer variable to an empty value (necessary in order to overwrite the previous value, for example, if the batch file is executed several times). set with the / p switch sets the value of the variable that the user enters. We check the value of the variable and if it is not equal to y, we complete the batch file.
In batch files it is possible to get a substring using the syntax% variable: ~ m, n% Where variable is the name of the variable m is the index of the first character, and n is the number of characters of the substring.
Therefore, if the echo% date% command (and the date format may differ for you) will print on 06/13/2009, in order to get the current month, just execute% date: ~ 3.2% Thus, if we need, for example, to form a name file or directory that corresponds to the current month and day, we will do it this way:
Personally, I needed this to set the / d switch for xcopy.
I did not immediately guess that it could be done like this: :)
Those. need to put an end after echo.
In most cases, these are:
The task is to archive the file with a name that changes daily, but to a different folder. Next piece of code
we select all the files by mask, executing the rar a -ep c: \ other_dir \ %% ~ ni.rar %% i command for each, where %% ~ ni is just the file name.
It is necessary, for example, in order to maintain one log for several batch files.
If we execute several repeating commands in one batch file, but with different values of variables, then this can be implemented like this:
Thus, I’m sent a dozen different files to different recipients using The Bat! Such a file is much easier to read and edit than there would be ten The Bat! contract.
First, get a list of all files sorted by modification date:
And go through it cycle.
It is logical that at the end of the loop in the variable% lastfile% we will have the last file by date of modification;)
Where the file_to_ftp.txt file will look like this:
I suggest you figure out the code yourself. :) The main opportunity that I wanted to show with this example is the use of the / a switch of the set command to evaluate expressions. ;)
Confirmation of completion
set answer = set / p answer = "Run? [y / n]:" if not "% answer%" == "y" exit / b
We set the answer variable to an empty value (necessary in order to overwrite the previous value, for example, if the batch file is executed several times). set with the / p switch sets the value of the variable that the user enters. We check the value of the variable and if it is not equal to y, we complete the batch file.
How to get the current month, day, year, etc.?
In batch files it is possible to get a substring using the syntax% variable: ~ m, n% Where variable is the name of the variable m is the index of the first character, and n is the number of characters of the substring.
Therefore, if the echo% date% command (and the date format may differ for you) will print on 06/13/2009, in order to get the current month, just execute% date: ~ 3.2% Thus, if we need, for example, to form a name file or directory that corresponds to the current month and day, we will do it this way:
set fname =% date: ~ 3.2 %% date: ~ 0.2%
Personally, I needed this to set the / d switch for xcopy.
How to output an empty line to a log file?
I did not immediately guess that it could be done like this: :)
echo. >>% logfile%
Those. need to put an end after echo.
Was the previous command successful?
In most cases, these are:
if% errorlevel% == 0 ( echo OK >>% logfile% ) else ( echo ERROR #% errorlevel% >>% logfile% )
Archiving a variable name file
for %% i in (c: \ dir \ #fe *. *) do rar a -ep c: \ other_dir \ %% ~ ni.rar %% i
The task is to archive the file with a name that changes daily, but to a different folder. Next piece of code
for %% i in (c: \ dir \ #fe *. *) do
we select all the files by mask, executing the rar a -ep c: \ other_dir \ %% ~ ni.rar %% i command for each, where %% ~ ni is just the file name.
Name of the current executable batch file
% ~ n0
It is necessary, for example, in order to maintain one log for several batch files.
"Function" in body shirts
If we execute several repeating commands in one batch file, but with different values of variables, then this can be implemented like this:
set thebat = c: \ Program Files \ The Bat! \ thebat.exe set action = SEND set mailto=email_1@domain.com set subject = Subject_1 set attach = c: \ dir \ file ???? call: mailit set mailto=email_2@domain.com set subject = Subject_2 set attach = c: \ dir \ file * .rar call: mailit ... exit : mailit @echo on "% thebat%" / MAILTO = "% mailto%"; SUBJECT = "% subject%"; ATTACH = "% attach%";% action% >>% logfile% @echo off exit / b
Thus, I’m sent a dozen different files to different recipients using The Bat! Such a file is much easier to read and edit than there would be ten The Bat! contract.
Get last created file by date
First, get a list of all files sorted by modification date:
dir / b / od / ad c: \ dir \ file _ *. xls
And go through it cycle.
for / f "tokens = *" %% a in ('dir / b / od / ad c: \ dir \ file _ *. xls') do set "lastfile = %% a"
It is logical that at the end of the loop in the variable% lastfile% we will have the last file by date of modification;)
Download file via FTP
ftp -s: file_to_ftp.txt 127.0.0.1
Where the file_to_ftp.txt file will look like this:
login password bin cd / files put file.xls quit
Create numbered directories with an increment of one
set dir_last = for / f "tokens = *" %% i in ('dir / ad / b') do set dir_last = %% i if '% dir_last%' == '' (set dir_last = 000) set dir_last = 1% dir_last% set / a dir_last =% dir_last% - 1000 set / a dir_new =% dir_last% + 1 set dir_new = 00% dir_new% set dir_new =% dir_new: ~ -3% md% dir_new%
I suggest you figure out the code yourself. :) The main opportunity that I wanted to show with this example is the use of the / a switch of the set command to evaluate expressions. ;)