Bat files, examples
You can execute all the examples published below not only by creating and running a BAT file containing these commands, but by copying them directly to the command line.
To open the command line in the right place (in the folder with files, for example), you need to call the context menu (RMB) with the Shift key held down:
command line call
You can read about how to work with the command line in this article. We pass directly to the teams.
A list of all console commands with a description can be obtained by typing help in the console
Help for any command can be obtained using the /?
For example: DIR /? displays help for all keys of the DIR command
Delete temporary files before shutting down your computer
I think everyone at least heard about sms viruses or came across them personally. The bat-file will help you to shut down the computer correctly by deleting temporary files, the virus folder is usually loaded into the folder with.
At the next boot of the device (at an early stage), the virus makes entries in the registry, disrupting the normal operation of the system. And when the desktop boots, the situation is more difficult to fix.
Of course, not all viruses work according to this scheme, but nevertheless, clearing temporary files and the system cache before shutting it down significantly reduces such risks.
start / wait “” “C: \ Program Files \ CCleaner \ CCleaner64.exe” / auto
start / wait “” “C: \ WINDOWS \ System32 \ shutdown.exe” / s / t 10
CCleaner is not installed by default on Windows. It needs to be installed separately. You can download the installer on the developer’s website.
The CCleaner program is launched first and deletes all temporary files on the computer. Then the computer shutdown program starts with a delay of 15 seconds to avoid possible conflicts with CCleaner.
You must create a .bat file and copy this example into it. Display the shortcut to the bat-file on the desktop, assign it a beautiful icon and turn off the computer using this shortcut-button.
Get a list of files in a folder using a bat file
I periodically use bat-files to get lists of files in folders. A common situation: for work, clients send an archive with photos from the photographer. Photos are named according to the article numbers.
There is no textual information accompanying the photograph. You need to make a list based on the photos sent and import it into the product catalog on the site. For each product, several photos were taken. They are named like this:
Photo of the product with the article A1234.jpg
Photo of the product with the article A1234 (2) .jpg
Photo of the product with the article B1234.jpg
Photo of the product with the article B1234 (2) .jpg
First, I get a list of all the files in the folder with the following command:
dir * .jpg / B / L> filelist.txt
The * .jpg command will allow only JPG files to be considered when compiling a list. The / B switch allows you to get a list containing only the names of the files in the folder. The / L switch will print all lowercase names. The command> filelist.txt will create a text file named filelist and write the result there.
The next step is to get rid of duplicates, so that only one entry is left in the list for each product:
type filelist.txt | findstr / I / V “(2)”> temp.txt
The findstr command will search the previously received file. The / I switch allows you to search for case insensitive entries, and the / V switch records lines that do not contain the desired match. In quotation marks is a string whose coincidence you need to fix. And the last command> temp.txt will write to the temp file all the results that do not contain the name “(2)”. As a result, I get:
Photo of the product with the article A1234.jpg
Photo of the product with the article B1234.jpg
And further on the basis of this text list I will prepare a file for import.
If you need to carry out the opposite operation – output only matches to the temp.txt file, then in the list of commands (findstr /?) You will not find the one you need. There is only an inverse filter by exact match – / X.
For this task, you can use the command to output line numbers / N in which there are matches (numbers are displayed together with the line):
type filelist.txt | findstr / I / N “(2)”> temp.txt
The main thing when working with text information (text files) is to remember one thing:
If for text operations you use a file that you did not create through the command line as the source, it must be in the encoding that is understood by the command line. For example, CP1251 (ANSI).
Otherwise, you run the risk of getting something like this:
wrong encoding
Copy directory tree without files
When I start doing new projects, it becomes necessary to get a directory tree similar to the old project one, with the difference that there should be no files in it. For a new project, it is easier to add 3-5 files to the desired empty folders than to copy an existing project and then delete the unnecessary from there.
To get a directory tree without files, use the following command:
xcopy folder_1 folder_2 / T / E