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.

Thursday, January 27, 2011

How to Export Registry Keys Through Batch File

Long time ago I wrote a post on having a deal with Windows registry in batch files. It's time to return to this topic and share a small, but still very useful batch file that will help you to export registry keys to a REG file. This code is really simple, still you can include it into complex script that you create with help of our powerful batch files editor.
Here is the code of this batch file:
@echo off regedit /ea %Temp%\ChkReg.txt "HKEY_CURRENT_USER\Software\MyPrg-1" if exist %Temp%\ChkReg.txt type %Temp%\ChkReg.txt>reg.reg regedit /ea %Temp%\ChkReg.txt "HKEY_LOCAL_MACHINE\Software\MyPrg-1" if exist %Temp%\ChkReg.txt findstr /v /b /i "REGEDIT4" %Temp%\ChkReg.txt>>reg.reg if exist %Temp%\ChkReg.txt del %Temp%\ChkReg.txt
As far as you can see, first of all we export the necessary keys to ChkReg.txt, than copying data itself to REG file, and then remove the temporary TXT. Hope this script will be useful for you.

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!

Saturday, January 22, 2011

Batch File To Restart Explorer And Reopen Windows

Though this is not a very common task, I suppose this example of batch scripting is still very useful for advanced users of batch files. You can change anything you want in a couple of minutes, especially with help of our powerful and handy batch files editor.
Here is the code to restart Windows Explorer and reopen all windows of the running applications:
@echo off setlocal enabledelayedexpansion for /f "usebackq tokens=8*" %%a in (`"cmdow /t /f | find /i "explorer""`) do ( if /i "%%a"=="explorer" set "open_windows=!open_windows! "%%b"" ) taskkill /f /im explorer.exe>nul start explorer for %%a in (%open_windows%) do ( if /i %%a=="My Computer" (start /min explorer shell:DriveFolder) else ( start /min explorer %%a) )
Hope you'll find this code useful.

Translate