Page 1 of 1

Batch: size of folders from a list

Posted: 12 Sep 2013 11:50
by tiagocampos
Hello,

I have this script:

Code: Select all

@echo off
setlocal
set /p dirName=Enter Directory Name:

for /f "tokens=3-4" %%v in ('dir "%dirName%" /s ^| find /i "file(s)"') do (
  set bytes=%%v


echo Folder: %dirName% contains %bytes% bytes

pause



The problem is I want to import from a .txt file with various directory folders.

How can I do that?


Thank you!

Re: Batch: size of folders from a list

Posted: 12 Sep 2013 18:06
by foxidrive
This should process a list:

Code: Select all

@echo off
for /f "delims=" %%a in (list.txt) do call :next "%%a"
pause
goto :eof
:next
set "dirname=%~1"

:: set /p dirName=Enter Directory Name:

for /f "tokens=3-4" %%v in ('dir "%dirName%" /s ^| find /i "file(s)"') do (
  set bytes=%%v
)

echo Folder: %dirName% contains %bytes% bytes

Re: Batch: size of folders from a list

Posted: 13 Sep 2013 03:11
by tiagocampos
Perfect, many thanks :)

Re: Batch: size of folders from a list

Posted: 13 Sep 2013 08:04
by tiagocampos
I got an issue when my folders path has spaces, like for example: "C:\Program Files (x86)\MTSOMS\adapter-logs\AS61001"

The script doesn't recognize the path :cry:

Re: Batch: size of folders from a list

Posted: 13 Sep 2013 19:00
by foxidrive
It's designed to allow spaces. Can you triple check that the location exists, and that you have permission to access it?

Re: Batch: size of folders from a list

Posted: 16 Sep 2013 04:09
by tiagocampos
EDIT: Ignored it, it was an error with the log file (feature I added). The location of the log folder was incorrect. Sorry to waste your time.

Re: Batch: size of folders from a list

Posted: 03 Oct 2013 09:02
by tiagocampos
for /f "tokens=3-4" %%v in ('dir "%dirName%" /s ^| find /i "file(s)"') do (
set bytes=%%v
)

:: To specify log folder and file

set LOG_FOLDER=C:\Users\administrator-ch\Documents\sizebackups\LOGS
set datetime="%date:~6,4%%date:~3,2%%date:~0,2%
set LOG_FILE=%LOG_FOLDER%\LOG_%datetime%.CSV

set exceldate=%date:~6,4%-%date:~3,2%-%date:~0,2%

echo %exceldate%;%dirName%;%bytes%>>%LOG_FILE%


@foxidrive, I noticed that empty folders are being skipped. Do you know how to make them count?

I am writing the output to a log file as you can see, and I needed to get and entry for empty folders too.

Re: Batch: size of folders from a list

Posted: 03 Oct 2013 09:53
by foxidrive
Explain how you see this error? I tried it in a tree with empty folders - and the code only reports the total anyway.

What do you expect it to report? Show me the log output, that will help too.

Re: Batch: size of folders from a list

Posted: 03 Oct 2013 10:10
by tiagocampos
Ok I runned the script for this 4 folders:

Code: Select all

A:\Ambidata
A:\BackupSQL
A:\DADOS
A:\Vazio


And this is what I get in the cmd:
http://prntscr.com/1v0owp

And the log file is like this:

Code: Select all

2013-10-03;A:\DADOS;135.157.117.617
2013-10-03;A:\Ambidata;6.027.825.713
2013-10-03;A:\BackupSQL;637.279.136


No entry for "A:\Vazio" folder, which is the empty one.

Re: Batch: size of folders from a list

Posted: 03 Oct 2013 10:35
by foxidrive
Try putting the redirection at the start of the line:

Code: Select all

>>%LOG_FILE% echo %exceldate%;%dirName%;%bytes%



It will have been reporting some incorrect values up till now too, because of that.

Re: Batch: size of folders from a list

Posted: 03 Oct 2013 11:07
by tiagocampos
Did that, but prompt closed with no results or log writen.

Maybe the full code can help:

Code: Select all

@echo off

:: To specify the location of the file from where we import the list of directories to take action
set file=C:\Users\administrator-ch\Documents\sizebackups\folderslist.txt

echo Updating log...
for /f "delims=" %%a in (%file%) do call :next "%%a"
echo Finished!
::timeout /T 15
pause
goto :eof



:next
set "dirname=%~1"

:: set /p dirName=Enter Directory Name:

for /f "tokens=3-4" %%v in ('dir "%dirName%" /s ^| find /i "file(s)"') do (
  set bytes=%%v
)

:: To specify log folder and file

set LOG_FOLDER=C:\Users\administrator-ch\Documents\sizebackups\LOGS
set datetime="%date:~6,4%%date:~3,2%%date:~0,2%
set LOG_FILE=%LOG_FOLDER%\LOG_%datetime%.CSV

set exceldate=%date:~6,4%-%date:~3,2%-%date:~0,2%

>>%LOG_FILE% echo %exceldate%;%dirName%;%bytes%

Re: Batch: size of folders from a list

Posted: 04 Oct 2013 08:29
by foxidrive
remove the double quote from this line:

set datetime="%date:~6,4%%date:~3,2%%date:~0,2%

Re: Batch: size of folders from a list

Posted: 04 Oct 2013 08:48
by tiagocampos
Oh God...so stupid.

Thank you very much