Showing posts with label loops. Show all posts
Showing posts with label loops. Show all posts

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.

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:
name[1].ext
name[2].ext
name[3].ext
name[4].ext
name[5].ext
...
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.

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.

Friday, March 25, 2011

How to Register Many DLLs/OCXs at Once

It is sometimes necessary to register a COM or ActiveX library. As far as you know, it is easy to perform it with help of Regsvr32 utility by Microsoft. But it is hard to type the name of library to be registered each time when you need to register a lot of them. It's really easier to copy all needful files to a single folder and then register them all with help of really simple script:
for %%i in (c:\DLL\ActiveX\*.dll) do regsvr32 %%i
for %%i in (c:\DLL\ActiveX\*.ocx) do regsvr32 %%i
Please note that you can register even remote files
for %%i in (\\192.168.102.184\DLL\*.dll) do regsvr32 %%i
for %%i in (\\192.168.102.184\DLL\*.ocx) do regsvr32 %%i
Hope you'll find this example useful.

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.

Translate