Batniki 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 tell about the capabilities of the Windows-console, but to show them, solving practical problems that I had to face.
Confirmation of execution
set answer =
set / p answer = "Run? [y / n]:"
if not "% answer%" == "y" exit / b
Set the answer variable to an empty value (necessary to overwrite the previous value, for example, if the batch file is executed several times). set with the / p key sets the value of a variable that is entered by the user. Check the value of the variable, and if it is not equal to y, terminate 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 from you) will be output on 13.06.2009, in order to get the current month, it’s enough to do% date: ~ 3.2% Thus, if we need to, for example, generate the name file or directory that corresponds to the current month and day, we will do it like this:
set fname =% date: ~ 3.2 %% date: ~ 0.2%
Personally, I needed this to set the / d key for xcopy.
How to display an empty line in the log file?
I did not immediately realize that it could be done like this: :)
echo. >>% logfile%
Those. need to put a dot after echo.
Did the previous command succeed?
In most cases this is:
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 a file with a name that changes daily, but in a different folder. The next piece of code
for %% i in (c: \ dir \ #fe *. *) do
we select all files by mask by executing the command rar a -ep c: \ other_dir \ %% ~ ni.rar %% i for each, where %% ~ ni is just the file name.
The name of the current executable batch file
% ~ n0
It is necessary, for example, to keep one log for several batch files.
"Function" in batch file
If we execute several duplicate commands in one batch file, but with different values of variables, then this can be done 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 have sent a dozen different files to different recipients using The Bat! Such a file to read and edit is much more convenient than it would be ten teams The Bat! contract.
Get the last file created by date
First we 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 cycle in the variable% lastfile% we will have the last file by modification date;)
Download file via FTP
ftp -s: file_to_ftp.txt 127.0.0.1
Where the file file_to_ftp.txt 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%
Understand the code suggest yourself. :) The main possibility that I wanted to show with this example is the use of the / a switch on the set command to evaluate expressions. ;)