DOS Traverse directory and file code (mainly with the for command) _dos/bat

Source: Internet
Author: User
Tags goto
===== folder Structure =============================================
D:\test
---A Folder 1
|-----A File 1.txt

|-----A File 2.txt
|-----A File 3.txt
---B Folder 2
|-----B File 1.txt
|-----B File 2.txt
|-----B File 3.txt
|---B Folder 3
|-----B Sub File 1.txt
|-----B Sub File 2.txt
|-----B Sub File 3.txt

Copy Code code as follows:

@echo off
Set Work_path=d:\test
D:
CD%work_path%
FOR/R%%s in (., *) Do (
Echo%%s
)
Pause

Results
D:\test\.
D:\test\A Folder 1\.
D:\test\A Folder 1\a File 1.txt
D:\test\A Folder 1\a File 2.txt
D:\test\A Folder 1\a File 3.txt
D:\test\B Folder 2\.
D:\test\B Folder 2\b File 1.txt
D:\test\B Folder 2\b File 2.txt
D:\test\B Folder 2\b File 3.txt
D:\test\B folder 2\b folder 3\.
D:\test\B Folder 2\b folder 3\b sub file 1.txt
D:\test\B Folder 2\b folder 3\b sub file 2.txt
D:\test\B Folder 2\b folder 3\b sub file 3.txt
Copy Code code as follows:

@echo off
Set Work_path=d:\test
D:
CD%work_path%
FOR/R%%s in (.) Do (
Echo%%s
)
Pause

Results
D:\test\.
D:\test\A Folder 1\.
D:\test\A Folder 1\a File 1.txt
D:\test\A Folder 1\a File 2.txt
D:\test\A Folder 1\a File 3.txt
D:\test\B Folder 2\.
D:\test\B Folder 2\b File 1.txt
D:\test\B Folder 2\b File 2.txt
D:\test\B Folder 2\b File 3.txt
D:\test\B folder 2\b folder 3\.
D:\test\B Folder 2\b folder 3\b sub file 1.txt
D:\test\B Folder 2\b folder 3\b sub file 2.txt
D:\test\B Folder 2\b folder 3\b sub file 3.txt

So
Copy Code code as follows:

FOR/R%%s in (., *) Do (
Echo%%s
)

And
Copy Code code as follows:

FOR/R%%s in (.) Do (
Echo%%s
)

What is the difference?
In the case of CD%work_path%, the results of these two commands are the same, as we have shown above. But
FOR/R%%s in (., *) Do (
Echo%%s
)
Does not have a CD%work_path% in the batch, it will display the traversal results under the folder where the batch file resides.

Copy Code code as follows:

@echo off
FOR/R "D:\test"%%s in (.) Do (
Echo%%s
)
Pause

Results
D:\test\.
D:\test\A Folder 1\.
D:\test\B Folder 2\.
D:\test\B folder 2\b folder 3\.
Copy Code code as follows:

@echo off
FOR/R "D:\test"%%s in (., *) Do (
Echo%%s
)
Pause

Results
D:\test\.
D:\test\A Folder 1\.
D:\test\A Folder 1\a File 1.txt
D:\test\A Folder 1\a File 2.txt
D:\test\A Folder 1\a File 3.txt
D:\test\B Folder 2\.
D:\test\B Folder 2\b File 1.txt
D:\test\B Folder 2\b File 2.txt
D:\test\B Folder 2\b File 3.txt
D:\test\B folder 2\b folder 3\.
D:\test\B Folder 2\b folder 3\b sub file 1.txt
D:\test\B Folder 2\b folder 3\b sub file 2.txt
D:\test\B Folder 2\b folder 3\b sub file 3.txt

It's a different thing to see.

And look at a =================================.
Copy Code code as follows:

@echo off
FOR/R "D:\test"%%s in (*) do (
Echo%%s
)
Pause

Results
D:\test\A Folder 1\a File 1.txt
D:\test\A Folder 1\a File 2.txt
D:\test\A Folder 1\a File 3.txt
D:\test\B Folder 2\b File 1.txt
D:\test\B Folder 2\b File 2.txt
D:\test\B Folder 2\b File 3.txt
D:\test\B Folder 2\b folder 3\b sub file 1.txt
D:\test\B Folder 2\b folder 3\b sub file 2.txt
D:\test\B Folder 2\b folder 3\b sub file 3.txt
is to show only the files
Visitf.bat-The program that specifies the file to be traversed for the specified path
Copy Code code as follows:

:: Visitf.bat-Program to traverse a specified path to specify a file
:: The first parameter is the file to be traversed (wildcard characters are supported), the second parameter is the path to traverse (the default is C packing)
@echo off
: Main
If [%1]==[] if not exist goto end
: Init
If exist if exist goto loop
Set file=%1
Set base=%2
If [%2]==[] set Base=c:
Dir%base%\%file%/s/a/b >
Echo e ' Set file= ' >
echo W >>
echo Q >>
: Loop
FC Nul/n | Find "1:" > Setfile.bat
if errorlevel 1 goto restore
Debug Setfile.bat nul
Call Setfile.bat
Echo visiting the file:%file%
:: User specified visit code replace this line
Find "%file%"/V
Copy > Nul
Goto Loop
: Restore
if exist del
if exist del
if exist del
If exist Setfile.bat del Setfile.bat
: End


Visitd.bat-Program to traverse the specified path specified directory
Copy Code code as follows:

:: Visitd.bat-Program to traverse the specified path specified directory
:: The first parameter is the directory to traverse (wildcard characters are supported), the second parameter is the path to traverse (the default is C packing)
@echo off
: Main
If [%1]==[] if not exist goto end
: Init
If exist if exist goto loop
Set dir=%1
Set base=%2
If [%2]==[] set Base=c:
Dir%base%\%dir%/s/ad/b >
Echo e ' Set dir= ' >
echo W >>
echo Q >>
: Loop
FC Nul/n | Find "1:" > Setdir.bat
if errorlevel 1 goto restore
Debug Setdir.bat nul
Call Setdir.bat
echo Visiting the dir:%dir%
:: User specified visit code replace this line
Find "%dir%"/V
Copy > Nul
Goto Loop
: Restore
if exist del
if exist del
if exist del
If exist Setdir.bat del Setdir.bat
: End

Visitl.bat-a program that iterates through files in a specified list of files
Copy Code code as follows:

:: Visitl.bat-Program to traverse a file in a specified file list
:: parameter is the list of files to traverse
@echo off
: Main
If [%1]==[] if not exist goto end
: Init
Set filelist=%1
If [%1]==[] set filelist=
If not exist%filelist% Goto end
Copy%filelist% > nul
If exist Goto loop
Echo e ' Set file= ' >
echo W >>
echo Q >>
: Loop
FC Nul/n | Find "1:" > Setfile.bat
if errorlevel 1 goto restore
Debug Setfile.bat nul
Call Setfile.bat
Echo visiting the file:%file%
:: User specified visit code replace this line
Find "%file%"/V
Copy > Nul
Goto Loop
: Restore
if exist del
if exist del
if exist del
If exist Setfile.bat del Setfile.bat
: End


Visiti.bat-The program that iterates through the files that specify dir information for the specified path
Copy Code code as follows:

:: Visiti.bat-Program that iterates through the files that specify dir information for the specified path
:: The first parameter is the specified dir information entry, and the second parameter is the path to traverse (the default is the current path)
:: Note: Dir information item can be file name, extension, date, time, etc. dir command provides directory information items
:: You can use multiple items at the same time, but you must add a pair of quotes, and the parameter format must be in strict accordance with Dir's information format
@echo off
: Main
If [%1]==[] goto END
: Init
If exist if exist goto loop
Set info=%1
Set base=%2
If [%2]==[] set base=.
Dir%base%\. /s/a/b >
echo E "' Set file= '" ' >
echo W >>
echo Q >>
: Loop
FC Nul/n | Find "1:" > Setfile.bat
if errorlevel 1 goto restore
Debug Setfile.bat nul
Call Setfile.bat
Dir "%file%" | Find%info% > Nul
If not errorlevel 1 echo Visit file: "%file%"
:: If not errorlevel 1
Find "%file%"/V
:: The "%file%" parameter determines that all files and directories under the matching subdirectory will not match again
Copy > Nul
Goto Loop
: Restore
if exist del
if exist del
if exist del
If exist Setfile.bat del Setfile.bat
Set info=
Set file=
Set base=
: End


Visit.bat-File Traversal batch Program
Copy Code code as follows:

:: Visit.bat-File Traversal batch Program
:: Would sort-10/17/2004-v2
::
:: Program use:
:: Perform the specified action on the specified set of files/directory set/File list
::
:: Command-line Description:
:: 1. VISIT file set/catalog set [parameters]
:: Performing a specified operation on a set of files/directories
:: 2. VISIT @ file List
:: Performs a specified action on a file in the specified file list
::
:: Attention Matters:
::-A file Set/directory set can contain valid paths and wildcard characters
::-Path defaults to current path, file set defaults to *.* (not all files)
::-File Set/catalog set with double quotation marks when containing spaces
::-[parameters] supported dir switch:/s/a/o/l etc not with/b conflict
::-[parameters] not supported dir switch:/w/p/V etc with/b conflict person
::-[operations] written in Visitcmd.bat by the caller
::-[actions] Use the%visitfile% reference to traverse the file
::-Program checks to see if the file list exists, but does not check whether it is valid
::-Do not traverse directories and files in hidden/system directories (exceptions specified on the command line)
::
:: Usage Examples:
:: Visit C:\/ad/s traverse all directories in C disk, including subdirectories
:: Visit "C:\My document"/a-d Traverse all Files under "C:\My document"
:: Visit c:\*.zip/a/s traverse C-disk all. zip compression packs, including subdirectories
:: If you want to traverse multiple file/directory sets, you can use the DIR file set/a/s>> file list multiple times
:: Generate a complete list of files, then use the file list for traversal; or use Visitce.bat
:: When traversing a hidden/system directory that is not explicitly specified, you can use the attrib file set/s to generate
:: A list of files, and then refer to the%visitfile% third to last string in Visitcmd.bat code.
:: Re-use the file list for traversal
::
:: Test Report:
:: The limited black-box test is passed on the Win98 command line
:: Performance is still the biggest bottleneck
::
@echo off
If "%1" = = "@" goto copylist
: makelist
Dir/b%1%2%3%5%6 > ~
Find "~"/V < ~ > ~
If not errorlevel 1 copy ~ ~>nul
Goto Makepreline
: Copylist
If not [%2]==[] if exist%2 copy%2 ~>nul
If not exist ~ Goto END
: Makepreline
echo Set visitfile=> ~
For%%c in (RCX e w q) do echo%%c>> ~
Debug ~ < ~ > nul
If [%os%]==[windows_nt] Chcp 936 > nul
: Loopvisit
Copy ~+~ ~ > nul
Find "Set visitfile=" < ~ > ~visit.bat
Call ~visit.bat
If "%visitfile%" = "" goto clear
If not exist visitcmd.bat echo visiting%visitfile%
If exist Visitcmd.bat call Visitcmd.bat
Find "Set visitfile="/V < ~ > ~
Goto Loopvisit
: Clear
Del ~visit.*
Set visitfile=
: End

Visitce.bat-File Traversal Batch program command line enhanced version
Copy Code code as follows:

:: Visitce.bat-File Traversal Batch program command line enhanced version
:: Would sort-10/17/2004-v2ce
::
:: Program use:
:: Performs the specified action on the specified file/directory set under the specified path/file list
::
:: Command-line Description:
:: 1. VISIT [path 1 path 2 ...] [Switch 1 switch 2 ...] [File Set 1 file Set 2 ...]
:: Perform specified actions on [file sets] with [path] and [switch] qualified
:: 2. VISIT @ file list 1 [file List 2 ...]
:: Perform a specified action on a file in the specified [file list]
::
:: Attention Matters:
::-[path] [parameters] [file sets] are optional or multiple
::-[path] cannot contain wildcard characters, [file set] can contain valid paths and wildcard characters
::-[path] defaults to the current path, [file set] defaults to *.* (not all files)
::-[path] [file set] double quotation marks are required to contain spaces
::-[parameters] supported dir switch:/s/a/o/l etc not with/b conflict
::-[parameters] not supported dir switch:/w/p/V etc with/b conflict person
::-[operations] written in Visitcmd.bat by the caller
::-[actions] Use the%visitfile% reference to traverse the file
::-Program checks to see if the file list exists, but does not check whether it is valid
::-Do not traverse directories and files in hidden/system directories (exceptions specified on the command line)
::
:: Usage Examples:
:: Visit C:\/ad/s traverse all directories in C disk, including all subdirectories
:: Visit "C:\My document"/a-d Traverse all Files under "C:\My document"
:: Visit C:\ d:\ e:\/s/a/on traverse all files in C,d,e and sort by file name
:: Visit \ A traverse all files and directories under current packing
:: When traversing a hidden/system directory that is not explicitly specified, you can use the attrib file set/s to generate
:: A list of files, and then refer to the%visitfile% third to last string in Visitcmd.bat code.
:: Re-use the file list for traversal
::
:: Test Report:
:: The limited black-box test is passed on the Win98 command line
:: Performance is still the biggest bottleneck
::
@echo off
If "%1" = = "$" goto makelist
If "%1" = = "@" goto copylist
If "%1" = "" Goto end
Set visitcommand=%0
: Getargu
: GetPath
If not exist%1.\nul goto Getswitch
Set visitpath=%visitpath%%1
Goto GetNext
: Getswitch
Echo%1 | Find "/" > nul
if errorlevel 1 goto getfilter
Set visitswitch=%visitswitch%%1
Goto GetNext
: GetFilter
Echo%1 | Find "*" > Nul
If not errorlevel 1 goto setfilter
Echo%1 | Find "?" > Nul
if errorlevel 1 goto end
: SetFilter
Set visitfilter=%visitfilter%%1
: GetNext
Shift
If not [%1]==[] goto Getargu
%visitcommand% $%visitfilter%
: makelist
If not [%visitpath%]==[] goto Formake
:D Irmake
Dir%2/b%visitswitch% >> ~
Goto Makenext
: Formake
For%%p in (%visitpath%) do dir%%p.\%2/b%visitswitch% >> ~
: Makenext
Shift
If not [%2]==[] goto makelist
Find "~"/V < ~ > ~
If not errorlevel 1 copy ~ ~>nul
Goto Makepreline
: Copylist
If not [%2]==[] if exist%2 type%2>>~
Shift
If not [%2]==[] goto Copylist
: Makepreline
If not exist ~ Goto END
echo Set visitfile=> ~
For%%c in (RCX e w q) do echo%%c>> ~
Debug ~ < ~ > nul
If [%os%]==[windows_nt] Chcp 936 > nul
: Loopvisit
Copy ~+~ ~ > nul
Find "Set visitfile=" < ~ > ~visit.bat
Call ~visit.bat
If "%visitfile%" = "" goto clear
If not exist Visitcmd.bat echo visiting%visitpath%%visitswitch%%visitfilter%-%visitfile%
If exist Visitcmd.bat call Visitcmd.bat
Find "Set visitfile="/V < ~ > ~
Goto Loopvisit
: Clear
For%%f in (~visit.*) Todo del%%f
For%%e in (Command Path Switch Filter File) do set visit%%e=
: End

There's a similar problem under 2000SP4, the second problem is actually the incompatibility of FC, which, when displayed by line number, has 8 byte bits before each line of text, instead of the 9 under DOS6 and Win9x, so it's easy to solve, and the E-M ' Set file= ' in the debug script "Set fil=" can be changed to E.

But that's just the thing, the above mentioned three batches, but my training in DOS under the test prototype, done very rough, since the formal steering Win98 this March, the use of a lot of new problems, such as the file name contains a space problem, System/hidden attribute directory problems In NT, it encounters the problem of Chinese filename, and because of the excessive text flow control in the loop body, the efficiency is severely low. In addition, three programs function and implementation is not much different, there is no need to split.
Above all, rewrite the above code becomes necessary. Merging code, remedying vulnerabilities, improving performance, writing documents, there is the so-called V2 version, which actually did not spend a lot of effort.

However, an accidental afternoon, sudden whim, why not the visit command line function to do an enhancement, such as supporting multiple paths, multiple filters (filter), multiple file list, the original want to just add a command line parameter analysis loop, but truly realized, but once again realized the complexity of the command line, if For nesting, flush directed output, determine the directory when the root directory and subdirectory of the different, for the filter of low energy replacement (Win9x/dos), a new problem crowds, endless, unexpectedly consumed me half of the workday, only reluctantly achieved a rough version, this is called the V2CE version , and I have not much confidence in the rigorous testing.

Now, looking back at my work, in fact that v2ce version is not much of the application value. Multiple paths and filters, it can be done by calling visit more than once, and in fact, we use only one path and filter in most cases, and multi-parameter control brings a drop in usability, users always have a certain psychological barrier to the program with many parameters, and my personal hobby of DOS compression software, From high to low, rar,zip,arj in turn, ease of use is the first quantity; In addition, the most important thing is that the Multi-parameter control led to a significant increase in code volume and complexity, which once let me into the if for the construction of the Ghost maze and do not know North and South West, the program's readability is getting worse, Debugging is also more and more difficult, at the same time the compatibility of pull more Chinmaso.

These are the costs of functional ascension, although this is a more extreme example, but the balance between function and simplicity is evident in the importance of programming. Affinity of the interface does not mean that the affinity of the code, human needs do not mean that the human implementation, blindly to pass on the complexity of the user side to the designer (such as Windows), is not a very clever idea, and vice versa (such as Linux), This does not take into account the double pressure on the designer and the consumer by the increased code functionality.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.