Tuesday, April 26, 2011

Another Example with Renaming Files

Renaming files is likely the most common task being automated with the help of batch files. Here we will discuss how to add the date of file's creation to its name. As far as you can see, the script to automate this task is quite simple, but you can enhance it with the help of our powerful batch files editor.
@echo off
for /f %%i in ('dir /b') do (
set aaa=%time%
set a1=!aaa:~0,2!
set a2=!aaa:~3,2!
set a3=!aaa:~6,2!
ren "%%i" "!a1!-!a2!-!a3!_%%i"
If you run this script, you will see that it adds the date before the name of the file. It is not hard to modify the script to make it add the date to the end of batch file.
And here is also another version of this task's automation:
@echo off
for /f %%i in ('dir /b') do (
set "file=%%i"
call :ert
goto :eof
ren "%file%" "%time:~0,2%-%time:~3,2%-%time:~6,2%_%file%"
Both of these scripts are quite quick, so in general you can use any of them.

Saturday, April 23, 2011

How to Remove Numbers from Filenames

Let's discuss another simple task that can be automated with help of batch files. The task is to remove numbers from filenames like these:
So what do you have to do? You can use a simple script like this:
@echo off for /f "tokens=1,2,3 delims=[]" %%a in ('dir /b *[1].*') do ren %%a[%%b]%%c %%a%%c
Hope you'll find it useful.

Wednesday, April 20, 2011

Copying Strings from One File to Another

The task to be discussed in this post looks very simple: copy some strings from a text file to another one. The file and the amount of strings are given. Here you can see the solution:
@echo off
setlocal enabledelayedexpansion
set "N=17"
set "count=0"
for /f "tokens=*" %%a in (first_file.txt) do (
if !count! GEQ !N! goto :EOF
echo %%a>>second_file.txt
set /a "count+=1"

But you can also try another version of this script. It will look like this (you should declare variables to run this script):
set count=0
for /f "skip=2 tokens=*" %%a in (%file_name%) do (
set /a count=!count!+1
if /i !count! leq N echo %%a>>new.txt
Hope you'll find these scripts useful.

Tuesday, April 19, 2011

Dr.Batcher 2.1.5 is Released

We are glad to introduce the new version of our award-winning software to create batch files. In this version added French translation, support for checking used bookmarks on toolbar, small bugfixes.
Click here to download it right now. Hope you'll enjoy our work.

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.

Friday, April 15, 2011

How to Turn Off Screen Output in Console

Not long ago we received a letter with the following question:
How to make a command not to type anything while its execution? I tried CLS but it makes nothing...
Though our award-winning batch files editor Dr.Batcher allows you to create scripts very easy, it still doesn't let you just check a box to turn screen output off (we'll implement this feature one day, we promise). So you should use the following instruction:
ping > nul
Of course, you can use any command you like, not only 'ping'. The main trick is '> nul'. It redirects the output from console to... nowhere! So you won't see anything except the command's name and given parameters themselves.