开发者

Print folders and files recursively using Windows Batch [duplicate]

This question already has an answer here: Closed 11 years ago.

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开发者_StackOverflow 
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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜