4

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
Community
  • 1
  • 1
Radu Stejerean
  • 345
  • 2
  • 13
  • 28
  • It is not entirely clear what you mean. I understand that you are given a folder name followed by a list of filenames, but I'm not sure what you mean by "all subfolders of the folder, where the file is found". Is each given filename expected to be in 1 or more subfolders, or does that simply mean you need to find *the* one subfolder (per file) which contains each file? – kqnr Apr 10 '11 at 17:23
  • If I have folder1 and a.txt , b.txt , c.txt as parameters and in folder1 i have a.txt and folder2( contains b.txt and c.txt ) and folder3 (contains b.txt and a.txt ) , I have to print for the file a.txt : folder1/a.txt .......... folder1/folder2/a.txt b.txt : folder1/folder2/b.txt .... folder1/folder3/b.txt c.txt : folder1 folder2.c.txt .... folder1/folder3/c.txt – Radu Stejerean Apr 10 '11 at 17:28
  • "I have to print" Sounds like homework. Why are you trying to do this with batch files specifically ? – Khez Apr 10 '11 at 18:09
  • It is my homework for operating systems . It is mandatory that I solve it with windows batch. I can't make a script that finds the paths for the files ( where the for loop starts ) . – Radu Stejerean Apr 10 '11 at 18:13
  • I really need someone to explain me how . – Radu Stejerean Apr 10 '11 at 18:18
  • Computer Science classes, always fun times. Have you identified the errors above ? – Khez Apr 10 '11 at 18:33
  • Can I make you a version that works by asking the user for the variables? (I hate command line batch files) – Khez Apr 10 '11 at 19:30
  • Perhaps, it's a good idea to begin with, learning the sytax rules of windows batch. `cmd /?`, `set /?`, `if /?` and `for /?` could help. Then you could try to write a simple batch to first find all the files then sort and print them. – jeb Apr 10 '11 at 21:14

1 Answers1

1

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.

Khez
  • 10,172
  • 2
  • 31
  • 51
  • Thank you. I will do my best. – Radu Stejerean Apr 10 '11 at 21:23
  • +1, for a good sample to find the files, but not a full solution, as this is the homework of Radu :-) – jeb Apr 10 '11 at 21:24
  • @jeb Heh, I was struggling with myself not to give him a full solution. The hardest part (by my standards at least) I omitted. Sorting sub-folders is gonna be killer :D – Khez Apr 10 '11 at 21:27