Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

How to recursively traverse dirs

Original Message
Name: BMillikan
Date: February 14, 2008 at 09:27:25 Pacific
Subject: How to recursively traverse dirs
OS: DOS
CPU/Ram: Intel
Model/Manufacturer: Any
Comment:
I'm trying to write a DOS batch script to recursively traverse a directory tree and then concatenate the files into a single file with a different extension in each directory. I'm not really a good batch script writer. I'm more of a UNIX script (korn shell, bash shell, etc) writer. I'd appreciate any help with this.

Thanks,
Brian


Report Offensive Message For Removal


Response Number 1
Name: Razor2.3
Date: February 15, 2008 at 06:00:47 Pacific
Subject: How to recursively traverse dirs
Reply: (edit)
DOS? You can't.

But why are you writing scripts for a decade dead OS? You really should move on to the WinNT line.


Report Offensive Follow Up For Removal

Response Number 2
Name: klint
Date: February 20, 2008 at 03:34:11 Pacific
Subject: How to recursively traverse dirs
Reply: (edit)
Razor, I think you've put the poor chap off. After all, he's just a lowly Unix type and hasn't experienced the expressive power of the Windows Command Processor.

(I bet he thinks he's running DOS but is actually running Windows XP without realising how much better (than Unix shells ;-) ) the Windows command shell is.)


Report Offensive Follow Up For Removal

Response Number 3
Name: BMillikan
Date: February 20, 2008 at 05:43:43 Pacific
Subject: How to recursively traverse dirs
Reply: (edit)
I'm looking for answers, not insults please. Instead of insulting me, how about telling me about Windows Command Processor and how to invoke it, etc. There's nothing lowly about UNIX, especially the bash shell. Volumes have been written on it because it is so powerful.

Report Offensive Follow Up For Removal

Response Number 4
Name: klint
Date: February 20, 2008 at 07:35:53 Pacific
Subject: How to recursively traverse dirs
Reply: (edit)
Sorry if you felt insulted, it was tongue-in-cheek.

I know, I use various Unix shells too. Some people think the command shell on Windows is a toy, which is not the case (although it is quirky and in some places unintuitive.) The FOR command can be used to traverse directory trees, when used with the /R switch. Type FOR /? for help on the FOR command's usage.

To concatenate multiple files into one (like the Unix cat command), you can use the copy command.

You should be aware that Windows has two types of file: text and binary (whereas there is no distinction on Unix.) The difference is basically to do with the line terminator (LF vs CR LF). It is usually safer to treat files as binary, using the COPY /B switch. To concatenate three files, use:

COPY /B src1 + src2 + src3 dst

To concatenate all files in the current directory to a new file (called new.dat):

COPY /B * new.dat

So to put it all together, I think you want something like this (warning: untested):

@echo off
pushd .
for /r %d in (.) do (
cd %d
copy /b * alldata.dat
)
popd


Report Offensive Follow Up For Removal

Response Number 5
Name: FishMonger
Date: February 20, 2008 at 08:02:29 Pacific
Subject: How to recursively traverse dirs
Reply: (edit)
BMillikan,

Recursive directory traversal can't be done in the Windows command shell; it's not powerful enough. Your best bet is to install the *nix tools/utilities i.e., cygwin.
http://www.cygwin.com/

My preference would be to use Perl.
http://activestate.com/store/produc...

Here are 2 Perl modules that will do recursive directory traversal with as little as 2 simple lines of code.

File::Find
http://search.cpan.org/~rgarcia/per...

IO::Dir::Recursive
http://search.cpan.org/~flora/IO-Di...


Report Offensive Follow Up For Removal


Response Number 6
Name: klint
Date: February 20, 2008 at 08:30:07 Pacific
Subject: How to recursively traverse dirs
Reply: (edit)
FishMonger,

"Recursive directory traversal can't be done in the Windows command shell"

It can - check out the FOR /R command.

klint


Report Offensive Follow Up For Removal

Response Number 7
Name: FishMonger
Date: February 20, 2008 at 08:57:01 Pacific
Subject: How to recursively traverse dirs
Reply: (edit)
Here's the test I ran
C:\temp>for /r %d in (.) do echo %d >> file.lst

Here's the contents of file.lst

C:\temp\. 
C:\temp\BFormMail\.
C:\temp\guestbook\.
C:\temp\M2go\.
C:\temp\rosys\.
C:\temp\rosys\fishmonger\.
C:\temp\rosys\www\.
C:\temp\rosys\www\images\.
C:\temp\rosys\www\OLD\.
C:\temp\rosys\www\OLD\images\.
C:\temp\test\.
C:\temp\tony\.

I'd say that it's processing the directories in alphabetical order, which is not recursion.

I also tested:

C:\temp>for /r %d in (.) do dir /b %d >> file.lst

Not only did it traverse the directories in alphabetical order, but it listed the files within those directories in alphabetical order as well. As far as I know, that's not a recursive processing.


Report Offensive Follow Up For Removal

Response Number 8
Name: klint
Date: February 20, 2008 at 09:10:00 Pacific
Subject: How to recursively traverse dirs
Reply: (edit)
What do you expect it to do?

Just because it goes to the trouble of sorting the entries for your convenience, does that make it wrong?


Report Offensive Follow Up For Removal

Response Number 9
Name: FishMonger
Date: February 20, 2008 at 09:26:26 Pacific
Subject: How to recursively traverse dirs
Reply: (edit)
I'd expect a recursive processing to process from deepest to shallowest within each part of the tree, not alphabetically.

In a recursive process, I'd expect
C:\temp\rosys\www\OLD\images\.

to be processed before
C:\temp\rosys\www\OLD\.


Report Offensive Follow Up For Removal

Response Number 10
Name: klint
Date: February 20, 2008 at 09:47:48 Pacific
Subject: How to recursively traverse dirs
Reply: (edit)
That's not necessarily the definition of recursion. In fact, I'm pretty sure the command processor uses recursion to traverse the directory tree (although that's an implementation detail, and the user should not be concerned with how a function is implemented, only what it does.)

Compare with the find command on Solaris.

% mkdir t
% touch t/a
% mkdir t/t
% touch t/t/a
% find . -name a
./t/a
./t/t/a

The same order of traversal as you would get on Windows. Except that on Solaris, the order depends on the order in which the directory entries were created. If you created ./t/t/a before ./t/a, the find command would give you it in that order. That's pretty random.

Now, coming back to the concept of recursion, whether a traversal algorithm lists it depth-first or not depends on whether it prints the directory name before or after it recurses into it. Either way, it's still recursion.

In any case, this is academic. The order in which each directory is processed makes no difference to the original poster's problem.


Report Offensive Follow Up For Removal

Response Number 11
Name: Razor2.3
Date: February 20, 2008 at 17:28:00 Pacific
Subject: How to recursively traverse dirs
Reply: (edit)
BMillikan: I'm looking for answers, not insults please. Instead of insulting me, how about telling me about Windows Command Processor and how to invoke it, etc.
I'd point out that you asked about DOS and wrote your OS as DOS. If the OS had been Windows, WinXP, Win2K, Win<ANYTHING> I'd have my jab and then give you information about CMD. But that's not what you asked.

After all, if you asked about Solaris, it wouldn't do for me to give you an answer about BSD. Instead, I'd give you an answer about Solaris.


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: How to recursively traverse dirs 

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




acer 312T BIOS problem

K7 Turbo possible max fsb?

Pc anywher problem

WinFLP & OE/Outlook2003

Computer resets after a few minutes


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC