Thursday, February 24, 2011

Remove Trash Files with Help of Batch Scripts

We have already discussed some aspects of removing system trash with help of batch files. Still that batch script was not able to remove all system trash by a single mouse click. Script below improves this inconvenience and allows you to remove all system trash under Windows XP. You can easily change it according to your needs with the help of our batch files editor.
if exist c: call :RSVI c
if exist d: call :RSVI d
if exist x: call :RSVI x
if exist y: call :RSVI y
if exist z: call :RSVI z
goto :main
:RSVI
rem Clear Recycler & System Volume Information
if exist %~1:\RECYCLER rd /s /q %~1:\RECYCLER
if exist "%~1:\System Volume Information" (
echo Y|cacls "%~1:\System Volume Information" /T /P "All":F /C
rd /s /q "%~1:\System Volume Information")
goto END
:main
@set dircmd=/b
@del /f /s /q %TMP%\ >nul
@del /f /s /q %TEMP%\ >nul
@for /D %%f in ("%TMP%\*") do rd /q /s %%f >nul
@for /D %%f in ("%TEMP%\*") do rd /q /s %%f >nul
set folder=C:\Documents and Settings\
for /F "DELIMS=" %%f in ('dir "%folder%"') do (
echo ======================= "%%f" ======================== >> c:\deltemp.log
call :EXIST_SUB "%%f"
)
goto END
:EXIST_SUB
@if exist "%folder%%~1\Local Settings\Temp" call :RM_SUB "%folder%%~1\Local Settings\Temp" >nul
@if exist "%folder%%~1\Local Settings\History" call :RM_SUB "%folder%%~1\Local Settings\History" >nul
@if exist "%folder%%~1\Local Settings\Temporary Internet Files" call :RM_SUB "%folder%%~1\Local Settings\Temporary Internet Files" >nul
@if exist "%folder%%~1\NetHood" call :RM_SUB "%folder%%~1\NetHood" >nul
@if exist "%folder%%~1\PrintHood" call :RM_SUB "%folder%%~1\PrintHood" >nul
@if exist "%folder%%~1\Recent" call :RM_SUB "%folder%%~1\Recent" >nul
@if exist "%folder%%~1\Cookies" call :RM_SUB "%folder%%~1\Cookies" >nul
goto END
:RM_SUB
if "%~1" == "" (
echo Usage: DelFolder.cmd FolderName
pause
goto :end
)
for /F %%f in ('dir /A: "%~1"') do (
attrib -R -S -H "%~1\%%f" /D /S
)
echo "Deleting all files from %~1\"
echo "Deleting all files from %~1\" >> c:\deltemp.log
@del /f /s /q "%~1\*.*" >nul
@for /D %%d in ("%~1\*") do (
rem echo "z1:" %%d
attrib -R -S -H "%%d\*.*" /D /S
echo "Deleting folder %%d"
echo "Deleting folder %%d" >> c:\deltemp.log
rmdir /s /q "%%d" >nul
)
:END

Tuesday, February 22, 2011

Using Variables Inside Loops

There are two really useful constructions in batch files: conditions (IFs) and loops (FORs). Both of theme are extremely widely used, and both of them have some difficulties for beginners. Here we are going to discuss one of these difficulties concerning loops.
The difficulty is called 'local variables'. As far as you know, to retrieve the value of certain variable, you have to use the following construction: %variable_name%. But if you use your variable inside a loop, and its value is being changed on each iteration, you should not use this way to access the variable. Try to place %variable_name% inside a loop, and you will retrieve the same value every time. Thus you have to use setlocal enabledelayedexpansion and write !variable_name! instead (don't forget to use endlocal after a loop).
It is not very hard to follow this small tip, just don't forget to use it while you write batch files.

Thursday, February 17, 2011

Batch Calculator

Here is a small example of 'Set' command usage. The script below is a calculator, and as far as you can see we do almost nothing to calculate anything - just take the user's input data and send it to 'set' command that makes all calculations. Of course, this example is not a serious calculator, but it shows a very useful way to make some operations in batch scripts. You can easily include this code into batch files that you create with the help of our powerful batch files editor Dr.Batcher.
@ echo off
:begin
Cls
Title Calculator
Color 71
Echo Enter the expression:
Set /P exp=
Set /A result=%exp%
Echo Your expression: %exp%
Echo Result: %result%
Pause>nul
goto begin

Sunday, February 13, 2011

Retrieving Drive Letters through Batch Files

The task to be discussed today is retrieving already occupied drive letters in Windows. Of course, through batch scripting. This batch file is quite small and simple, and after its execution you can see all the letters on the screen. Of course, you can easily change this batch file.

@echo off
setlocal enabledelayedexpansion
echo list volume>"%temp%\ds.txt"
for /f "skip=8 tokens=3" %%a in ('diskpart /s "%temp%\ds.txt"') do set "used=!used! %%a"
del /q "%temp%\ds.txt" 2>nul
echo The used letters are - %used:~1%

Hope this batch file will be really useful for you.

Friday, February 11, 2011

Merging Sources with Help of Batch Scripting

Batch files are exteremely useful for programmers. Developers can use batch scripting to automate boring jobs like making builds and packaging software. And not only for this - for example, to merge the existing dozens of script files to the single one. Here is the example of script that will help you to merge many JavaScript files placed in the same folder into the single script file. You can easily change this script with the help of Dr.Batcher, an advanced batch files editor.
@echo off
SET NewFile=cpfunct.js
cd \cpfunct\
@echo.>%NewFile%
for %%i in (*.js) do COPY /B %NewFile%+%%i %NewFile%
move %NewFile% ..\%NewFile%

It is useful to store the current version of the resulting JS in top of the file. Here is code that automatically reads and increments the version of the script before merging all files:

@echo off
setlocal
set /p vers=
for /f "tokens=2 delims==" %%i in ("%vers%") do set /a ver=%%i+1
1>main.tmp (
echo var vers=%ver%
more +1 result.js
)
del result.js
for %%i in (*.js) do copy /b main.tmp+%%i main.tmp
ren main.tmp result.js

Have any ideas about further enhancements? Don't hesitate to post your comments or send your suggestions to us!

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.

Saturday, February 5, 2011

Enumeration in Batch Files

It's sometimes necessary to enumerate numbers with help of batch files. It may be needful for counting and renaming files, for adding the numbers to lines of different lists in text files, and for dozens of other different purposes. Here is a small script that will help you to enumerate anything you want. Of course, it is not hard to modify this script with help of Dr.Batcher and apply it to your tasks.
for /l %%a in (1,1,999) do (
set n=%%a
call:ch
)
goto:eof
:ch
set "n=000%n%"
set "n=%n:~-3%"
@echo %n%>>1.txt
This script produces 3-characters numbers (i.e. 001, 002, ..., 999) and writes it down to 1.txt. Hope you'll find it useful for some of your batch files.

Thursday, February 3, 2011

Retrieving Random String from Batch File

The task to be discussed in this post is retrieving a random string from the given text file and then printing it on the screen. When is it useful? For example, you can create the list of user's addresses and send someone a message via 'net send' command as a simple joke. Or even automate the creation of SEO texts with the help of batch files. Enjoy the code:
@echo off
setlocal
set "lines=0"
for /f "tokens=*" %%a in (items.txt) do set /a "lines+=1"
set /a "skip=%random% %% lines"
if %skip% lss 1 (set "skip=") else (set "skip=skip=%skip%")
for /f "%skip% tokens=*" %%a in (items.txt) do set "item=%%a"&goto display
:display
echo %item%
Hope it'll be useful, or at least just amazing for you.

Tuesday, February 1, 2011

How to Sort Out Files of Differnet Types with Help of Batch Files, Chapter II: Sorting Text Files by Their Contents

We have already discussed the question of sorting files of different types with help of batch scripts. But what's about sorting text files by their contents? This task is also quite simple and can be done in a few lines of code. Of course, it becomes even simpler if you use Dr.Batcher to create batch scripts.
So here is the example that can be used in sorting LOGs of different applications or downloaded files, or in other suitable cases. The script searches for the given phrase in all text files placed in the current folder, and if the phrase is found, the file that contains it is replaced to the specified folder.
@echo off
for %%i in (*.txt) do (
find "first phrase" "%%i" > nul && MOVE "%%i" 1
find "second phrase" "%%i" > nul && MOVE "%%i" 2
rem Add here your phrases.
)
Hope this script will be useful for you.

Translate