Many DBA’s and developers are now routinely using VMware Workstation virtual machines to simplify database research and even development. I am one of those people. I also use Oracle’s Virtual Box too. Today I’m sharing my Windows FIX_VMDK.BAT script for fixing, defragging, compacting and zipping up my VMware VM’s. For example when I’ve got a VM exactly the way I like it and want to create a version to save, I run this script. While some steps like defragging and compacting the VM can be done from the VMware GUI, I just want to do it all at once – with minimal interaction from me. VMware’s Workstation provides some very useful command line capabilities – but most users don’t know they have them. So I wrote and am sharing this script. Let’s assume that you have your VM’s in the C:\VM directory and now want to fix, defrag, compact and zip up as a RAR file the LINUX_ORACLE12 VM, then you simply issue the following command:

 

                C:\> fix_vmdk.bat  c:\vm  linux_oracle12  rar

 

@REM ===========================================

@REM SCRIPT:  FIX_VMDK.BAT

@REM AUTHOR:  BERT SCALZO

@REM USAGE:   %0  VM-DIR VM-NAME  {7z rar both}

@REM ===========================================

 

:PARMCOUNT

@set argC=0

@for %%x in (%*) do @Set /A argC+=1

@if %argC% GTR 3 @goto USAGE

@goto PARM1

 

:PARM1

@if "%1"==""        @goto USAGE

@if EXIST %1        @goto PARM2

@goto USAGE

 

:PARM2

@if "%2"==""        @goto USAGE

@if EXIST %1\%2     @goto PARM3

@goto USAGE

 

:PARM3

@if "%3"==""        @goto CONTINUE

@if "%3"=="7z"      @goto CONTINUE

@if "%3"=="rar"     @goto CONTINUE

@if "%3"=="both"    @goto CONTINUE

@goto USAGE

 

:USAGE

@echo.

@echo ====================================================

@echo USAGE:  %0  VM-DIR VM-NAME  {7z rar both}

@echo.

@echo ERROR:  Mistake made in parameter list

@echo ====================================================

@goto RETURN

 

:CONTINUE

@set vm_dir=%1

@set vm_name=%2

@set zipping=%3

@FOR /F %%G IN ('DIR %1\%2\*.vmdk /B') DO @SET vm_file=%%G

@if "vm_file"==""  @goto USAGE

 

@echo.

@echo ##################################################

@echo #### %0:

@echo ####     VM Dir:   %vm_dir%

@echo ####     VM Name:  %vm_name%

@echo ####     VM VMDK:  %vm_file%

@echo ###      Zipping:  %zipping%

@echo ##################################################

 

REM @goto RETURN

REM @goto SKIP

"C:\Program Files (x86)\VMware\VMware Workstation\vmware-vdiskmanager.exe" -e %vm_dir%\%vm_name%\%vm_file%

"C:\Program Files (x86)\VMware\VMware Workstation\vmware-vdiskmanager.exe" -R %vm_dir%\%vm_name%\%vm_file%

"C:\Program Files (x86)\VMware\VMware Workstation\vmware-vdiskmanager.exe" -d %vm_dir%\%vm_name%\%vm_file%

"C:\Program Files (x86)\VMware\VMware Workstation\vmware-vdiskmanager.exe" -k %vm_dir%\%vm_name%\%vm_file%

:SKIP

 

@if EXIST %vm_dir%\%vm_name%\*.log   @del /f /q /s %vm_dir%\%vm_name%\*.log

@if EXIST %vm_dir%\%vm_name%\*.vmsd  @del /f /q /s %vm_dir%\%vm_name%\*.vmsd

@if EXIST %vm_dir%\%vm_name%\caches  @del /f /q /s %vm_dir%\%vm_name%\caches

@if EXIST %vm_dir%\%vm_name%\caches  @rmdir /q /s  %vm_dir%\%vm_name%\caches

 

@if EXIST %vm_dir%\%vm_name%\*.*     @attrib -a    %vm_dir%\%vm_name%\*.*

 

@echo ##################################################

@echo.

 

@SET isodate=%date:~10,4%-%date:~4,2%-%date:~7,2%

 

@if "%zipping%"=="7z" @goto ZIP7Z

@if "%zipping%"=="rar" @goto ZIPRAR

@if "%zipping%"=="both" @goto ZIPRAR

@goto RETURN

 

:ZIPRAR

@if EXIST %vm_dir%\%vm_name%-%isodate%.rar @del /f /q %vm_dir%\%vm_name%-%isodate%.rar

@"C:\Program Files\WinRAR\rar"   a -ac -ai -cl -ed -m5 -mt4 -r -s -t %vm_dir%\%vm_name%-%isodate%.rar %vm_dir%\%vm_name%\

@if "%zipping"=="both" @goto ZIP7Z

@goto RETURN

 

:ZIP7Z

@if EXIST %vm_dir%\%vm_name%-%isodate%.7z  @del /f /q %vm_dir%\%vm_name%-%isodate%.7z

@"C:\Program Files\7-Zip\7z.exe" a -t7z -r -mx=9 -ms=on -mmt=4 -m1=LZMA -slp   %vm_dir%\%vm_name%-%isodate%.7z  %vm_dir%\%vm_name%\

@goto RETURN

 

:RETURN