Code Paparazzo

Code snippets I'd like to remember, by Peter Forret
My 'real' blog is at blog.forret.com

waRmZip.wsf v1.7 (Nov 2005)
Utility to clean up/free up space in a folder (and its subfolders),
* compressing files after a certain number of days (with external program)
* rotating files after a certain number of days/above a certain size
* deleting files and/or empty folders after a certain period of time
* moving files to other folders/disks

:: start doing %NB% things
call :calsectime
set TIME1=%SECTIME%

::(do stuff)

:: finished doing %NB% things
call :calsectime
set TIME2=%SECTIME%

set /A TPS=%NB% / ( %TIME2% - %TIME1% )
echo Thoughput = %TPS% things/sec

:calcsectime
	set NOW=%TIME:~0,8%
	for /F "delims=: tokens=1,2,3" %%f in ( "%NOW%" ) do (
		SET /A SECTIME=%%h + %%g * 60 + %%f * 3600
		)
	goto :EOF

Remarks:

  • I calculate the # seconds since midnight, so if you start something just before midnight, and end just after, it will not work, you will get a negative nonsense throughput (TIME2 < TIME1)
:freespace
	dir %1 2>&1 | findstr free > %TMPFILE%
	for /f "tokens=3,4*" %%f in ( %TMPFILE% ) do (
		set FREEBYTES=%%f
	)
	set FREEBYTES=%FREEBYTES:.=%
	set FREEBYTES=%FREEBYTES:,=%
	set /a FREEMB=%FREEBYTES:~0,-6%
	goto :end

Some remarks:

  • CMD DOS can only handle 32 bit signed integer, so the sizes of disks over 2GB are impossible to store as bytes. I convert the number to MB by ignoring the last 6 chars (equivalent to dividing by 1.000.000, but as a string operation, not a number division)
  • Depending on the language of the Windows you have, numbers will be shown as 1.234.567.890 or 1,234,567,890. I remove these separators.
call :foldersize c:\temp
...
:foldersize
	if not exist %1 (
		echo *** WARNING: folder [%1] does not exist!
		set MBFOLDER=0
		goto :EOF
	)
	dir %1 2>&1 | findstr File > %TMPFILE%
	for /f "tokens=1,2,3,4*" %%f in ( %TMPFILE% ) do (
		set NBFILES=%%f
		set NBBYTES=%%h
	)
	set NBBYTES=%NBBYTES:.=%
	set NBBYTES=%NBBYTES:,=%
	set /a MBFOLDER=%NBBYTES:~0,-6%
	goto :EOF

Remarks:

  • Because DOS integers (signed 32 bit) cannot be used to hold the # of bytes, I use the output of DIR and ignore the last 6 numbers, which gives me an approximation in MB.
  • Because of the rounding down to the nearest MB, this will not work for a folder with a lot of small files
  • I write to a TMPFILE temporary file, which I have defined earlier, %TMP%\tmp_%USERNAME%.txt is a e.g. good option. 
  • I could work with for /f “usebackquote”, but since I often use keyboards without ` (they exist) and it’s hard to do 2 piped commands between backquotes
  • This example was made for the English version of Windows: DIR has a line with the word ‘File’. If you use another language, adapt as necessary
:findlabel
	dir %1  2>&1 | findstr /C:"drive" > %TMPFILE%
	for /f "tokens=6,7*" %%f in ( %TMPFILE% ) do (
		set DRVLABEL=%%f
		)
	set DRVLABEL=%DRVLABEL: =%
	goto :EOF

Remarks:

  • I write to a TMPFILE temporary file, which I have defined earlier, %TMP%\tmp_%USERNAME%.txt is a e.g. good option. 
  • I could work with for /f “usebackquote”, but since I often use keyboards without ` (they exist) and it’s hard to do 2 piped commands between backquotes
  • This example was made for the English version of Windows: DIR has a line with the word ‘drive’. If you use another language, adapt as necessary