递归使用Windows批量打印文件夹和文件 [英] Print folders and files recursively using Windows Batch
问题描述
可能重复:结果
Windows批处理(文件)
块引用>您好。我试图做一个批处理脚本的命令行打印为每个文件名的文件夹,该文件被发现(我有作为参数的文件夹和文件名的列表)的所有子文件夹。子文件夹名称应该按降序文件大小(文件可以在不同的子文件夹不同大小)的顺序排序。
我找不到打印路径的脚本。谁能帮助?关闭@echo
REM检查参数的数量
如果%2==转到ERR1
REM检查:是第一个参数一个目录?
如果NOT EXIST%1 \\ NUL转到ERR2
集合D =%1
转移
REM迭代parametersa其余
为%%我在DIR%%(
找到%DIR /名称%I>温度如果有杜/ B温度|切/ F 1页转到ERR3
MYVAR = TYPE温度
回声文件%i的是: 在%MYVAR %% j执行
回声引用%j 回音后排序
杜/ B%MYVAR |排序/ NR
):ERR1
呼应的两个参数是必要的
转到结束
:ERR2
回声第一个参数必须是一个目录。
:ERR3
回声文件不存在。
:结束
解决方案下面是一个脚本,还挺你想要做什么,对于长期拖延抱歉。
关闭@echo
REM应用程序逻辑。 askForDir。 [askForFile => findFile] {}无限
:askForDir
CLS
设置基本路径=
回声不提供一个目录将退出。尾随斜线请
设置/ p =基本路径目录中搜索:
如果x%基本路径%==点¯x转到endAppREM获取文件名,设置CURDIR
:askForFile
CLS
设置文件名=
回声当前正在搜索的:'%基本路径%
回声不提供文件名,将返回到目录选择
设置/ p =文件名搜索文件:
如果x%FILENAME%==点¯x转到askForDirREM我们知道我们正在寻找和在哪里
设置文件=%基本路径%%文件名字%
如果存在的文件%%(
回声%文件%
)
REM检查递归在每个子文件夹的文件名
FOR / R%基本路径%%%在f IN(%FILENAME%)DO(
回声%%˚F
)REM成品目录和所有潜艇,askForFile再次
回声完成。
暂停> NUL
转到askForFileREM成品
:endApp
CLS
呼应我们希望你喜欢这个presentation。
暂停&GT; NUL虽然作为杰布指出的,我建议你学习一些基础知识,考虑到这是家庭作业,这将不利于你那么多,如果你不实际学习如何做到这一点。试图评论我的code尽可能多的让你明白,但尝试阅读更多关于<一个href=\"http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true\"相对=nofollow>使用批处理文件。
Possible Duplicate:
Windows batch (files)Hello . I am trying to do a batch script that prints for each filename from the command line all subfolders of the folder, where the file is found (I have as parameters a folder and a list of filenames). The subfolder names should be sorted in descending order of the file sizes (the file can have various sizes in different subfolders). I can't find a script that prints the paths. Can anyone help?
@echo off REM check the numbers of parameters if "%2"=="" goto err1 REM check: is first parameter a directory? if NOT EXIST %1\NUL goto err2 set d=%1 shift REM iterate the rest of the parametersa for %%i in %dir% ( find %dir /name %i > temp if EXIST du /b temp | cut /f 1 goto err3 myvar=TYPE temp echo "file " %i "is in: " for %%j in %myvar do echo %j echo after sort du /b %myvar | sort /nr ) :err1 echo Two parameters are necessary goto end :err2 echo First parameter must be a directory. :err3 echo file does not exist. :end
解决方案Here's a script that kinda does what you want, sorry for the long delay.
@echo off REM application logic. askForDir. [askForFile => findFile]{infinity} :askForDir cls set basePath= echo Not supplying a directory will exit. Trailing slash please set /p basePath=Search in directory: if x%basePath%==x goto endApp REM Gets a file name, sets the curDir :askForFile cls set fileName= echo Currently searching in: '%basePath%' echo Not supplying a filename will return you to directory selection set /p fileName=Search for file: if x%fileName%==x goto askForDir REM We know what we're searching for and where set file=%basePath%%fileName% if exist %file% ( echo %file% ) REM check recursively for the fileName in each subfolder FOR /R %basePath% %%f IN (%fileName%) DO ( echo %%f ) REM Finished dir and all subs, askForFile again echo Done. pause>nul goto askForFile REM Finished :endApp cls echo We hope you enjoyed this presentation. pause>nul
Although as jeb pointed out, I'd recommend learning some of the basics, considering this is homework, it won't help you that much if you don't actually learn how to do it. Tried to comment my code as much for you to understand, but try reading more about using batch files.
这篇关于递归使用Windows批量打印文件夹和文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!