User Tools

Site Tools


Sidebar



batch

Batch files

log stdout & stderr to file
tmp.bat > test.txt 2>&1

or both to different files:

test.bat > testlog.txt 2> testerrors.txt
counting in a for-loop

http://stackoverflow.com/questions/7522740/counting-in-a-for-loop-using-dos-batch-script

setlocal enableextensions enabledelayedexpansion
SET /A COUNT=1
FOR /F "tokens=*" %%A IN ('dir /b "data\clouds\queue\*.jpg"') DO (
  echo !COUNT!
  copy "data\clouds\queue\%%A" "data\clouds\numbered\!COUNT!.jpg"
  SET /A COUNT+=1
)
endlocal
loop over files
for /f "tokens=*" %%G in ('dir /b "data\clouds\queue\*.jpg"') do (
  echo Found %%G
)
comments

dubbele dubbele punt (::) is een alternatief voor REM

delete svn folders recursively on windows

use this in a batch file:

FOR /F "tokens=*" %%G IN ('DIR /B /AD /S *.svn*') DO RMDIR /S /Q "%%G"

thanks to peter

handige batchfiles
passing arguments using set

29 maart 2009

@echo off
 
rem ------------------------------------
set width=1024
set height=512
set ext=jpg
set src=source
set dst=%ext%%width%
set iview=c:\Progra~1\IrfanView\i_view32.exe
 
rem ------------------------------------
set a=%a% %cd%\%src%\*.*
set a=%a% /resample
set a=%a% /resize=(%width%,%height%)
set a=%a% /jpgq=100
set a=%a% /convert=%cd%\%dst%\*.%ext%
 
rem ------------------------------------
mkdir %dst%
%iview% %a%
dir /b %dst% > files.txt
setting a batch file as Windows Shell
@echo off
echo.
echo   The computer needs to be restarted
echo   to enter the present mode
echo.
echo   Press a key to continue...
pause > nul
set shell="%cd%\Globe4D.bat"
reg add "HKLM\Software\Microsoft\Windows NT\Currentversion\Winlogon" /f /v Shell /t REG_SZ /d %shell% > nul
echo %shell%
shutdown /r /t 0
set Windows shell back to Explorer.exe
@echo off
set shell=Explorer.exe
reg add "HKLM\Software\Microsoft\Windows NT\Currentversion\Winlogon" /f /v Shell /t REG_SZ /d %shell%
shutdown /r /t 0
Change default editor of batchfiles

Open regedit at HKEYCLASSESROOT\batfile\shell\edit\
Set 'command' to your favorite editor followed by %1

Batch file menu
@echo off
cd /d %0\..
 
SetLocal EnableDelayedExpansion
set menucount=0
call :MenuAdd Run "Run Globe4D"
call :MenuAdd PresentMode "Reboot to Globe4D Present Mode"
call :MenuAdd WindowsMode "Reboot to Windows"
call :MenuAdd IniSettings "Edit Globe4D settings file"
call :MenuAdd UserAccountControl "User Account Control Settings"
call :MenuAdd Display "Display settings"
call :MenuAdd Power "Power settings"
call :MenuAdd Driver "Install driver"
call :MenuAdd Quicktime "Install Quicktime"
call :MenuAdd Command "Open Command shell"
call :MenuAdd Explorer "Open Explorer"
call :MenuAdd Reboot "Reboot"
call :MenuAdd Shutdown "Shutdown"
call :MenuAdd ExitMenu "Exit Menu"
 
if [%1]==[/run] goto Run
 
:Menu
cls
echo.
echo  Globe4D - interactive four-dimensional globe
echo  Copyright (c) 2005-2012 www.globe4d.com
echo. 
for /f "usebackq delims== tokens=1-2" %%i in (`set title`) do echo %%j
echo.
 
set /p input="> "
set label=!label_%input%!
if [%label%]==[] goto Menu
goto %label%
 
exit
 
:MenuAdd
set /a menucount+=1
set myvar=%2
set noquotes=!myvar:"=!
set withzero=0!menucount!
set withzero=!withzero:~-2!
set withspace= !menucount!
set withspace=!withspace:~-2!
set title_%withzero%=  %withspace%. %noquotes%
set label_%menucount%=%1
goto :eof
 
:PresentMode
set shell="%~p0Globe4D.bat /run"
goto SetShell
 
:WindowsMode
set shell=explorer.exe 
goto SetShell
 
:UserAccountControl
UserAccountControlSettings.exe
cls
echo.
echo  The computer needs to be restarted after  
echo  changing the User Account Control Settings.
echo.
choice /m " Do you want to reboot now?"
if [%errorlevel%]==[1] shutdown /r /t 0
goto Menu
 
:Command
cls
start cmd
goto Menu
 
:Explorer
start .
goto Menu
 
:Run
cls
Globe4D.exe
pause
goto Menu
 
:IniSettings
start data/Globe4D.ini
goto Menu
 
:Display
start desk.cpl
goto Menu
 
:Power
start powercfg.cpl
goto Menu
 
:Driver
start drivers
start mmc devmgmt.msc
goto Menu
 
:Quicktime
start tools/QuickTimeInstaller.exe
goto Menu
 
:SetShell
reg add "HKLM\Software\Microsoft\Windows NT\Currentversion\Winlogon" /f /v Shell /t REG_SZ /d %shell% >nul 2>&1 
if [%errorlevel%]==[1] (
  cls
  echo.
  echo  There was a problem changing the Windows Shell.
  echo  Please first set 'User Account Control' to 'Never notify'
  echo.
  echo  Press a key to open the settings window.
  pause > nul
  goto UserAccountControl
) else (
  cls
  goto Reboot
)
 
:Reboot
echo.
echo  Press a key to restart the computer.
pause > nul
shutdown /r /t 0
goto Menu
 
:Shutdown
cls
echo.
echo  Press a key to turn off the computer.
pause > nul
shutdown /s /t 0
goto Menu
 
:ExitMenu
exit
batch.txt · Last modified: 2014/11/23 16:33 (external edit)