2
votes

I am trying to write a script where I have to list all the folders inside a folder with the following logic:

Say folder A, B, C are inside folder F and A, B and C contain subfolders and files.

I have to write a script that would show the Folder A, B , C as header and then lists the files above a specified size inside them(including subfolders)... if possible with their modified date.

I have prepared a skeleton.

@echo off & setLocal EnableDelayedExpansion pushd C:\F

for /f "tokens=* delims= " %%a in ('dir/b/a:d') do (

echo %%a >>C:\F\list.txt
echo "-----------------------------------------------">>C:\F\list.txt
pushd %%a 
for /f "tokens=* delims= " %%i in ('dir/b/s') do (
    echo %%i >>C:\F\list.txt
    if %%~Za gtr 10000 echo %%i is %%~Za >>C:\F\list.txt
    ))

The the desired output is:

Directory A

file1 size1 date1

file2 size2 date2

Directory B

file3 size3 date3

file4 size4 date4

Directory C

file5 size5 date5

file6 size6 date6

---date field is not mandatory but better if included.

Thanks & Regards

2

2 Answers

1
votes

Here's the general idea for the code. It basically searches through each directory and sub directory, and finds all the files of a specified type. Then the program finds each unique file directory and search for files greater than the specified size to output in the format you requested. Note: There is a lot of "extra" included in the code for troubleshooting purposes. Feel free to remove the unnecessary text files as you please. =]

@echo off & setLocal EnableDelayedExpansion pushd C:\F

::sets size limit
SET sizelimit=10000

::searches for all files in directories and subdirectories and outputs to files.txt
dir /b/s >> files.txt

::finds all .zip files in files.txt
type files.txt | findstr /E .zip > myfile1.txt

::finds all .zip file locations and unique file locations
FOR /F "tokens=* delims=\" %%a in (myfile1.txt) do @echo %%~dpa >>filelocations.txt
FOR /F "delims==" %%L in (filelocations.txt) do find "%%L" unique.txt>nul || echo %%L>>unique.txt

::Loops through each unique location, finds all the .zip files and checks if they are larger
:: than the specified file size, then outputs the results to output.txt
FOR /F "tokens=* delims= " %%a in (unique.txt) do (
echo %%a >>output.txt
findstr "%%a" myfile1.txt >temp.txt
FOR /F "tokens=* delims=" %%a in (temp.txt) do (
if %%~za gtr %sizelimit% echo %%~nxa %%~za %%~ta >>output.txt
)
)

end local

::Cleans up extra files (which are generated for troubleshooting purposes)
del files.txt myfile1.txt filelocations.txt unique.txt temp.txt
0
votes

In answer to your question HERE.

Well, you could pipe the output to sort, but there are at least three problems with that.

The first problem is that when you just tack it to the end, it only sort's one line at a time. Meaning it doesn't sort at all. To fix that, you can put the entire for command inside a block () then pipe the output of the block to sort, which causes other problems, though I'm confident that it's possible to solve these issues.

The second problem with that is that it sort's the date alphabetically, starting with the month. This means that all Januaries 1/dd/yyyy will come first, then all Octobers 10/dd/yyyy meaning that you get all the years mixed together, AND it's sorted January, October, November, December, Febuary, March, etc.

You could sort by size by using sort's /+col command, but you'll be comparing size[tab]filename meaning 2[tab]hello comes AFTER 1000[tab]hello. (Type sort /?ENTER to learn more about sort.)

What I reccomend is that use use dir's ability to sort like so:

:: Sort by name
FOR /F "tokens=* delims= " %%a in ('dir /b /o:n ') do @if %%~za gtr %sizelimit% echo %%~ta%tab%%%~za%tab%%%~nxa

:: Sort by size
FOR /F "tokens=* delims= " %%a in ('dir /b /o:s ') do @if %%~za gtr %sizelimit% echo %%~ta%tab%%%~za%tab%%%~nxa

:: Sort by date
FOR /F "tokens=* delims= " %%a in ('dir /b /o:d ') do @if %%~za gtr %sizelimit% echo %%~ta%tab%%%~za%tab%%%~nxa

There are other options for the /o: order command. Type dir /?ENTER at the command prompt for more information on how dir works.