Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to traverse directories and files under dos

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to traverse directories and files under dos, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

= folder structure =

D:\ test

-A Folder 1

| |-A file 1.txt |

| |-A file 2.txt |

| |-A file 3.txt subscription |

-B Folder 2

| |-B file 1.txt |

| |-B file 2.txt |

| |-B file 3.txt subscription |

| |-B Folder 3 |

| |-B sub file 1.txt |

| |-B sub file 2.txt |

| |-B sub file 3.txt |

Cymbal

The code is as follows:

@ echo off

Set work_path=D:\ test

D:

Cd work_path%

For / R% s in (., *) do (

Echo s

)

Pause

Result

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

The code is as follows:

@ echo off

Set work_path=D:\ test

D:

Cd work_path%

For / R% s in (.) Do (

Echo s

)

Pause

Result

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

that

The code is as follows:

For / R% s in (., *) do (

Echo s

)

And

The code is as follows:

For / R% s in (.) Do (

Echo s

)

What's the difference?

When you have cd% work_path%, the results of these two commands are the same, as in our example above. But

For / R% s in (., *) do (

Echo s

)

If there is no cd% work_path% in the batch, the traversal result under the folder where the batch file is located will be displayed.

The code is as follows:

@ echo off

For / R "D:\ test"% s in (.) Do (

Echo s

)

Pause

Result

D:\ test\.

D:\ test\ A Folder 1\.

D:\ test\ B Folder 2\.

D:\ test\ B Folder 2\ B Folder 3\.

The code is as follows:

@ echo off

For / R "D:\ test"% s in (., *) do (

Echo s

)

Pause

Result

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

In that case, you can see the difference.

Look at another one.

The code is as follows:

@ echo off

For / R "D:\ test"% s in (*) do (

Echo s

)

Pause

Result

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

It just shows the file.

VisitF.bat-the program that traverses the specified path and specified file

The code is as follows:

:: VisitF.bat-the program that traverses the specified path and specified file

: the first parameter is the file to be traversed (wildcards are supported), and the second parameter is the path to be traversed (default is C disk root)

@ 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 100''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-the program that traverses the specified path and specified directory

The code is as follows:

:: VisitD.bat-A program that traverses a specified path and a specified directory

: the first parameter is the directory to be traversed (wildcards are supported), and the second parameter is the path to be traversed (default is C disk root)

@ 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 100 '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 traverses files in a specified file list

The code is as follows:

:: VisitL.bat-A program that traverses files in a specified file list

:: parameter is the list of files to be traversed

@ 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 100 '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-A program that traverses files with specified path and specified DIR information

The code is as follows:

:: VisitI.bat-A program that traverses files with specified path and specified DIR information

: the first parameter is the specified DIR information item, and the second parameter is the path to be traversed (default is the current path)

Note: DIR information items can be directory information items provided by file name, extension, date, time and other DIR commands

:: multiple items can be used at the same time, but a pair of quotation marks must be added, and the parameter format must strictly conform to the information format of DIR

@ 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 100 '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 matched 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

The code is as follows:

:: Visit.bat-Files traverse the batch program

:: Will Sort-10 Compact 17 Compact 2004-V2

::

:: program purpose:

:: performs the specified operation on the specified fileset / directory set / file list

::

:: command line description:

: 1. VISIT file set / directory set [parameters]

:: perform the specified operation on the fileset / directory set

:: 2. VISIT @ file list

:: performs the specified operation on the files in the specified file list

::

:: notes:

::-the fileset / directory set can contain valid paths and wildcards

::-the path defaults to the current path and the fileset defaults to *. * (not all files)

::-when the fileset / directory set contains spaces, it should be enclosed in double quotes

::-[parameters] supported DIR switches: / S / A / O / L, etc. Those who do not conflict with / B

::-[Parameter] unsupported DIR switch: / W / P / V, etc. / B conflicter

::-[Operation] pre-written by the caller to the visitcmd.bat

Traverse the file using% VisitFile% reference in::-[Operation]

:-the program checks to see if [file list] exists, but does not check whether it is valid

::-do not traverse directories and files under the hidden / system directory (except when specified on the command line)

::

:: usage example:

:: visit c:\ / ad / s traverses all directories on disk C, including subdirectories

:: visit "C:\ My document" / Amurd traverses all files under "C:\ My document"

:: visit C:\ * .zip / a / s traverses all .zip packages on disk C, including subdirectories

:: if you want to traverse multiple file / directory sets, you can use DIR fileset / a / s > > File list multiple times.

:: generate a complete list of files, and then use the file list to traverse, or use VisitCE.Bat

Can be generated with "attrib fileset / s" when traversing hidden / system directories that are not explicitly specified

:: list of files, and then reference the third to last string of% VisitFile% in the code of visitcmd.bat

:: then use the file list to traverse

::

:: test report:

:: pass the limited black box test in Win98 command line mode

Performance is still the biggest bottleneck

::

@ echo off

If "% 1" = = "@" goto CopyList

: MakeList

Dir / b 1 2 3 4 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 enhancement

The code is as follows:

:: VisitCE.bat-File traversal batch Program Command Line Enhancement

:: Will Sort-10 Compact 17 Compact 2004-V2CE

::

:: program purpose:

:: performs the specified operation 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.] [fileset 1 fileset 2.]

Perform the specified operation on the fileset defined by the path and switch

:: 2. VISIT @ File list 1 [File list 2.]

:: perform the specified operation on the files in the specified [file list]

::

:: notes:

::-[path] [parameter] [fileset] can be unselected or multi-selected

::-[path] cannot contain wildcards. [fileset] can contain valid paths and wildcards.

::-[path] defaults to the current path, and [fileset] defaults to *. * (not all files)

::-[path] [fileset] if it contains spaces, it should be enclosed in double quotation marks.

::-[parameters] supported DIR switches: / S / A / O / L, etc. Those who do not conflict with / B

::-[Parameter] unsupported DIR switch: / W / P / V, etc. / B conflicter

::-[Operation] pre-written by the caller to the visitcmd.bat

Traverse the file using% VisitFile% reference in::-[Operation]

:-the program checks to see if [file list] exists, but does not check whether it is valid

::-do not traverse directories and files under the hidden / system directory (except when specified on the command line)

::

:: usage example:

:: visit c:\ / ad / s traverses all directories on disk C, including all subdirectories

:: visit "C:\ My document" / Amurd traverses all files under "C:\ My document"

:: visit c:\ d:\ e:\ / s / a / on traverses all the files in C _ MagneD _ E and sorts them by file name.

:: visit\ / a traverses all files and directories under the current disk root

Can be generated with "attrib fileset / s" when traversing hidden / system directories that are not explicitly specified

:: list of files, and then reference the third to last string of% VisitFile% in the code of visitcmd.bat

:: then use the file list to traverse

::

:: test report:

:: pass the limited black box test in Win98 command line mode

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

: DirMake

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.*) do del f

For e in (Command Path Switch Filter File) do set Visit%%e=

: End

There is a similar problem under 2000sp4, and the second problem is actually the incompatibility of fc. When displaying by line number, there are eight bytes in front of each line of text, instead of nine under dos6 and win9x, so it is easy to solve, just change the e 100 'set file=' in the debug script to e 100' set fil='.

However, this is only on a case-by-case basis. The three batches mentioned above are just my experimental prototype for military training under dos, which is very rough. Since I officially turned to win98 in March this year, I have encountered a lot of new problems in use, such as the problem that file names contain spaces, the problem of system / hidden attribute directories, and the problem of Chinese file names when used under nt. And due to the use of too much text flow control in the loop body, the efficiency is seriously low; in addition, there is no big difference in the function and implementation of the three programs, so there is no need to split.

To summarize the above, it is necessary to rewrite the above code. Merging code, fixing loopholes, improving performance, and writing documentation led to the so-called V2 version, which didn't take much effort.

However, one accidental afternoon, suddenly on a whim, why not enhance the command line functions of visit, such as supporting multiple paths, multiple filters (filter), and multiple file lists? originally I just wanted to add a command line parameter analysis loop, but when it was really implemented, I once again realized the complexity of the command line, rushing the directional output when nesting if for, and determining the difference between the root directory and the subdirectory. For's low-energy replacement (win9x/dos) of filters, new problems emerge one after another, and it took me more than half of my working day to barely implement a rough version, which is the so-called V2CE version. I don't have much confidence in rigorous testing.

Now, looking back at my work, in fact, that V2CE version does not have much application value. Multiple paths and filters can be achieved by calling visit many times, and in fact, we only use one path and filter in most cases; moreover, multi-parameter control brings about a decline in ease of use, and users always have certain psychological obstacles to programs with a variety of parameters. My personal interest in dos compression software is rar,zip,arj from high to low, and ease of use is the first to rely on. In addition, the most important thing is that multi-parameter control has led to a significant increase in code volume and complexity, this program once let me drill into the ghost labyrinth built by if for, the readability of the program is getting worse and worse, it is becoming more and more difficult to debug, and there are more tripping cords on compatibility.

These are the costs of functional improvement. Although this is only an extreme example, the balance between function and simplicity can be seen in the importance of programming. The friendly interface does not mean the affinity code, the requirement of humanization does not mean the realization of humanization, it is not a very smart idea to transfer the complexity of the user to the designer (such as Windows), and vice versa (such as Linux), regardless of the dual pressure of code enhancement on the designer and the user.

The above is all the contents of the article "how to traverse directories and files under dos". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report