Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
tiagocampos
- Posts: 38
- Joined: 21 Feb 2013 12:41
#1
Post
by tiagocampos » 12 Sep 2013 11:50
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!
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#2
Post
by foxidrive » 12 Sep 2013 18:06
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
-
tiagocampos
- Posts: 38
- Joined: 21 Feb 2013 12:41
#4
Post
by tiagocampos » 13 Sep 2013 08:04
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
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#5
Post
by foxidrive » 13 Sep 2013 19:00
It's designed to allow spaces. Can you triple check that the location exists, and that you have permission to access it?
-
tiagocampos
- Posts: 38
- Joined: 21 Feb 2013 12:41
#6
Post
by tiagocampos » 16 Sep 2013 04:09
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.
-
tiagocampos
- Posts: 38
- Joined: 21 Feb 2013 12:41
#7
Post
by tiagocampos » 03 Oct 2013 09:02
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.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#8
Post
by foxidrive » 03 Oct 2013 09:53
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.
-
tiagocampos
- Posts: 38
- Joined: 21 Feb 2013 12:41
#9
Post
by tiagocampos » 03 Oct 2013 10:10
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/1v0owpAnd 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.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#10
Post
by foxidrive » 03 Oct 2013 10:35
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.
-
tiagocampos
- Posts: 38
- Joined: 21 Feb 2013 12:41
#11
Post
by tiagocampos » 03 Oct 2013 11:07
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%
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#12
Post
by foxidrive » 04 Oct 2013 08:29
remove the double quote from this line:
set datetime="%date:~6,4%%date:~3,2%%date:~0,2%