-1

I am trying to create a list of folders and which size they have, basically because I have low space on my drive. I cannot figure out where and why I am running out of space, hence I want to create a list of all folders, with their size and modify date (or creation date works too). To list the folders and their size is something that I managed, however I am not sure how to write the modify date too. I tried some variations with set "mtime=%%~tf", but not successful though. I am not interested in the contents of each folder, just this relevant information for all subfolders from the one I define in the script. Below what I`ve done so far (also tested). Thanks in advance!

Later edit: Not a duplicate, that post explains how to write the size, I already figured that out and it is written in the code. I am not able to figure out how to write the modify/creation date too..

@echo off
pushd "D:\test" 
for /f "delims=" %%a in (' dir /ad /b ') do call :size "%%~fa"
popd
pause
goto :eof

:size
for /f "tokens=3" %%b in ('dir /s "%~1" 2^>nul ^|find " File(s)"') do set "n=%%b"
set dirsize=%n%
REM set dirsize=%dirsize:,=%
set dirsize=%dirsize%
set dirsize=%dirsize:~-18%
>>"D:\test\dirsize.tmp" echo %dirsize% "%~1"

Output:

7,557 "D:\test\e"
2,519 "D:\test\t"
2,519 "D:\test\ta"
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 2
    Possible duplicate of [How to list all folder with size via batch file](http://stackoverflow.com/questions/21711180/how-to-list-all-folder-with-size-via-batch-file) – JosefZ Dec 30 '16 at 09:37
  • Nope, not a duplicate, already looked into that post. The answers are just for size. I already have the size of the folders and their names in the temporary file, I just cannot figure it out how to write the creation date (or modify date) too. – Ana Scheianu Dec 30 '16 at 09:45
  • @OanaScheianu, please never change the topic of a question, particularly in case there have already been answers posted, as these may become invalid then! ask a new question instead! There is the [`~t` modifier](http://ss64.com/nt/syntax-args.html) that returns the last modification date/time... – aschipfl Dec 30 '16 at 10:27
  • @aschipfl I hope you are kidding. I have not changed the topic of my question/ I only changed now the title to be more specific, as I see people don't bother to read it from the beginning to the end. I specifically written that I manage to list the names and sizes of the folders. The date is the one I couldnt successfully write. Tried already with the ~t modifier, didn't work out. Hence I posted here if anyone can specifically help me with this part. – Ana Scheianu Dec 30 '16 at 10:57
  • No, I am not kidding; your original title was very misleading as you asked for the size but not the date. Anyway, your code cannot work as you have it posted in the question, so please revise your post and copy the code you are actually using; basically, there are many line-breaks missing; once you fixed it, I think I could easily help you... – aschipfl Dec 30 '16 at 12:01
  • I [edited](http://stackoverflow.com/posts/41393748/revisions) your question and tried to fix the line-breaks in the code you posted -- please check it, @OanaScheianu, and update it if I did something wrong; thank you in advance! – aschipfl Dec 30 '16 at 13:00
  • 1
    `for /f "delims=" %%a in (' dir /ad /b ') do call :size "%%~fa" "%%~ta"` would supply datetime last written as 2nd parameter. – JosefZ Dec 30 '16 at 13:47

2 Answers2

2

At first, I would change the strategy of retrieving the folder sizes, because your approach is strongly locale-dependent. But there are two things that could be done to avoid that:

  1. to add the switch /-C to the dir command in order to avoid the thousand grouping symbols;
  2. to not search for the text File(s) but to only read the next-to-last line instead;

So this is the changed code (I hope I interpreted your wrongly formatted code right):

@echo off
pushd "D:\test"
> "D:\test\dirsize.tmp" (
    for /F "eol=| delims=" %%A in ('
        dir /B /A:D
    ') do (
        call :SIZE "%%~fA"
    )
)
popd
goto :EOF

:SIZE
set "NUM="
for /F "skip=2 tokens=3" %%B in ('
    2^> nul dir /S /-C "%~1"
') do (
    call set "DIRSIZE=%%NUM%%"
    set "NUM=%%B"
)
echo %DIRSIZE% "%~1"

The skip=2 option just avoids the two-line header to be read in case of an error.


To add the date/time of the last modification of each directory, simply use the ~t modifier for the argument reference %1 in the last line of the sub-routine :SIZE:

echo %~t1 %DIRSIZE% "%~1"

For getting the date/time of creation of every folder, there is no modifier, but there is the /T:C option of the dir command, and the directory ., which is the first returned item, pointing to the given folder itself. So we can use another for /F loop to capture the date/time next to ., which is implemented in the new sub-routine :CDAT in the following code:

@echo off
pushd "D:\test"
> "D:\test\dirsize.tmp" (
    for /F "eol=| delims=" %%A in ('
        dir /B /A:D
    ') do (
        call :CDAT "%%~fA"
        call :SIZE "%%~fA"
    )
)
popd
goto :EOF

:SIZE
set "NUM="
for /F "skip=2 tokens=3" %%B in ('
    2^> nul dir /S /-C "%~1"
') do (
    call set "DIRSIZE=%%NUM%%"
    set "NUM=%%B"
)
echo %DIRSIZE% "%~1"
goto :EOF

:CDAT
for /F "skip=5 tokens=1-2" %%C in ('
    dir /A:D /T:C "%~1"
') do (
    < nul set /P ="%%C %%D "
    goto :NEXT
)
:NEXT
goto :EOF

The < nul set /P command is just a trick to echo a string without a trailing line-break, so the creation date/time is returned in the same line as the rest (size plus folder path).

aschipfl
  • 33,626
  • 12
  • 54
  • 99
0

See my full answer here on cleaning hard drives https://answers.microsoft.com/en-us/windows/forum/windows_vista-files/need-help-in-deleting-un-neccesary-files-from-the/a6daf1f1-7c50-45bd-b7a9-7732b5c19f28

To see where files are taking up space

This will list the number of files and size for folders on your drive. Each command does a subset so you can manage it better.

Start - All Programs - Accessories - Right click Command Prompt and choose Run As Administrator. Type (or copy and paste by right clicking in the Command Prompt window and choosing Paste).

Then configure the command prompt. Right click the Command Prompt's titlebar - Properties - Layout tab - and enter 1000 for Screen Buffer Size Height and 43 (or 50) for Window Size Height. Change the Color (Color tab) and Font (Font tab) if you want.

To see size of folders in Documents, excluding music, video, or pictures folders.

for /f "skip=2 tokens=3" %A in ('Reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Personal"') do set doc=%A

for /f "usebackq tokens=2* delims= " %i IN (`dir "%doc%" /a /s ^|findstr /i /v "\/"^|findstr /l /v "Pictures Music Video"`) DO @echo %j&echo.

To see size of folders in Music.

for /f "skip=2 tokens=4" %A in ('Reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "My Music"') do set Doc=%A

for /f "usebackq tokens=2* delims= " %i IN (`dir "%doc%" /a /s ^|findstr /i /v "\/"`) DO @echo %j&echo.

To see size of folders in Pictures.

for /f "skip=2 tokens=4" %A in ('Reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "My Pictures"') do set Doc=%A

for /f "usebackq tokens=2* delims= " %i IN (`dir "%doc%" /a /s ^|findstr /i /v "\/"`) DO @echo %j&echo.

To see size of folders in Video.

for /f "skip=2 tokens=4" %A in ('Reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "My Video"') do set Doc=%A

for /f "usebackq tokens=2* delims= " %i IN (`dir "%doc%" /a /s ^|findstr /i /v "\/"`) DO @echo %j&echo.

To see size of folders on the Desktop.

for /f "skip=2 tokens=3" %A in ('Reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Desktop"') do set doc=%A

for /f "usebackq tokens=2* delims= " %i IN (`dir "%doc%" /a /s ^|findstr /i /v "\/"`) DO @echo %j&echo.

To see size of folders the Windows folder.

for /f "usebackq tokens=2* delims= " %i IN (`dir "%windir%" /a /s ^|findstr /i /v "\/"`) DO @echo %j&echo.

To see size of folders the Program Files folder.

for /f "usebackq tokens=2* delims= " %i IN (`dir "%ProgramFiles%" /a /s ^|findstr /i /v "\/"`) DO @echo %j&echo.
  • Hello. Again, I am not interested in calculating the size, already done that with the bat file. I am interested in writting the date too in the temp file, which currently I didn't manage to do. – Ana Scheianu Dec 30 '16 at 09:48