Solved Batch file to send an email containing a file's size

May 21, 2012 at 14:56:12
Specs: Windows 7
Hi,

I have a specific file on my Windows 7 PC whose file size I would like to monitor. I'm trying to create a batch file that will email me it's file size. My intention is to configure Task Scheduler to run this batch file every hour so I can get an hourly email of the file's current size.

I've used sendEmail.exe in batch files previously to send emails so I hope I can use that here as well. Can anyone help me with the command lines that will be needed to determine the current file size and issue an email?

Thanks!


See More: Batch file to send an email containing a files size

Report •

#1
May 21, 2012 at 22:52:47
:: ==========================================
:: email file size
:: ruger.bat Tue 22-05-2012 12:43:42.04
@echo off & setLocal enableDELAYedeXpansioN

:: I don't know sendEmail.exe but this will get file size
:: into a variable.

for /f "tokens=* delims= " %%a in ('dir/b myfile') do (
set size=%%~Za
)
echo !size!

goto :eof

::===========================================


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
May 22, 2012 at 04:25:25
Thanks. Let's say for example the file name is file.rar and it's located in the directory c:\miscfiles\. How would it be listed in the command line you have shown above?

Also, when calling the variable that contains the file size, should it be called as size or !size! ?


Report •

#3
May 22, 2012 at 12:32:17
✔ Best Answer
:: ==========================================
:: email file size
:: ruger.bat Tue 22-05-2012 12:43:42.04
@echo off & setLocal enableDELAYedeXpansioN
:: I don't know sendEmail.exe but this will get file size
:: into a variable.
for /f "tokens=* delims= " %%a in ('dir/b c:\miscfiles\file.rar') do (
set size=%%~Za
)
echo !size!
goto :eof
::===========================================

It will work as is, but I'm not sure why M2 is using delayed expansion when it's not needed. The size is listed in bytes.

Tony


Report •

Related Solutions

#4
May 22, 2012 at 23:10:29
Hi Tony,

Only because it's in my template and I'm too lazy to fuss with it.


=====================================
Life is too important to be taken seriously.

M2


Report •

#5
May 23, 2012 at 04:04:40
Thanks M2 and Tony. I was able to get the batch file working in Task Scheduler.

The value for the file size is reported in bytes. Any way to have that in megabytes instead?

I noticed whenever Task Scheduler runs the batch file, the command prompt window appears in the foreground for about 5 seconds and then goes away. This repeats every time the batch file is triggered. Is it possible to have this window come up in the background instead, or better yet, not come up at all?


Report •

#6
May 24, 2012 at 08:13:43
Here to convert to mb set /A size /=1048576.
Be aware this will only convert it to the closest integer.
To be hidden download some batch compiler and compile as "ghost".
For something worthwhile you either pay or piracy is involved.

Report •

Ask Question