Translation from octal to hexadecimal system and vice versa is carried out through a binary system with a triads and tetrads

Practical Work #1.

The Number system.

Subject: Number systems. Translation numbers. Binary arithmetic. Numbers. Codes: forward, backward, an additional, modified.

 

Objective: To learn how to translate from one number system to another.

 

1. Number systems.

1.1 Basic concepts and definitions.

Under a number system refers to a way of representing any number using an alphabet of symbols, called digits.

All notations are divided into positional and nonpositional.

Nonpositional such systems are the number system, in which each character retains its value regardless of the location of its position in the number.

An example nonpositional number system is the Roman system. The disadvantage of such systems include the large number of characters and the complexity of arithmetic operations.

The system called positional notation, if the same number has a different value, determines the position of the numbers in the sequence of digits representing the number. This unique value varies depending on the position occupied by the figure, according to some law.

An example of a positional number system is a decimal system used in everyday life.

Number of p different numbers used in the positioning system determines the name of the number system is called the base of the number system - "p".

Ten digits are used in the decimal system: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9; this system has a base ten number.

Any number N in a positional number system with base p can be expressed as a polynomial in p base:

N = anpn+an-1pn-1+ ... +a1p+a0+a-1p-1+a-2p-2+ ...

Here N - number, aj - ratios (number of digits), p - radix (p> 1).

Adopted represent numbers in a sequence of numbers:

N = anan-1 ... a1a0. a-1a-2 ...

In this sequence, a point separates the integer part from the decimal (the coefficients of the positive powers, including zero, the coefficients of the negative powers). Point omitted if no negative powers (an integer).

The computers use the positional number system with a non-decimal base: binary, octal, hexadecimal.

The hardware-based computer lay-off items that can only be in two states; one of which is denoted by 0, and the other - 1. Therefore, the basic number system used in computers is a binary system.

The binary number system. Used two digits 0 and 1. In any binary number can be represented as:

 

N = bnbn-1 ... b1b0 . b-1b-2 ...

где bj or 0, or 1.

where bj is either 0 or 1.

 

Octal number system. Use eight numbers: 0, 1, 2, 3, 4, 5, 6, 7. It is used as an auxiliary in the computer for recording information in an abbreviated form. To represent a single digit octal system uses three binary digits (triad) (Table 1).

 

The hexadecimal number system. To display numbers 16 digits are used. The first ten digits of the system are denoted by numbers from 0 to 9 and six digits older - Latin characters 10-A, 11-B, 12-C, 13-D, 14-E, 15-F. The hexadecimal system is used for recording information in an abbreviated form. To represent a single digit hexadecimal number system uses four binary digits (Book) (Table 1).

 

Table 1. The most important number system.

 

Binary (Base 2) Octal (Base 8) Decimal (Base 10) Hex (Base 16)
  triad   triad
0 1 0 1 2 3 4 5 6 7 000 001 010 011 100 101 110 111 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 A B C D E F 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

 

1.2 Transfer of numbers from one number system to another.

Translation numbers in the decimal system is carried out by drawing a power series with the base of the system, of which the number is transferred. Then the calculated value of the sum.

Example.

a) Transfer 10101101.1012 "10" с.с.

 

Here and below, the simultaneous use of several different number systems the base system to which the number will be indicated as a subscript.

10101101.1012 = 1 27+ 0 26+ 1 25+ 0 24+ 1 23+ 1 22+ 0 21+ 1 20+ 1 2-1+ 0 2-2+ 1 2-3 = 173.62510

б) Transfer 703.048 "10" с.с.

703.048 = 7 82+ 0 81+ 3 80+ 0 8-1+ 4 8-2 = 451.062510

в) Transfer B2E.416 "10" с.с.

B2E.416 = 11 162+ 2 161+ 14 160+ 4 16-1 = 2862.2510

 

Translation decimal integer in the non-decimal number system by repeatedly dividing the decimal number on the basis of the system in which it is translated, as long until you get less of the private foundation. The number of the new system is written in the form of fission residues from the last.

Example

.

а) Transfer 18110 "8" с.с.

Result: 18110 = 2658

б) Transfer 62210 "16" с.с.

Result: 62210 = 26E16

 

Translation of proper fractions of the decimal system in the non-decimal.

To transfer the right to another decimal system this fraction should be sequentially multiplied by the base of the system into which it is translated. At the same time multiplied by the fractional part only. Fraction in the new system is written in the form of whole pieces of works, starting with the first.

Example.

Translate 0.312510 "8" с.с.

Result: 0.312510 = 0.248

Comment. The ultimate decimal number system to another may correspond to an endless (sometimes periodic) fraction. In this case, the number of digits in the representation of the fraction in the new system is taken depending on the desired accuracy.

Example.

Translate 0.6510 "2" с.с. Accuracy of 6 characters.

Result : 0.6510 0.10(1001)2

To put in the wrong decimal number system with a non-decimal base is necessary to separately translate the integer part and the fractional separately.

Example.Translate 23.12510 "2" с.с.

1) We translate the integer part: 2) Translate the fractional part:


In this way: 2310 = 101112; 0.12510 = 0.0012.
Result: 23.12510 = 10111.0012.

 

It should be noted that the integers are whole and proper fractions - fractions in any base.

To convert octal or hexadecimal numbers in binary form is sufficient to replace each digit of the number of the corresponding three-digit binary number (triad) (Tab. 1) or a four-digit binary number (Book) (Tab. 1), the cast unnecessary zeros in the senior and junior ranks.

Example.

 

а) Translate 305.48 "2" с.с.

б) Translate 7B2.E16 "2" с.с.

 

For the transition from binary to octal (hexadecimal) system, proceed as follows: moving from the point to the left and to the right, break the binary number into groups of three (four) discharge, if necessary, complementing zeros extreme left and right groups. Then triad (Book) is replaced by the corresponding octal (hexadecimal) digit.

Example.

а)Translate 1101111001.11012 "8" с.с.

б) Translate 11111111011.1001112 "16" с.с.

Translation from octal to hexadecimal system and vice versa is carried out through a binary system with a triads and tetrads.

Example. Translate 175.248 "16" с.с.

Result: 175.248 = 7D.516.

 

Binary arithmetic.

The rules of performing arithmetic operations on binary numbers are given tables of binary addition, subtraction and multiplication.

Table binary addition Table binary subtraction Table binary multiplication
0+0=0 0+1=1 1+0=1 1+1=10 0-0=0 1-0=1 1-1=0 10-1=1 0 0=0 0 1=0 1 0=0 1 1=1

 

With the addition of binary numbers in each digit adds the terms of numbers and transfer from the nearby junior category, if available. It should be borne in mind that 1 + 1 give zero in the discharge unit and the transfer to the next.

Example. Run the addition of binary numbers:


а) X=1101, Y=101;

 

Результат 1101+101=10010.

б) X=1101, Y=101, Z=111;

Result 1101+101+111=11001.

When subtracting binary numbers in this schedule has been one of the most significant bit, if necessary. This is a 1 is equal to two 1 of the discharge.

Example. We are given the binary number 10010 X = and Y = 101. Calculate X-Y.

Result 10010 - 101 = 1101.

Multiplication of binary numbers produced by the same rules as for using the decimal binary multiplication and addition tables.

Example. 1001 101 =?

Результат 1001 101=101101.

The division of binary numbers produced by the same rules as for the decimal. It uses a table of binary multiplication and subtraction.

Example. 1100.011 : 10.01=?

Result 1100.011 : 10.01=101.1.

 

Setting options

1. Translate the following numbers in the decimal system:

а) 1101112; б) 10110111.10112; в) 563.448; г) 721.358; д) 1C4.A16; е) 9A2F.B52.

2. Translate the following number of "10" etc., в "2", "8", "16" etc.:

а) 463; б) 1209; в) 362; г) 3925; д) 11355.

3. Translate the following numbers of "10" etc "2", "8", "16" etc (Accuracy of calculations - 5 decimal points):

а) 0.0625; б) 0.345; в) 0.225; г) 0.725; д) 217.375; е) 31.2375; ж) 725.03125; з) 8846.04.

4. Translate the following numbers in the binary system:

а) 1725.3268; б) 341.348; в) 7BF.52A16; г) 3D2.C16.

5. Translate the following numbers from one number system to another:

а) 11011001.010112 "8" с.с.;
б) 1011110.11012 "8" с.с.;
в) 1101111101.01011012 "16" с.с.;
г) 110101000.1001012 "16" с.с.

6. Translate the following numbers from one number system to another:

а) 312.78 "16" с.с.; б) 51.438 "16" с.с.;
в) 5B.F16 "8" с.с.; г) D4.1916 "8" с.с.

7. Given binary numbers X and Y. Calculate X + Y and X-Y, where:

а) X=1101001; Y=101111;
б) X=101110110; Y=10111001;
в) X=100011001; Y=101011.

8. Given binary numbers X and Y. Calculate the X * Y and X / Y, where:

а) X=1000010011; Y=1011;
б) X=110010101; Y=1001; в) X=100101.011; Y=110.1;
г) X=100000.1101; Y=101.01.

PRACTICAL WORK #2.   Subject: Automation of work with folders and files: create batch files and scripts.   Goal: Batch or batch files are plain text files, containing an assortment of command interpreter and with the extension bat or cmd (cmd only work in OS NT family). Edit these files can be using Notepad or any other text editor. In this FAQ contains recommendations that have been tested on Windows 2000. Some commands and variables used when writing examples absent. How to create a batch file? Open Notepad and type the following two lines: @echo This batch file @pause Choose File - Save As, select the type of file - All files in the File Name box, type 1.bat and save it to the root of drive C, for example. Now open the C drive and run 1.bat file. The screen will display two lines. This batch file Press any key to continue ... Press any key to close the window, because bat-file is executed. Please note that dogs are a symbol in front of each team in the bat-file indicates that the command itself to display is not necessary, but you want to display only the result of its work. To experiment, remove the dog character from the beginning of each line, save and run the resulting bat file. The commands used in a bat file The list of commands that can be used in a bat file can be viewed by typing the command prompt (Start - Run - the cmd for Windows NT or Start - Run - command line for 9x) command help The result of the help - a list of available commands with brief explanations to them. For more information on the interests of the team, type in the command line help command_name. For example, to get detailed help for the AT command keys, run the following command: help AT As a result, the screen will display a list of keys to start the AT commands from the bat file. If the bat file is executed under Windows (not in pure DOS-e), then it is possible to launch any application or open files. For example, you want to automatically open the log file operation bat file when it is complete. To do this, it suffices to include in the bat file the last line of the following command: start filename.txt The result of this command will be the opening filename.txt file, and the bat file to complete its work. This method is good if the log file is small, otherwise Notepad refuse to open it by proposing to use WordPad. But this problem is also solved, as will be shown in the following examples. How to automate the launch bat file It is often necessary to automate the launch bat file to carry out routine operations. To run the bat file on a schedule best suited scheduler included in the standard distribution of Windows. Since it can be used very flexibly configure batch file to run on specific days or hours, at a certain interval. You can create multiple schedules, etc. For local run batch files, you can use third-party solutions, the benefit paid and free alternatives to the standard Scheduler great variety. Batch files can also be used as login scripts in the domain. In this they are used they will be executed in spite of his desire whenever a user logs into the network. They can be used to automate the collection of information about the machines or software installed on users' computers, change the setting to force Windows, install without the user software and automate other tasks, manual execution of which would take a very long time. How to create a file with a random name from bat file To create a file in the course of the batch file, use the redirection symbol. It looks like this: > Those. to create a file you want to redirect the flow of the screen to a file. This can be done using the following command: @echo Start file> C: \ 1.txt After running this command in the root of drive C, the text file will be created with the Start file line. When creating a file name in his system variables can be used or parts thereof. For example, you can create a report file on the bat file named by date run bat file. To this end, the following lines can be used in a bat file. set datetemp =% date: ~ -10% @echo.>% SYSTEMDRIVE% \% DATETEMP% .txt These two lines operate as follows. First, we create a variable in the memory datetemp, which assigns 10 characters from right to left on the system variable DATE. So now in the temporary variable datetemp contains only the current date. The next line redirects the output of the symbol point to a file whose name is take from variable datetemp, and clearly indicate the extension of txt. The file will be created on the computer system disk, which runs a bat file. When collecting information about administrator computers on the network will be more convenient to add to the file name, the computer name. This can easily be done using the following command: . @echo> C: \ FolderName \% COMPUTERNAME% .txt This command in the batch file will create on the disk with a text file with a name of the computer that is running a batch file. To create a file with a certain name, you can use any system variables, or create your own, based on system variables and / or other data. How to create a folder from the bat file To create a folder, use the command MKDIR analogue or abbreviated MD. To create a folder from the bat file need to use the following command: MD FolderName After executing this command FolderName folder will be created in the folder from which launched the bat file. To create a file in a different run bat file location, for example in the D disk root, use a clear indication of the new folder location. The team will look like this: MD D: \ FolderName When creating a folder you can use system variables. For example, you can create in the root folder D with the name of the current user. This will require the variable% USERNAME%, and the team will look like this: MD D: \% USERNAME% You can further complicate command and create a folder with the name of the current user on the system drive of his computer. The command for this would look like: MD% SYSTEMDRIVE% \% USERNAME% possible to use any system variables or their parts or when creating a file folder. The following example shows how to create a system disk on the user's computer folder with a name equal to the current date. set datetemp =% date: ~ -10% MD% SYSTEMDRIVE% \% datetemp% This construction operates as follows. The first command creates a variable datetemp in memory, which will be destroyed after use bat file. So long as the bat file has not finished its work it is possible to operate with the value of this variable. Variable datetemp assigned to 10 characters from right to left on the system variable DATE, ie from the current date. DATE variable is Dn DD.MM.YYYY format. The first characters to the left - the name of the day of the week and so we reject them and assign a temporary variable datetemp only the current date. This is not limited to the list of possibilities when creating folders. You can oprerirovat variables as convenient to you to create a folder with a unique, easy to read names. Get a list of all possible variables using the SET command. How to redirect the output of the command to a file Often, when performing complex bat file automatically verify the results of his work is difficult for many reasons. It is therefore easier to record the results of the batch file commands into a text file (log file). and then analyze the correct operation of the bat file on the ravine. Redirect the output of commands to a log file simply file bat. It will be shown how to do it. Create a bat-file to read (copy these lines into Notepad and save the file with the extension bat): @echo off echo Start% time% echo Create test.txt echo test> C: \ test.txt echo Copy Test.txt to Old_test.txt copy C: \ test.txt C: \ Old_test.txt echo Stop% time% The first line disables the output of the commands themselves. Thus, only the results of their execution are written to the log file. The second line writes to the log file when starting a batch file. The third line writes to the log file an explanation of what the following command creates a file test.txt A team from the fourth line creates a test.txt file from the root of drive C. The file is created for example. This command writes the file C: \ test.txt word test The fifth line displays the log file an explanation that the following command is copying a file from one location to another. The team in the sixth line of copies created by the file C: \ test.txt to the file C: \ Old_test.txt, ie creates a copy of the file under a new name. Last, the seventh row contains the time-to-completion command to the batch file. The amount of recording time in a log file start the batch file, these two time values ​​make it possible to estimate the time of the batch file. Save this batch file as, for example, 1.bat Suppose that on the batch file a report we would like to be kept in a separate folder and every day to write a report with a new file name to be able to address any of the days to the logs for the previous day. Moreover, the name of the log tether would like to have a date of the batch file. To realize all this will create on drive C (for example) a directory named LOG, ie full path to it will look like C: \ LOG. Created 1.bat batch file will run the following command: 1.bat> C: \ LOG \% date ~ -10% .txt If the batch file is run from the Scheduler, you need to specify the full path to the bat-file. Note that if the path contains spaces, you have to either use quotation marks, or 8.3 format. That is, if the path to the bat-file C: \ Program Files \ 1.bat, for example, the scheduler on the command line to run the bat-file must specify one of the following lines: "C: \ Program Files \ 1.bat"> C: \ LOG \% date ~ -10% .txt C: \ Progra ~ 1 \ 1.bat> C: \ LOG \% date ~ -10% .txt After starting 1.bat file in the folder C: \ LOG file will be created with a name equal to the launch date bat-file, for example, 13.01.2004.txt This will be the report of the batch file 1.bat Running bat-file, an example of which is shown in the first listing at the top of the page, the above command will create a log file of the content: Start 19: 03: 27,20 Create test.txt Copy Test.txt to Old_test.txt Copied files: 1. Stop 19: 03: 27.21 Thus, to perform redirects results of bat-file in the log file you want to use the redirection symbol> The syntax is as follows: Path \ ImyaFayla.bat> Path \ ImyaLogFayla.txt Expansion of the log file can be anything. If desired, the implementation of a batch job report can be issued even in the form of the page html (the tags can be output to a log file so as to be displayed in the comments 1.bat example) and copy it to a corporate server. How to automatically respond to the confirmation request Some commands require confirmation when performing potentially dangerous actions. For example, commands such as format or del prior request confirmation for further implementation. If one of these commands is performed in a batch file, you are prompted to confirm to stop execution of a batch file and it will wait for the user to select one of the options. And if the result of execution of the batch file is redirected to a log file, then the user will not see a request for confirmation or batch file will appear unresponsive. To remedy such troubles can redirect your answer to the team. Those. perform reverse action to redirect command output the results to a file. Let's look at an example of how looks a request for confirmation of a potentially dangerous action. Create on the C drive, for example, the Folder folder. We create it or copy it in any two files. Then, open a command prompt and run the following command: del C: \ Folder This command should delete all files in the specified folder. But first you will be prompted to confirm as follows: C: \ Folder \ *, to continue [Y (yes) / N (No)]?   The execution will be stopped as long as you do not press any key Y or N. Key When you run a batch file in the automatic mode, it execution stops. To avoid this use redirection. Redirection is done using the symbol   A vertical bar indicates that it is necessary to "give" instead of the symbol display following the team symbol. Verify redirection work. At the command prompt, the following command: echo Y | del C: \ Folder The screen will show a request for confirmation to delete all the files in the Folder folder, but with a positive response (Y). All files from the Folder folder will be deleted. Be careful with this command. How to disable the output of the command screen when you run a batch file When you run a batch file on the screen, in addition to the team of the results, conclusions and the commands themselves. To disable this command, you can use the @ symbol. To display a single command, you can put the @ sign at the beginning of this command. echo Testing This command displays the command echo Testing, and the next line - the result of its work, the word Testing. @echo Testing This command displays only the result of team work, ie word Testing. The team itself will not be displayed. If during the performance of the entire file on the screen to display the command is not necessary, it is easier to first line in a batch file write the following command: @echo off This command will disable the output of commands on the screen during the performance of the entire batch file. To the team itself is not output, it starts with the @ symbol. As one of the bat-file start another Sometimes, when you run a batch file, it is necessary to run another batch file. Moreover, in some cases, the performance of the main batch file should be suspended as long as the sub-file and other supporting files must be run in parallel with the base. For example, create two bat file. One named 1.bat and containing a single command call 2.bat A second named 2.bat and also contains one command pause Now start 1.bat file window will open, which will be prompted to press any key to continue, which will close after clicking a window. Thus, a call from a batch file using another call command stops the execution of the batch file as long as the end of the batch file, caused by the call command. In another case, it is necessary to start from the bat file or application, or other batch file without interrupting the performance of the main batch file. This is often necessary to make, for example, forced open the batch file log scheduled at night to the morning, the user can verify the correctness of its implementation. For this purpose, start the command line to correct in 1.bat file on start 2.bat and run the file now 1.bat window opened in which it is necessary to continue to press any button, and the window of the main batch file (1.bat) having worked closed. Thus, to call a batch file from one another, without stopping operation of the first batch file, you need to use the start command. The above command start and call can be used not only to launch the other batch files, but also to launch any application or open a file. For example, the start command log.txt, which is in the body of a batch file, open log.txt file in Notepad without interrupting the batch file. How do I send a message from a bat-file When the batch file is executed on one of the machines on the network, it is convenient to inform the administrator about the end of its execution by means of a message sent to the administrator's machine. You can do this by including a batch file command net send name Message text Where name or machine name of the user to whom is addressed the message and Message text - text messages. After running this command, the user name will be sent. Note that if you use text messages Cyrillic text should be typed in MS-DOS character set (code page 866). Otherwise, a message comes in the form unreadable characters. Dial in a DOS text encoding can be with any text editor that supports Unicode. It may be, for example, FAR. Open the FAR in a batch file for editing (F4) and press the F8 key. The top line coding DOS editor must be specified, but from below, from the tips of the shortcuts have the F8 key should be the inscription Win, talking about what the current encoding and DOS to switch to Win the coding necessary to press F8. How to automate the deletion of files by type To clean the disk from temporary files, you can use the command del / f / s / q C: \ * tmp. Where / F - deletes all files, even if they have read-only attribute is set / S - delete files from all the subdirectories / Q - off prompted to confirm deleting the file C: - drive on which will be found and deleted files. You can specify the entire disk, and the folder, for example, C: \ WinNT * .tmp - The type of files to be deleted Be careful with the key / q and the types of files to be deleted. Command removes, without asking permission, and if you specify the wrong type of file may remove too much. How to change the IP address of the computer from a batch file IP address can be changed by using the netsh command. To properly change the IP address, you first need to find out the current configuration. This can be done from the command line with the command netsh interface ip show address The output of this command - displays the current configuration of the network interface. We are interested in the interface name. Assume that it is called FASTNET. Suppose you want to change the IP address to 192.168.1.42, addressing the network is static, without using DHCP, the gateway 192.168.1.1, mask 255.255.255.0 In this case, the command to be executed from a batch file would look like this: netsh interface ip set address name = "FASTNET" static 192.168.1.42 255.255.255.0 192.169.1.1 1 After running this command from FASTNET IP interface address changes to 192.168.1.42. netsh command provides ample opportunities for managing network settings from the command line. For other use of its functionality certificate using netsh /? How do I know the computer name from the bat file To find a computer name when the bat file (in order to use this value in the future), use the command hostname This command returns the name of the computer on which it is running. How to rename all files matching mask from a batch file Sometimes you need to rename all the files in a folder on a template from a batch file. This can be done using the following command in a bat-file: for / f "tokens = *" %% a in ( 'dir / b PATH \ *. *') do ren PATH \ %% a Prefix %% a This line should be replaced by PATH \ on the path to the files will be renamed, and Prefix those characters that will be added to the file name when renaming. Do not place the batch file in the folder where the name change, otherwise it will be renamed too. If there are subfolders, the subfolders also the name prefix will be added to the folder where the file renaming, ie subfolders will be renamed as the files. If you specify a specific mask for file types that are to be renamed, for example, * .txt, and not *. * In the example, it will be renamed files only specified types. Other files and folders will not be renamed. The second option: set thePATH = C: \ test for %% I in (* .txt) do ren "% thePATH% \ %% ~ nxI" "%% ~ nI.dat" How to use the percentage symbol in the batch file To use the percent symbol (%) it is necessary to write twice a batch file. for example echo 50 %% This command in the bat-file will display 50%. If you use the echo command 50%, then the screen only the number 50 is displayed. Consider this feature when using the% character in the writing batch files. As from a batch file to make the registry export Sometimes you need to save the file in a certain bush (branch) registry with all parameters as a backup or for other purposes. Make it from a batch file, you can using the following command: regedit.exe -ea C: \ environment.reg "HKEY_CURRENT_USER \ Environment" This command when the batch file unload branch HKEY_CURRENT_USER \ Environment in the file C: \ environment.reg When the need to restore the settings in the HKEY_CURRENT_USER \ Environment, will be enough to run environment.reg file. This command can be used for daily backup software and system settings are stored in the registry. Do not forget that if the way in which the output file or the name of the registry branches there is a space to be preserved, they must be enclosed in quotes. As the batch file import the registry variables If there is a need of a batch file to import the registry previously saved or new values ​​of the variables, you can do this with the command regedit.exe -s C: \ environment. reg This command imports the registry data from environment.reg file without asking for confirmation by using the -s option. As from the bat file to bypass validation date Some software startup checks the current system date. If the date is more than incorporated developer, the program does not start. For example, the developer believes that the version of the program can work a month, and then the user will have to install an updated version of the software. On the one hand it is to take care about the user, which will have at its disposal the latest version of the program with the elimination of shortcomings in relation to the previous versions. On the other hand, the manufacturer forces the user to download the new version, even if the user is completely satisfied with that version of the program that had been established. This problem can be easily solved using the following batch file that will run the program, wait for its completion and return date to that which was before the start of the program. set tempdate =% date: ~ -10% date 01-01-04 notepad.exe date% tempdate% In this example, the current system date is first stored in a variable, then (the second line), the system date is set on the 1st January 2004, and then call a program that checks the system date. In this example, Notepad. As long as the notebook is open, the batch file is pending, is not completed, and without taking the system date back. Once the notebook is closed, the batch file will continue to perform and set the system date stored in the variable tempdate value, ie, the fact that it was to launch a batch file. Do not forget that if the path to the file that starts the program contains spaces, then it (the path) must be enclosed in quotes. If the path contains Cyrillic, the text editor that supports encoding DOS (eg, FAR) should be used when writing a batch file. Otherwise, when you run the batch file, a message will appear that the "specified file is not an internal or external commando ...". If the program checks the current system date only if its start-up and while working longer does so, the batch file can be modified by adding to the name of the executable file of the operator start, ie, our example will look like this: set tempdate =% date: ~ -10% date 01-01-04 start notepad.exe date% tempdate% In this case, the batch file to change the system date, the program will start without waiting for its completion date will return to the one who was to run the program. As in a bat file to wait for a particular file Sometimes you need to perform some action when a specific file in the folder. To organize check the appearance of the file in the folder, you can use the following batch file : test if exist c: \ 1.txt goto go sleep 10 goto test : go notepad This batch file will check availability with an interval of 10 seconds in the root directory C 1.txt 1.txt file, and when a file appears to be carrying out the action, the following tags! Go, ie, in this example we will launch Notepad. sleep utility is freely distributed as a part of Resource Kit. You can download it here. If the file 1.txt large and copied from somewhere, it may happen that a batch file will check its availability at the time, as the file is not copied or used by another application. In this case, an attempt to perform any action with the file 1.txt will fail. To avoid this, you can modify the batch file as follows: : test if exist c: \ 1.txt goto go sleep 10 goto test : go rename c: \ 1.txt 1.txt if not errorlevel 0 goto go del c: \ 1.txt 1.txt When the files are copied to the C drive is not full, or in use by another application, an attempt to rename it will fail and the cycle is repeated as long as the file is copied in full or will not be released. After the command rename c: \ 1.txt 1.txt will be executed without error (ie, the file is free), then it is possible to perform any action. The last example is its deletion. How to add comments to the bat-file When you write a large batch file is very useful to add comments to its main units. This will easily understand that these blocks make the passage of time. Comments can be added in several ways. The first longer suitable for writing great comments that describe either the entire batch file or a few large blocks of it. The code is as follows: goto start This batch file is used to automate routine operations, performed at night to sync Content Corporate ftp-server with branches ftp-servers, The batch file is written 01/01/2004 Last time modified 10/02/2004 Etc. : start Such a comment when you run the batch file transfers control immediately to the command following the label start. It is responsible for this transition operator goto. Simpler can add comments beginning with rem command line or from two colons following each other. rem This unit establishes a connection with the remote server: This unit checks the file dates change Commenting large batch files (such as, in principle, any code) - good tone, which greatly facilitates the process of reviewing these files by other people or by the author of a considerable time lapse from the moment of writing. Automatic creation of backup data Favorite kindly shared with us a little script that automatically creates a backup copy of important data. Here is his message: Script archives faylopomoyki folder (\\ SERVER \ e $ \ Exchange) and the folder with the data users (home users data (\\ SERVER \ e $ \ Departments)) The script does not archive media, since they take place the car and me at a loss is not critical rem ******************* rem Asking screws which are folders for archiving and screw which will back up rem ******************* SET DRIVE1ST=d: SET DRIVE2ND=e: rem ******************* rem пароль на архив rem ******************* SET PASSWD=qmsdhgsdfgsdfhdsfhsdhf rem ******************* rem если копировать для надёжности куданить на сетку rem ******************* rem SET COPYTO=\\shpakk\c$ \!backup rem ******************** rem *** Проверим путь назначения архива rem ******************** %DRIVE1ST% if not exist \!backup md \!backup if not exist \!backup\Departments md \!backup\Departments if not exist \!backup\Exchange md \!backup\Exchange if not exist \!backup\MEDEA_rdisk md \!backup\MEDEA_rdisk cd \!backup rem ****************************** rem *** Backup \\SERVER\e$ \Exchange rem ****************************** SET DIRNAME=Exchange SET FILENAME=SERVER_Exchange SET SOURCEDIR=e:\Exchange rar a -r -m5 -dh -x*.mp3 -x*.fcd -x*.avi -x*.zip -x*.rar -x*.exe -w%DRIVE2ND%\ -p%PASSWD% %DIRNAME%\%FILENAME%_new %SOURCEDIR%\*.* if not exist %DIRNAME%\%FILENAME%_new.rar goto next2 del %DIRNAME%\%FILENAME%_old.rar ren %DIRNAME%\%FILENAME%.rar %FILENAME%_old.rar ren %DIRNAME%\%FILENAME%_new.rar %FILENAME%.rar copy %DIRNAME%\%FILENAME%.rar %COPYTO%\%DIRNAME% :next2 rem *************************** rem *** Backup \\SERVER\e$ \Users rem *************************** SET DIRNAME=Departments SET FILENAME=SERVER_Departments SET SOURCEDIR=e:\Departments if not exist %DIRNAME%\%FILENAME%_new.rar goto next3 del %DIRNAME%\%FILENAME%_old.rar ren %DIRNAME%\%FILENAME%_4.rar %FILENAME%_old.rar ren %DIRNAME%\%FILENAME%_3.rar %FILENAME%_4.rar ren %DIRNAME%\%FILENAME%_2.rar %FILENAME%_3.rar ren %DIRNAME%\%FILENAME%_new.rar %FILENAME%_2.rar :next3 rem ******************* rem это если архив будет большим - кусками бить -v357888k rem ******************* rem rar a -r -m5 -v357888k -dh -x*.mp3 -x*.fcd -x*.avi -x*.exe -w%DRIVE2ND%\ -p%PASSWD% %DIRNAME%\%FILENAME%_new %SOURCEDIR% rem ******************* rem архивирование пошло, не берём мультимедиа файлы rem ******************* rar a -r -dh -x*.mp3 -x*.fcd -x*.avi -x*.exe -x*.jpg -x*.tif -p%PASSWD% %DIRNAME%\%FILENAME%_new %SOURCEDIR% goto next4 :next4 rem ********************************** rem *** Create repair disk on \\SERVER rem ********************************** SET DIRNAME=MEDEA_rdisk SET FILENAME=MEDEA_rdisk SET SOURCEDIR=%windir%\repair %windir%\system32\rdisk.exe /s- rar a -r -m5 -dh -w%DRIVE2ND%\ -p%PASSWD% %DIRNAME%\%FILENAME%_new %SOURCEDIR%\*.* if not exist %DIRNAME%\%FILENAME%_new.rar goto next5 del %DIRNAME%\%FILENAME%_old.rar ren %DIRNAME%\%FILENAME%.rar %FILENAME%_old.rar ren %DIRNAME%\%FILENAME%_new.rar %FILENAME%.rar copy %DIRNAME%\%FILENAME%.rar %COPYTO%\%DIRNAME% :next5 Finally, we have 3 files: Departments, in it: SERVER_Departments_2.rar - 2 days old SERVER_Departments_3.rar - 3 days old SERVER_Departments_4.rar – 4 days old SERVER_Departments_new.rar - new SERVER_Departments_old.rar – old Exchange Since we archive exchanger - he told us that much and do not need a store only today, yesterday archives SERVER_Exchange.rar SERVER_Exchange_old.rar Well, the system just in a folder SERVER_rdisk SERVER_rdisk.rar SERVER_rdisk_old.rar How to prevent restarting the application If the application is run bat-file, the track (and ban), it can be restarted as follows: pslist.exe -e "process" if% errorlevel% == 1 the start "" "Program" pslist - a free utility. How to create a folder with a name equal to the current date Quite often, you need to create a folder with a name equal to the current date, and then copy it, for example, backups. There are much options to create a folder: MD% SYSTEMDRIVE% \% date: ~ -10% This command will create the system drive root folder named to the current date. This command does not work if the regional settings are different from Russian and date separator used slash. In this case, one of the following will scripts: md% system drive% \% date: ~ 0,2% _% date: ~ 3,2% _% date: ~ 6,4% or md% systemdrive% \% date: ~ 6,4% _% date: ~ 3,2% _% date: ~ 0,2% In the second case, the folder will be sorted alphabetically. As an alternative, you might consider adding to the name of the current date of the file: @echo off For / F "tokens = 1-4 usebackq delims =." %% 1 in ( `date / t`) do set mydate = %% 4. 3 %%. %% 2 ren test.txt test% mydate% .txt test.txt The file name will be renamed test2005.03.05.txt Is it possible to loop in a bat-file the command? Sometimes you need to create a bat-file, which will run a certain command after a certain time interval. To solve this problem, you can use Scheduler and often run a bat-file from it, and you can create a file in a bat-cycle pause. The cycle is organized with the following commands: : loop ... Goto loop Pause can be created with any team. For example, using ping: ping -n 1 -w 100000 192.168.254.254> nul -n 1 - perform a single query 100000 -w - wait for a response of 100 seconds (value in milliseconds) 192.168.254.254 - IP address, which obviously does not exist in the network, ie, I never respond to a ping > Nul - redirect all the ping command output to nowhere.