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.
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.)
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.
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
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/
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.
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.
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.
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