De Vrijes website Menu sluitenMenu openenNaar top gaan

Menu opties

Voorbeeld van een cmd batch programma

::=== File 'cmdexm.cmd' ========================================================
:: Example of a batch file with:
:: - 1st parameter: File name with surrounding quotes
:: - 2nd parameter: Log file name (without extension)
:: - 3rd parameter: 'No' or 'Yes' (in lower- or in uppercase or mixed)
:: - 4th parameter: 'N[o]' or 'Y[e[s]] (both case insensitive)
:: - 5th parameter: (not used yet)
::******************************************************************************
:: Initialization
::******************************************************************************
@echo off
set rtnpgm=0
::******************************************************************************
:: Concerning program parameters:
::******************************************************************************
echo ---------------------------------------------------------------------------
echo Variable (Perc)* gives all program parameters:
echo … %*
echo Variable (Perc)0 gives program name:
echo … %0
echo Variable (Perc)1 gives 1st program parameter:
echo … %1
echo Variable (Perc)2 gives 1st program parameter:
echo … %2
echo Variable (Perc)3 gives 1st program parameter:
echo … %3
echo Variable (Perc)4 gives 1st program parameter:
echo … %4
echo Variable (Perc)5 gives 1st program parameter:
echo … %5
echo Variable (Perc)6 gives 1st program parameter:
echo … %6
echo Variable (Perc)7 gives 1st program parameter:
echo … %7
echo Variable (Perc)8 gives 1st program parameter:
echo … %8
echo Variable (Perc)9 gives 1st program parameter:
echo … %9
pause
echo ---------------------------------------------------------------------------
echo For every program parameter (n = 1, 2, …, 9) and when it is a file name:
echo Variable (Perc)(tilde)1 gives program parameter without surrounding quotes:
echo … %~1
echo Variable (Perc)(tilde)a1 gives the list of file attributes:
echo … %~a1
echo Variable (Perc)(tilde)d1 gives the drive lettre:
echo … %~d1
echo Variable (Perc)(tilde)f1 gives the fully qualified path name
echo          (incl. file name):
echo … %~f1
echo Variable (Perc)(tilde)n1 gives the file name but without file extension:
echo … %~n1
echo Variable (Perc)(tilde)p1 gvives the fully qualified path name but
echo          without file name:
echo … %~p1
echo Variable (Perc)(tilde)s1 gives the short file name in format
echo          'nnnnnnnn.xxx':
echo … %~s1
echo Variable (Perc)(tilde)t1 gives the date and time:
echo … %~t1
echo Variable (P)(tilde)x1 gives only the file extension:
echo … %~x1
echo Variable (Perc)(tilde)z1 gives the file size:
echo … %~z1
echo Two or more modifiers above can be combined, for instance:
echo Variable (Perc)(tilde)ftz1:
echo … %~ftz1
pause
echo ---------------------------------------------------------------------------
echo In order to get data of the file via the subroutine sbrwr1:
call :sbrwr1 exm.cmd
echo Date, time and size: %sbrext%
pause
::******************************************************************************
:: Concerning connectation
::******************************************************************************
echo ---------------------------------------------------------------------------
call :sbrwr3 %~dp0 %2 .txt
set flilog=%wrk%
echo Log path: %wrk%
pause
::******************************************************************************
:: Concerning input from keyboard
::******************************************************************************
echo ---------------------------------------------------------------------------
set wrk="D:\xxx\yyy\zzz\xxx.xxx"
echo Enter your input data
copy con %wrk%
if %errorlevel% == 1 echo The output path '%wrk%' cannot be found or is INVALID
pause
::******************************************************************************
:: Concerning FOR
::******************************************************************************
echo ---------------------------------------------------------------------------
set cntwrk=1
for /f "tokens=*" %%G IN ('dir /b *.cmd') do (
call :sbrwr2 %%G )
pause
::******************************************************************************
:: Concerning IF
::******************************************************************************
echo ---------------------------------------------------------------------------
if exist %flilog% (
echo Log file '%flilog%' exists
) else (
echo Log file '%flilog%' do NOT exist)
pause
echo ---------------------------------------------------------------------------
set wrk=correct
if /i not "%3" == "N" (
if /i not "%3" == "NO" (
if /i not "%3" == "Y" (
if /i not "%3" == "YE" (
if /i not "%3" == "YES" (set wrk=INVALID!)))))
echo 3th parameter '%3' is %wrk%
pause
echo ---------------------------------------------------------------------------
set wrk=correct
if not "%4" == "N" (
if not "%4" == "No" (
if not "%4" == "Y" (
if not "%4" == "Ye" (
if not "%4" == "Yes" (set wrk=INVALID!)))))
echo 4th parameter '%4' is %wrk%
pause
::******************************************************************************
:: Exit
::******************************************************************************
exit /b &rtnpgm%
::******************************************************************************
:: Subroutines
::******************************************************************************
:sbrwr1
set sbrext=%~tz1
exit /b
::------------------------------------------------------------------------------
:sbrwr2
echo File number %cntwrk%: %1
set /a cntwrk+=1
exit /b
::------------------------------------------------------------------------------
:sbrwr3
set wrk=%1%2%3%4%4%6%7%8%9
exit /b
::==============================================================================