Tag Archives: Dos

Change timezone with TZ environment variable in Windows

If your program uses one of the following MFC or CRT function, then you can change timezone using TZ environment variable:

  • CTime::GetLocalTm
  • CTime::Format
  • COleDateTime::operator=
  • ctime
  • _wctime
  • _tctime
  • _ctime64
  • _wctime64
  • _tctime64
  • mktime
  • _mktime64
  • _utime
  • _wutime
  • _tutime
  • _utime64
  • _wutime64
  • _tutime64

For example, to set the TZ environment variable to correspond to the current time zone in Germany, enter the following on the command line:
set TZ=GST-1GDT
This will add environment variable TZ with value ‘GST-1GDT’.

More about TZ environment variable:
Specifying the Time Zone with TZ
MSDN _tzset from Time Management

Getting file size in KB using batch file

To get file size using batch file, we use variable with modifier. Then set /a, performs match operation (divide by 1024). Unlike other examples, this example works on any Windows language.

@echo off
:: Name: file_size.bat
:: Author wishmesh.com
:: Calculate file size in KB from %1
set /a size = %~z1 / 1024
echo %size% KB

Example:
file_size.bat "c:\files\some_file.dat"

Quotes necessary when there are spaces in file or folder name.

Tested on Windows XP and Windows Server 2003.

Batch script to test if folder / directory is empty

Yesterday I needed a Dos batch script to check if folder is empty. A little Google search revealed some methods:

  • Using dir and find commands, does not work on non-english Windows versions: www.computing.net
  • Using dir command with /a-d, does not work for folders: www.computing.net
  • Using dir and findstr commands, does not work on non-english Windows versions: windowsitpro.com

If you want something done right do it yourself. Here is a my script:
@echo off
for /F %%i in ('dir /b "c:\test directory\*.*"') do (
   echo Folder is NON empty
   goto :EOF
)
echo Folder is empty or does not exist

Update Sep 21, 2011 – added “or does not exist”. Thanks to the Thump.
Update Jul 5, 2013 – for more code examples see the comments!