China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-22 16:58
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Original][BAT] Text Traversal and File Traversal Code in DOS (6 Pieces) DigestI View 15,174 Replies 23
Original Poster Posted 2004-02-11 00:00 ·  中国 山东 烟台 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
:: VisitF.bat - Program to traverse specified files in a specified path
:: The first parameter is the file to traverse (supports wildcards), the second parameter is the path to traverse (default is C: root)
@echo off

:main
if == if not exist filelist.txt goto end

:init
if exist filelist.txt if exist xset.asd goto loop
set file=%1
set base=%2
if == set base=c:
dir %base%\%file% /s /a /b > filelist.txt
echo e 100 ''set file='' > xset.asd
echo w >> xset.asd
echo q >> xset.asd

:loop
fc filelist.txt 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 filelist.tx2
copy filelist.tx2 filelist.txt > nul
goto loop

:restore
if exist filelist.txt del filelist.txt
if exist xset.asd del xset.asd
if exist filelist.tx2 del filelist.tx2
if exist setfile.bat del setfile.bat

:end


[ Last edited by willsort on 2006-7-29 at 20:51 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 2 Posted 2004-02-11 00:00 ·  中国 山西 太原 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline

:: VisitD.bat - Program to traverse specified directories in a given path
:: The first parameter is the directory to traverse (supports wildcards), the second parameter is the path to traverse (default is C drive root)
@echo off

:main
if == if not exist dirlist.txt goto end

:init
if exist dirlist.txt if exist xset.asd goto loop
set dir=%1
set base=%2
if == set base=c:
dir %base%\%dir% /s /ad /b > dirlist.txt
echo e 100 'set dir=' > xset.asd
echo w >> xset.asd
echo q >> xset.asd

:loop
fc dirlist.txt 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 dirlist.tx2
copy dirlist.tx2 dirlist.txt > nul
goto loop

:restore
if exist dirlist.txt del dirlist.txt
if exist xset.asd del xset.asd
if exist dirlist.tx2 del dirlist.tx2
if exist setdir.bat del setdir.bat

:end


[ Last edited by willsort on 2006-7-29 at 20:46 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 3 Posted 2004-02-11 00:00 ·  中国 山西 太原 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
:: VisitL.bat - Program to iterate through files in a specified file list
:: Parameter is the file list to iterate through
@echo off

:main
if == if not exist filelist.txt goto end

:init
set filelist=%1
if == set filelist=filelist.txt
if not exist %filelist% goto end
copy %filelist% filelist.tx1 > nul
if exist xset.asd goto loop
echo e 100 'set file=' > xset.asd
echo w >> xset.asd
echo q >> xset.asd

:loop
fc filelist.tx1 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 filelist.tx2
copy filelist.tx2 filelist.tx1 > nul
goto loop

:restore
if exist xset.asd del xset.asd
if exist filelist.tx1 del filelist.tx1
if exist filelist.tx2 del filelist.tx2
if exist setfile.bat del setfile.bat

:end


[ Last edited by willsort on 2006-7-29 at 20:47 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 4 Posted 2004-02-12 00:00 ·  中国 辽宁 沈阳 联通
铂金会员
★★★★
痴迷DOS者
Credits 5,798
Posts 1,924
Joined 2003-06-20 00:00
23-year member
UID 5583
Gender Male
From 金獅電腦軟體工作室
Status Offline
The original poster is truly a batch processing expert. Admiration! Admiration!
熟能生巧,巧能生精,一艺不精,终生无成,精亦求精,始有所成,臻于完美,永无止境!
金狮電腦軟體工作室愿竭诚为您服务!
QQ群:8393170(定期清理不发言者)
个人网站:http://www.520269.cn
电子邮件:doujiehui@vip.qq.com
微信公众号: doujiehui
Floor 5 Posted 2004-02-26 00:00 ·  中国 山东 烟台 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
:: VisitI.bat - A program that traverses files with specified DIR information under a specified path
:: The first parameter is the specified DIR information item; the second parameter is the path to traverse (defaults to the current path)
:: Note: DIR information items can be filenames, extensions, dates, times, and other directory information items provided by the DIR command
:: Multiple items can be used at the same time, but they must be enclosed in a pair of quotation marks, and the parameter format must strictly conform to DIR's information format
@echo off

:main
if == goto end

:init
if exist filelist.txt if exist xset.asd goto loop
set info=%1
set base=%2
if == set base=.
dir %base%\. /s /a /b > filelist.txt
echo e 100 ''''set file='''' > xset.asd
echo w >> xset.asd
echo q >> xset.asd

:loop
fc filelist.txt 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 filelist.tx2
:: The "%file%" parameter determines that all files and directories under the matched subdirectory will not be matched again
copy filelist.tx2 filelist.txt > nul
goto loop

:restore
if exist filelist.txt del filelist.txt
if exist xset.asd del xset.asd
if exist filelist.tx2 del filelist.tx2
if exist setfile.bat del setfile.bat
set info=
set file=
set base=

:end





[ Last edited by willsort on 2006-7-29 at 20:47 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 6 Posted 2004-02-26 00:00 ·  中国 广东 深圳 南山区 电信
初级用户
★★
鹰击长空
Credits 271
Posts 61
Joined 2003-08-20 00:00
22-year member
UID 8964
Gender Male
Status Offline
Question:
if exist filelist.txt if exist xset.asd goto loop
set info=%1
set base=%2
What does it mean? Where can I find materials about batch file operations?
适合的,就是最好的!
DOS,永远的初恋情人。
Floor 7 Posted 2004-02-27 00:00 ·  中国 山东 烟台 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re 笑傲江湖:

if exist filelist.txt if exist xset.asd goto loop
rem If both filelist.txt and xset.asd exist, go to loop and start looping

set info=%1
rem Assign the first parameter %1 to the environment variable info

set base=%2
rem Assign the second parameter %2 to the environment variable base

  For materials on batch file operations, the basics can be found in DOS tutorials on the Internet; intermediate and advanced techniques have to be obtained by reading more, modifying more, and writing more. From old computer magazines before 1999, you should be able to find quite a few techniques about batch programming. This site and some DOS forums also often have many classic batch file programs for study and reference. Here is a link for you first.

http://model.chinajewelry.net/dos/dosbbs/dispbbs.asp?boardID=12&ID=8905



※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 8 Posted 2004-04-20 00:00 ·  中国 河南 安阳 联通
银牌会员
★★★
Credits 1,186
Posts 334
Joined 2003-05-30 00:00
23-year member
UID 2626
Gender Male
Status Offline
A very useful batch file
I learned quite a few techniques from the OP through this thread
But some I understand
And some command lines I don't understand very well
I still need to read it several more times
If everything were explained the way you explained it to 笑傲江湖
It would simply be a classic tutorial and a blessing for newbies
Even so, still thank you so much
Floor 9 Posted 2004-04-21 00:00 ·  中国 辽宁 朝阳 联通
铂金会员
★★★★
痴迷DOS者
Credits 5,798
Posts 1,924
Joined 2003-06-20 00:00
23-year member
UID 5583
Gender Male
From 金獅電腦軟體工作室
Status Offline
The address on the 7th floor should now be:
http://dos.e-stone.cn/dosbbs/dispbbs.asp?boardID=12&ID=8905
Everyone is welcome to support the OP.
熟能生巧,巧能生精,一艺不精,终生无成,精亦求精,始有所成,臻于完美,永无止境!
金狮電腦軟體工作室愿竭诚为您服务!
QQ群:8393170(定期清理不发言者)
个人网站:http://www.520269.cn
电子邮件:doujiehui@vip.qq.com
微信公众号: doujiehui
Floor 10 Posted 2004-04-21 00:00 ·  中国 河南 安阳 联通
银牌会员
★★★
Credits 1,186
Posts 334
Joined 2003-05-30 00:00
23-year member
UID 2626
Gender Male
Status Offline
The following is quoted from Kinglion's post on 2004-4-21 16:16:08:
The address on the 7th floor should now be:
http://dos.e-stone.cn/dosbbs/dispbbs.asp?boardID=12&ID=8905
Everyone is welcome to support the OP.

Great
Thanks
Floor 11 Posted 2004-10-15 00:00 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
Re WillSort: I tested the first program in the DOS mode of Windows 20003 and it did not pass, but I basically understood the principle. It extracts the first file through fc, then uses debug to modify the characters before the filename on the first line of setfile.bat to set file=, and then executes that batch file to obtain the needed environment variable. But under 2003 there are these two problems: 1. fc cannot compare a file with the nul device, so another empty file can be used instead. 2. debug cannot correctly modify setfile.bat. I can't find a solution, because I am too unfamiliar with debug.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 12 Posted 2004-10-18 00:00 ·  中国 山西 运城 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
:: Visit.bat - File traversal batch file program
:: Will Sort - 10/17/2004 - V2
::
:: Program purpose:
:: Execute specified operations on a specified file set/directory set/file list
::
:: Command line description:
:: 1. VISIT file set/directory set
:: Execute specified operations on the file set/directory set
:: 2. VISIT @ file list
:: Execute specified operations on the files in the specified file list
::
:: Notes:
:: - The file set/directory set may contain valid paths and wildcards
:: - The path defaults to the current path, and the file set defaults to *.* (not all files)
:: - When the file set/directory set contains spaces, it must be enclosed in double quotation marks
:: - DIR switches supported by : /S /A /O /L and others that do not conflict with /B
:: - DIR switches not supported by : /W /P /V and others that conflict with /B
:: - is written in advance by the caller into visitcmd.bat
:: - Use %VisitFile% in to reference the traversed file
:: - The program checks whether exists, but does not check whether it is valid
:: - It does not traverse directories and files under hidden/system directories (except when specified on the command line)
::
:: Usage examples:
:: visit c:\ /ad /s Traverse all directories on drive C, including subdirectories
:: visit "C:\My document" /a-d Traverse all files under "C:\My document"
:: visit C:\*.zip /a /s Traverse all .zip archives on drive C, including subdirectories
:: If you want to traverse multiple file/directory sets, you can use "DIR file set /a /s>>file list" multiple times
:: to generate a complete file list, and then use the file list for traversal; or use VisitCE.Bat
:: When traversing hidden/system directories that are not explicitly specified, you can use "attrib file set /s" to generate
:: a file list, then reference the third-to-last string of %VisitFile% in the code of visitcmd.bat,
:: and then use the file list for traversal
::
:: Test report:
:: Passed limited black-box testing 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 > ~visit.lst
find "~visit.lst" /v < ~visit.lst > ~visit.tmp
if not errorlevel 1 copy ~visit.tmp ~visit.lst>nul
goto MakePreLine

:CopyList
if not == if exist %2 copy %2 ~visit.lst>nul
if not exist ~visit.lst goto End

:MakePreLine
echo set VisitFile=> ~visit.pre
for %%c in (rcx e w q) do echo %%c>> ~visit.asd
debug ~visit.pre < ~visit.asd > nul
if == chcp 936 > nul

:LoopVisit
copy ~visit.pre+~visit.lst ~visit.tmp > nul
find "set VisitFile=" < ~visit.tmp > ~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 < ~visit.tmp > ~visit.lst
goto LoopVisit

:Clear
del ~visit.*
set VisitFile=

:End


[ Last edited by willsort on 2005-8-14 at 22:44 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 13 Posted 2004-10-18 00:00 ·  中国 山西 运城 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
:: VisitCE.bat - Command-line enhanced edition of the file traversal batch file program
:: Will Sort - 10/17/2004 - V2CE
::
:: Program purpose:
:: Execute specified operations on specified files/directory sets under specified paths/file lists
::
:: Command line description:
:: 1. VISIT
:: Execute specified operations on the limited by and
:: 2. VISIT @ file list1
:: Execute specified operations on the files in the specified
::
:: Notes:
:: - may all be omitted or specified multiple times
:: - may not contain wildcards; may contain valid paths and wildcards
:: - defaults to the current path, and defaults to *.* (not all files)
:: - When contains spaces, it must be enclosed in double quotation marks
:: - DIR switches supported by : /S /A /O /L and others that do not conflict with /B
:: - DIR switches not supported by : /W /P /V and others that conflict with /B
:: - is written in advance by the caller into visitcmd.bat
:: - Use %VisitFile% in to reference the traversed file
:: - The program checks whether exists, but does not check whether it is valid
:: - It does not traverse directories and files under hidden/system directories (except when specified on the command line)
::
:: Usage examples:
:: visit c:\ /ad /s Traverse all directories on drive C, 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 filename
:: visit \ /a Traverse all files and directories under the root directory of the current drive
:: When traversing hidden/system directories that are not explicitly specified, you can use "attrib file set /s" to generate
:: a file list, then reference the third-to-last string of %VisitFile% in the code of visitcmd.bat,
:: and then use the file list for traversal
::
:: Test report:
:: Passed limited black-box testing 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 == goto GetArgu

%VisitCommand% $ %VisitFilter%

:MakeList
if not == goto ForMake
irMake
dir %2 /b %VisitSwitch% >> ~visit.lst
goto MakeNext
:ForMake
for %%p in (%VisitPath%) do dir %%p.\%2 /b %VisitSwitch% >> ~visit.lst
:MakeNext
shift
if not == goto MakeList
find "~visit.lst" /v < ~visit.lst > ~visit.tmp
if not errorlevel 1 copy ~visit.tmp ~visit.lst>nul
goto MakePreLine

:CopyList
if not == if exist %2 type %2>>~visit.lst
shift
if not == goto CopyList

:MakePreLine
if not exist ~visit.lst goto End
echo set VisitFile=> ~visit.pre
for %%c in (rcx e w q) do echo %%c>> ~visit.asd
debug ~visit.pre < ~visit.asd > nul
if == chcp 936 > nul

:LoopVisit
copy ~visit.pre+~visit.lst ~visit.tmp > nul
find "set VisitFile=" < ~visit.tmp > ~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 < ~visit.tmp > ~visit.lst
goto LoopVisit

:Clear
for %%f in (~visit.*) do del %%f
for %%e in (Command Path Switch Filter File) do set Visit%%e=

:End


[ Last edited by willsort on 2005-8-14 at 22:48 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 14 Posted 2004-10-18 00:00 ·  中国 山西 运城 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re Climbing:

  A similar problem occurs under 2000sp4. The second problem is actually also an fc incompatibility problem. When it displays by line
number, there are 8 byte positions before each line of text, rather than the 9 under dos6 and win9x, so solving it is also
very simple: change e 100 'set file=' in the debug script to e 100 'set fil='.


  However, this is just dealing with the matter as it stands. The three batch files mentioned above were only experimental prototypes I used for practice
under dos6.22, and they were made very roughly. Since I officially switched to win98 in March this year, I encountered many
new problems in use, such as the problem of filenames containing spaces, the problem of system/hidden attribute directories; when used under nt,
I also encountered the problem of Chinese filenames; moreover, because too much text stream control was used in the loop body, it led to
seriously low efficiency; in addition, the three programs do not differ much in function or implementation, so there is no need to split them up.

  For all the reasons above, rewriting the above code became necessary. Merging the code, making up for loopholes, improving performance, and writing
documentation produced the so-called V2 edition; actually, not much effort was spent in the process.

  However, one accidental afternoon, on a sudden whim, I thought, why not enhance visit's command-line functions a bit,
such as supporting multiple paths, multiple filters, and multiple file lists. At first I thought it would only require adding a command-line parameter
analysis loop, but when I actually implemented it, I once again experienced the complexity of the command line: redirection output conflicts when if for are nested,
the difference between root directories and subdirectories when determining directories, for's low-grade substitution of filters (win9x/dos), and
one new problem after another came crowding in endlessly. It actually took me a good half a workday before I barely implemented
a rough version. This is the so-called V2CE edition. I do not have much confidence in strict testing.

  Now, looking back at my work, that V2CE edition actually does not have much application value. Multiple
paths and filters can be completely implemented by calling visit multiple times, and in fact, in most cases we only
use one path and one filter; moreover, multi-parameter control brings a decline in ease of use. Users always have a certain psychological barrier toward
programs with many parameters. My personal preference for DOS compression software, from high to low, is rar,zip,arj,
and ease of use is the first consideration; in addition, most importantly, multi-parameter control caused a large increase in code size
and complexity. This program once made me burrow into a ghostly maze built from if for and lose all sense of direction. The readability of the program became
worse and worse, and debugging became more and more difficult, while compatibility pulled in even more tripwires.

  These are the price paid for feature enhancement. Although this is only a relatively extreme example, the importance of the balance between function and
simplicity in program design is obvious from this. An interface with affinity does not mean code with affinity,
and user-friendly requirements do not mean user-friendly implementation. Blindly shifting the complexity on the user's side to the designer (for example
Windows) is not a very smart idea, and the reverse is also true (for example Linux). This is not even considering the dual pressure on both the designer and the user caused by
code feature enhancement.

  “complex makes simple”, indeed!


[ Last edited by willsort on 2005-8-14 at 22:53 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 15 Posted 2005-12-04 22:19 ·  中国 湖南 郴州 中移铁通
新手上路
Credits 2
Posts 1
Joined 2005-12-04 21:21
20-year member
UID 46490
From hn
Status Offline
  Really impressive. I'll save it and study it carefully.
  Thank you, moderator willsort.
Forum Jump: