Showing posts with label solutions. Show all posts
Showing posts with label solutions. Show all posts

Monday, April 29, 2013

How to Clean Temp Folders

We have already discussed how to clean temp folders in this blog. But there are more ways to clean temp folders, and here we'll show one more of them. You can see the batch file that will clean all temporary files on your computer below.

cd %windir%\Temp
del /s /q %windir%\Temp\*.*
md %windir%\Temp


cd %userprofile%\AppData\Local
del /F /S /P %userprofile%\AppData\Local\*.tmp
md %userprofile%\AppData\Local

cd %userprofile%\AppData\Local\Temp
del /F /S /P %userprofile%\AppData\Local\Temp\*.tmp
md %userprofile%\AppData\Local\Temp

cd %userprofile%\AppData\LocalLow
del /F /S /P %userprofile%\AppData\LocalLow\*.tmp
md %userprofile%\AppData\LocalLow

cd %userprofile%\AppData\Roaming
del /F /S /P %userprofile%\AppData\Roaming\*.tmp
md %userprofile%\AppData\Roaming


This batch file is suitable for Windows Vista and newer operating systems.
We kindly remind that Dr.Batcher is the best program to create batch file, both .BAT and .CMD, try it if haven't tried it yet. 

Thursday, April 25, 2013

Batch File to Send a File to All Computers in Network

Sending a single file to all computers in the network is quite a common task for Windows system administrators. It is handy to have a script that will automate this process. Here is the batch file to send a file to all computers in network:
REM Mass File Sender
REM Sends the specified file to all computers
REM in your network. 


REM Don't forget to replace "compyfile" before
REM executing this script.
SET copyfile=SomeFile.ext
net use S: /delete /y
@net view  | @FOR /F "tokens=1" %%c in ('@find "\\" ') do (
net use S: %%c\c$\bin
COPY %copyfile% S:\ /y
net use S: /delete /y
)

You should just change 'copyfile' to the path to the needful file before executing this script. You should also remember that sending large files can make your network very slow.
We kindly remind that Dr.Batcher is the best program to create batch file, both .BAT and .CMD, try it if haven't tried it yet. 

Tuesday, February 12, 2013

How to Make Changes in Windows Registry Using Batch Files?

 It seems that the simplest way here to make changes in Windows registry is to use REG files. These are special text files that function in Windows for making necessary Windows registry changes without searching long for the key in the tree shown by RegEdit. As I see it, the matter goes about making batch changes in the case you can manage without BAT files at all: you may write several registry keys into one REG file and the keys will be changed while REG file is launched. But if you need to generate registry keys dynamically, BAT file can perform REG file generating and launching. The only question is how to create batch file.
REG-files have a simple format. In fact it is a text file written as follows:
[HKEY_LOCAL_MACHINE\Sub1\Sub2]
"Par1"=dword:0
"Par2"=”string”
"Par3"=hex:cc,1b,00,00,00,40,3d,68

Here, it’s obvious, instead of lines in brackets you should put the real path to the branch in registry which parameters you are intend to change (do not omit brackets). The names of these parameters should be quoted and the same for their values, in case of line parameters. For DWORD and HEX parameters, as in example, write its type and put colon after equals in the line. Do not separate the values somehow – changes in parameters, starting with a new line in brackets, will be made to a new address. At the beginning of the file put the line “Windows Registry Editor Version 5.00” (without quotation marks) – this line is necessary for the Registry Editor to determine whether this file includes the keys for making registry changes – it is very important to include them into REG files. Also, do not forget to name the extension of the output file as .REG.
To compose the file use “>>” - command. That is a standard way to convert BAT file data into a text file. For making changes in the registry you use regedit utility in which REG file’s name should be used as a parameter, for example, “regedit crack.reg” (without quotation marks).

Sunday, August 21, 2011

Retrieving the List of Files Modified Lately

It's quite a common task to retrieve the list of files modified during the latest few days. It is easy to solve this task with the help of batch files:
@echo off
for %%i in (bmp jpg png gif) do (
forfiles -pC:\Test -s -m*.%%i -d+14 -c"cmd /c echo @PATH @FILE"
)
As far as you can see, this batch file searches for images in 'C:\Temp' folder, and shows those of them that were modified during the latest two weeks.
Hope you'll find this small example useful.

Tuesday, May 17, 2011

Another Way to Ping Many IPs at Once

We have already discussed how to ping the given list of IP addresses. Now let's look what we can do if this list isn't placed in a text file. As usual, the solution is very simple:
for /L %%i in (1, 1, 255) do ping 192.168.1.%%i
Of course, you have to use your own subnet mask instead of 192.168.1 in the example above. Hope this small script will help you to write your own powerful batch files.

Sunday, May 8, 2011

Search for Running Process and Start It if It Is not Found

It is quite a common task to start some processes in an automatic way. Of course we usually have no need to run many instances of the application simultaneously, thus we have to check running processes first. Fortunately, batch files are pretty useful in this case and let us solve the problem in a fast and simple way. The solution will look like:
tasklist | findstr /i yourapp.exe 1>nul || start "" "C:\Program Files\Megacorp Inc\yourapp.exe"
You can easily modify this single-line script with the help of our award-winning batch file editor or send your questions and suggestions to us.

Sunday, April 17, 2011

How to Answer Y/N to Console Program from Batch File


Sometimes it is necessary to answer 'Y' or 'N' to a console program during the execution of a batch file. If your file is running under a user's sight, it causes no problems, but if you need to run it in a completely autonomous mode, you have to find a solution.
The solution is really simple: you should just write 'echo Y | ' in your batch file, where the name of a program that wants to know your decision is. Sometimes you should type 'YES' or 'NO' instead of short forms of 'Y' and 'N'. So read carefully the program's manual before passing answers to it.

Tuesday, March 29, 2011

Adding Quotes to Lines in Text

Let's discuss quite a simple task today. Imagine: you need to change each line in a text file by adding quotes to it. I mean you want to get from list like listitem1, listitem2, listitem3... list like "listitem1", "listitem2", "listitem3". This is quite a simple task, and the script solution looks like this:
@echo off
for /f "usebackq tokens=*" %%c in ("about.txt") do (
echo "%%c",>> about1.txt
)

As far as you can see, it's not difficult to write this script with help of our award-winning batch files editor. Hope you'll find this small example useful.

Wednesday, February 9, 2011

How To Print a Line without Closing CR\LF in Batch Files

'Echo' is likely the most often used command in the whole batch scripting. The only disadvantage of this command is ending CR\LF that is always being put after the text. Unfortunately, there is no command that can print text without closing CR\LF in the standard set of commands provided by MS. Thus we have to write quite a large amount of code to implement this feature.
Here is the code:

@ECHO OFF
> #.SCR ECHO N _CHO.COM
>>#.SCR ECHO E 0100 BB 80 00 43 80 3F 0D 75 FA C6 07 24 B4 09 BA 82
>>#.SCR ECHO E 0110 00 39 DA 7F 02 CD 21 B4 4C CD 21
>>#.SCR ECHO RCX
>>#.SCR ECHO 001B
>>#.SCR ECHO W
>>#.SCR ECHO Q
>>#.SCR ECHO.
DEBUG<#.SCR>NUL
DEL #.SCR
_CHO %DATE% >TEST.TXT
_CHO TEST FIRST WORD >>TEST.TXT
_CHO TEST SECOND WORD >>TEST.TXT

As far as you can see, this code produces a small COM file that is able to print text without closing CR\LFs. Of course, you can easily edit the batch script and rename this COM file from _CHO to any name you prefer. Hope you'll find this post useful.

Tuesday, January 25, 2011

Batch File to Retrieve Batch Files from Network Drive and Pack Them


Here is a useful script that administrators can use to retrieve automatically files from the network drive and pack them immediately after that. All the actions are being logged to the LOG file called by default 1.log. You can easily modify this script with help of Dr.Batcher, a powerful utility designed to create and modify batch files.
@echo Starting>> 1.log
@Echo off
@DATE /t >> 1.log
@TIME /T >> 1.log
@echo Copying and logging results ...
@xcopy v:\post\*.doc /z /c /Q >> 1.log
@echo Packing... >> 1.log
@rar a -ag + YYYY:MM:DD:HH:MM \*.doc >> 1.log
@echo Moving the archive to TEMP folder... >> 1.log
@move *.rar TEMP
@echo Removing DOC... >> 1.log
@del /F d:\*.doc
@echo Done... >> 1.log
Have any suggestions on this batch file? Leave your comments!

Wednesday, January 19, 2011

Detecting the Insertion of USB Flash in Batch Files

How to detect that user inserts USB Flash drive and make computer shut down after this? Here you can see step-by-step explanation.
1. Download and install Dr.Batcher, the program designed to create and edit batch files.
2. Start Dr.Batcher and create new empty batch script (File->New->Empty Batch Script).
3. Add command named "Display a message" (Command->Add), click OK in the "Add Command" dialog.
4. Choose "off" as the first parameter and click "OK".
5. Add command named "Set Label".
6. Set loop as a label name, click "OK".
7. Add command named "If (Condition)". Choose "Edit the condition" in the appeared dialog.
8. Choose "File Exists (Condition Only)". 9. Set the letter of the flash drive as the parameter in the following dialog. Note: "\." after the drive letter should not be removed.
9. Add command named "Go to a Label".
10. Set loop as the label name. Click "OK".
11. Following the instructions in paragraphs 5 and 6, add a label with name end.
12. Add command named "Shut Down or Restart Computer".
13. Set the first parameter of this command equal to -r to restart or -s to shut down
14. To make this file start automatically when you log on, place it inside the "Startup" folder in Start menu.
15. Enjoy!

Thursday, January 13, 2011

How to Sort Out Files of Differnet Types with Help of Batch Files

Have you got directories with thousands and thousands of unsorted files? Sure everyone has. I also have folder called 'Downloads' where I place almost everything downloaded from the Web. Usually I clear it once or twice per month and then start to fill it with 'digital garbage' again. But in this case I often lose gems among the garbage, that's this week I decided to sort files in 'Downloads' instead of removing them. I didn't want to lose my time and started Dr.Batcher to create the batch file which had to sort everything without my additional effort. I won't waste your time describing what I tried to do, just give you the code that was created after some experiments.

@ECHO off
REM Creating folders to separate files of different types
IF not EXIST TXT MD TXT
IF not EXIST AVI MD AVI
IF not EXIST MP3 MD MP3
IF not EXIST JPG MD JPG
IF not EXIST RAR MD RAR
IF not EXIST HTM MD HTM
IF not EXIST EXE MD EXE
ECHO Moving text files
MOVE /-Y *.txt TXT
MOVE /-Y *.doc TXT
MOVE /-Y *.rtf TXT
MOVE /-Y *.PDF TXT
ECHO Moving movies
MOVE /-Y *.avi AVI
MOVE /-Y *.mpg AVI
MOVE /-Y *.divx AVI
MOVE /-Y *.xvid AVI
ECHO Moving audio
MOVE /-Y *.mp3 MP3
MOVE /-Y *.wav MP3
MOVE /-Y *.ogg MP3
MOVE /-Y *.wma MP3
ECHO Moving images
MOVE /-Y *.jpg JPG
MOVE /-Y *.bmp JPG
MOVE /-Y *.gif JPG
MOVE /-Y *.png JPG
ECHO Moving archives
MOVE /-Y *.RAR RAR
MOVE /-Y *.ZIP RAR
MOVE /-Y *.gz RAR
MOVE /-Y *.7z RAR
ECHO Moving executables
MOVE /-Y *.exe EXE
ECHO Everything is sorted!
PAUSE

As far as you can see, this script is quite simple though it is long enough. This Flag '/-Y' is added to avoid replacing one file existing in a target folder with a new file with the same name. Hope you'll find this script useful.

Saturday, January 8, 2011

Batch File To Clean Temp Folder

There is a lot of utilities for Windows designed especially for cleaning temporary files in Windows Temp folder and some other places where they usually can be found. Some of these programs are freeware, some of them are paid. With help of small batch script instead of one of the such utilities you can save your money and some space on HDD. Also this batch script gives you great flexibility: you will be able to add new folders to be cleaned if you need.
The script below cleans folder named 'C:\Temp' and makes it default temp folder for those applications who use the folder given in TEMP variable:
@ECHO Cleaning temporary folder...
DEL /S /Q /F C:\TEMP > NUL
SET TMP=C:\TEMP
SET TEMP=C:\TEMP
SET PKTMP=C:\TEMP
@ECHO Temporary folder is cleaned
If you want to clean also Windows default temp folder, you should also add line with 'DEL' for 'c:\Documents and Settings\Your User Name\Local Settings\Temp' (for Windows XP). Of course, the simplest way to modify this batch script is using Dr.Batcher.

Wednesday, January 5, 2011

Funny But Common Mistake

Once Dr.Batcher's support team received a letter describing a problem with running batch file after re-installing Windows. The batch file was called 'shutdown.bat', and its purpose was to turn off computer in single mouse click. It contained the following line of code:
shutdown -s -t 0
Do you know what was wrong? Of course, the renaming file to 'shutdown1.bat' completely solved the problem. This mistake is quite silly and funny, but I consider it to be rather common, that's why I decided to pay your attention on it. When a batch file doesn't work at all, though its code seems to be correct, first of all check its name: probably, it's the cause of its wrong behavior.

Thursday, December 30, 2010

Batch File for IP Change


There is a plenty of users that want to change the IP address of a computer with help of batch files. There is a simple way to perform it, all you need is standard netsh command and some imagination to 'invent' a new IP address.
The following line of code will help you to set the IP address and the subnet mask:
netsh int ip set address name = "LAN" source = static addr = 127.0.0.1 mask = 255.255.255.205
Of course, you have to use real IP address instead of samples given above (127.0.0.1 and 255.255.255.205).
Also we advice you to learn netsh command because it's really useful for different networking operations.

Sunday, December 13, 2009

Creating Directories in Batch Files

Creating new directories using batch files is a common task for system administrators and even home users. But it's not so simple to create a directory with the proper name sometimes. For example, let's consider that we want to create a directory named using the current date and current time. How will you create it through a batch script? Probably you will type something like this:
md %date%%time:~0,2%%time:~3,2%
Now let's execute this code... What a surprise! We get two different folders instead of a single one. Why? Windows includes space into the
folder's name, and thus 'md' command considers creating two directories. Two folders will be created when time is then 10 a.m., e.g. when it can be represented with a single digit.
To solve this problem, we decided to remove space between the date and time. Here is the proper code to create directory with such name:
set d=%date%_%time:~0,2%.%time:~3,2%
set dt=%d: =%
md %dt%
This code will always create a single folder any time you call it.

Translate