Archive for the ‘Batch’ Category

Batch script to detect CD/DVD-rom drive

Friday, May 2nd, 2014

Batch script to detect CD/DVD-rom drive

REM Check if we have a CDROM drive
Setlocal EnableDelayedExpansion

echo list vol > diskpart.txt
diskpart /s diskpart.txt > dp_volumes.txt
set hascdrom=0
FOR /F "delims=," %%v in (dp_volumes.txt) do (
rem echo %%v
set str1=%%v
rem echo Line is !str1!
rem echo "x!str1:ROM=!"
rem echo "x!str1!"
if not "x!str1:ROM=!"=="x!str1!" set hascdrom=1
rem echo !hascdrom!
if "!hascdrom!"=="0" GOTO nocdrom

echo We have a CD/DVD-rom
goto :EOF

echo No CD/DVD-rom drive available
goto: EOF


Friday, March 21st, 2008

Batch-scripting is not as powerful as WSH-scripting or PowerShell.
But with some tools you can perform some simple but useful actions.
One tool I often use is robocopy.

  1. Asking for values in a batch-script
    @echo off
    @set /P pctocheck=Enter name of pc:
    @cscript.exe "script.vbs" %pctocheck%
  2. Copy non-existing folders back to C-drive
    @echo off
    rem Check if the script was called with parameter RUN
    IF "%1"=="RUN" GOTO run
    rem Since we are not running the script with parameter RUN, start it using cmd whilst enabling delayed environment variable expansion (/V:ON)
    cmd /V:ON /C H:\copy_c-drive.cmd RUN
    echo ...Copying extra folders on C-drive
    rem List all existing files and directories on the C-drive, including hidden ones.
    dir /a:d /b C:\ >C:\WINDOWS\TEMP\rc_existsystemdirs.txt
    dir /a:hd /b C:\ >>C:\WINDOWS\TEMP\rc_existsystemdirs.txt
    dir /a:-d /b C:\ >C:\WINDOWS\TEMP\rc_existsystemfiles.txt
    dir /a:h-d /b C:\ >>C:\WINDOWS\TEMP\rc_existsystemfiles.txt
    rem Build up variables
    set existingdirs=
    FOR /F "delims=," %%i IN (C:\WINDOWS\TEMP\rc_existsystemdirs.txt) do set existingdirs=!existingdirs! /XD "%%i"
    set existingfiles=
    FOR /F "delims=," %%i IN (C:\WINDOWS\TEMP\rc_existsystemfiles.txt) do set existingfiles=!existingfiles! /XF "%%i"
    rem Do the copy thing
    robocopy /COPYALL /E X:\ C:\ /TEE /LOG+:C:\WINDOWS\TEMP\rc_system.log /XO /R:1 /W:3 %existingdirs% %existingfiles%