dumb cmd shell tricks Windows NT/2000: FOR /R recursive traversal of directory

| | Comments (6) | TrackBacks (0)

For is fun! Here are some simple tricks you can use FOR to perform very useful tasks!

Recursive Find Text String In Files, Output Line Number:

FOR /R c:\~kenneth %v IN (*.css) DO find /N /I "#banner" "%~fv" >> test.txt
Note the %v is a variable, could be any letter. The quoted "%~fv" expands to the quoted fully qualified pathname. With out quotes Directories with spaces are not processed.
*.css could be any wildcard or even * for all.
the >> concatenates the output to one file, in this case test.txt. Output looks like this:
[16] #banner {
[29] #banner a,
[30] #banner a:link,
[31] #banner a:visited,
[32] #banner a:active,
[33] #banner a:hover {
[253] #banner-commentspop {

[16] #banner {
[28] #banner a,
[29] #banner a:link,
[30] #banner a:visited,
[31] #banner a:active,
[32] #banner a:hover {
[257] #banner-commentspop {

Copy files out of directory to backup folder:

for /d /r %V IN (*) DO xcopy d:\conversion\borders\*.* %V
Note the case is important on your variable, in this case I used a capitalized V.

Batch convert Microstation .DGN files to Autocad DWG:

FOR /r %%i IN (*.dgn) DO msbatch dwgout createdwg input:%%i output:%%~di%%~pi%%~ni.dwg pausescreen:off
This is so powerful if you even know what Microstation is memorize it.

Recursively convert files:

FOR /r %%i IN (*.dbf) DO perl dbf2csv.pl input:"%%i" output:"%%~di%%~pi%%~ni.xls"
Double percents are used in a CMD file when calling for from a batch file...

Recursively delete:

for /d /r %V IN (*) DO del %V\TNKSPEC.dwg
This recursively deletes the specific file through directory tree!
For Goodness sake use with caution. Testing with DIR is a very safe test!

FOR /D %variable IN (set) DO command [command-parameters]

0 TrackBacks

Listed below are links to blogs that reference this entry: dumb cmd shell tricks Windows NT/2000: FOR /R recursive traversal of directory.

TrackBack URL for this entry: http://www.kennethhunt.com/mt/mt-tb.cgi/175


ole said:

Just an HTML comment...

I would use a padding-left:3px; (on the box ins)

www.kimble.org (for shockwave)

ole said:

BTW, your site is really unique. Toy with CSS uniquely leaves some of those knowledge craving interests grasping for more.

Thanks for sharing the commandline exuberance. Almost as much fun as a heavy handed blimp enthusiast.


Kenneth said:

Put this line in a .cmd file recgzip.cmd for instance:
for /r %%V IN (*) DO gzip -9v "%%V"
This will recursively traverse the entire directory tree from the current directory down and compress every file.

Put this line in a .cmd file recgunzip.cmd for instance:
for /r %%V IN (*.gz) DO gzip -dv "%%V"
this will recursively traverse the entire directory tree from the current directory down and uncompress every file.

use it so:

Kenneth said:

Here's a way to remove a range of AT entries on a Windows 2000 machine:

Using the FOR /L command, from Beginning by STEP to End do the following:
FOR /L %V IN (1,1,24) DO at %V /delete
deletes AT entries 1 through 24 on the local machine.

Kenneth said:

See also Finding and deleting files recursively on linux which includes this example:
find . -name "*.java" -exec rm "{}" \; is better and faster.

Kenneth said:

Here is an example of running FOR from a cmd file:

FOR /R %%v IN (*) DO find /N /I "lock" "%%~fv" >> test.txt

place in a file test.cmd for example and it will search the directory you are in. Generating a log file with headers for each file and a line number for occurances of the file lock.

Note the use of %% which is required when calling the FOR command from a cmd file.

About this Entry

This page contains a single entry by klsh published on February 28, 2003 11:25 PM.

Crypto Python CryptoPy not pyCrypto was the previous entry in this blog.

it was on clearance for $15. What a deal! is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.