使用Windows批处理递归打印文件夹和文件

时间:2011-04-10 17:15:33

标签: windows batch-file

  

可能重复:
  Windows batch (files)

你好。我正在尝试做一个批处理脚本,从命令行打印文件夹的所有子文件夹,找到文件的每个文件名(我有一个文件夹和一个文件名列表作为参数)。子文件夹名称应按文件大小的降序排序(文件可以在不同的子文件夹中具有各种大小)。 我找不到打印路径的脚本。有人可以帮忙吗?

@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

1 个答案:

答案 0 :(得分:1)

这是一个脚本,有点做你想要的,抱歉长时间的延迟。

@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

虽然正如杰布指出的那样,我建议学习一些基础知识,考虑到这是家庭作业,如果你实际上没有学会如何去做,那对你来说就不会那么有用了。试图对我的代码进行评论,以便您理解,但请尝试阅读有关using batch files的更多信息。