Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions DateAndTime/DateParts.bat

This file was deleted.

31 changes: 0 additions & 31 deletions DateAndTime/W32DOW.bat

This file was deleted.

24 changes: 0 additions & 24 deletions DateAndTime/W32tmSleep.bat

This file was deleted.

25 changes: 0 additions & 25 deletions DateAndTime/getTime.bat

This file was deleted.

18 changes: 0 additions & 18 deletions DateAndTime/sleeptp.bat

This file was deleted.

26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
batch.scripts
=============
# Batch Script Utils and Examples

## 1. Naming Conventions

### 1.1. Functions

Function names are capitalized and begin with the 'F' character. For example:

```bash
CALL :FFUNCTION
GOTO :EOF

:FFUNCTION
ECHO The FFUNCTION function has been executed!
EXIT /B 0
```

### 1.2. Variables

Variables are written in pascal case and begin with the 'V' character. For example:

```bash
SET /A VResultData=1
```
166 changes: 166 additions & 0 deletions [01].DataStructures/BinaryTree.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
:: ===========================================================================================================
:: @file BinaryTree.bat
:: @brief Binary tree implementation in batch file
:: @usage BinaryTree.bat
:: @see https://github.com/sebetci/batch.script/[01].DataStructures/BinaryTree.bat
:: @reference https://en.wikipedia.org/wiki/Binary_tree
:: @reference https://github.com/npocmaka/batch.scripts/tree/master/dataStructures
:: @reference https://www.dostips.com/forum/viewtopic.php?t=7642
:: @todo The FFIND method does not search after the first level of the tree.
:: @todo The FDELETE method will be developed.
:: ===========================================================================================================

@ECHO OFF
CALL :FMAIN
GOTO :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: @function This function inserts new values to the tree and then finds for some values in the tree.
:: @parameter None
:: @return None
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:FMAIN
SETLOCAL ENABLEDELAYEDEXPANSION

:: This variable must be assigned a value of 1 or 0.
:: If this variable is set to 1, the echo command in its sub-methods will work.
SET /A VDebug=1

:: The following commands add new nodes to the tree.
CALL :FINSERT VTreeName 1
CALL :FINSERT VTreeName 3
CALL :FINSERT VTreeName 5

:: The following commands find for the value 1 in the tree.
:: If the FFIND method finds the value 1, the variable %ERRORLEVEL% has the value 1.
ECHO.
ECHO [VDebug:MAIN] Searching for value 1
CALL :FFIND VTreeName 1
ECHO [VDebug:MAIN] Return Value: %ERRORLEVEL%
IF %ERRORLEVEL% EQU 0 (ECHO [VDebug:MAIN] 1 was found in the three.) ELSE (ECHO [VDebug:MAIN] 1 not found in tree.)

:: The following commands find for the value 1 in the tree.
:: If the FFIND method does not find the value 8, the variable %ERRORLEVEL% will have the value 0.
ECHO.
ECHO [VDebug:MAIN] Searching for value 8
CALL :FFIND VTreeName 8
ECHO [VDebug:MAIN] Return Value: %ERRORLEVEL%
IF %ERRORLEVEL% EQU 0 (ECHO [VDebug:MAIN] 8 was found in the three.) ELSE (ECHO [VDebug:MAIN] 8 not found in tree.)

:: The tree content is printed to the console.
CALL :FPRINT VTreeName

:: This method content will be developed.
CALL :FDELETE VTreeName
EXIT /B 0

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: @function This function searches the Value value in the VTreeName tree. It returns 0 if the
:: searched value is found in the tree.
:: @parameter VTreeName This parameter is the name of the node.
:: @parameter VValue This parameter is the value of the node.
:: @return Returns 0 if value is found in the tree.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:FFIND VTreeName VValue
:: SETLOCAL ENABLEDELAYEDEXPANSION
SET /A VValue=%~2

IF %VDebug% EQU 1 ECHO [VDebug:FIND] Content-1: %VTreeName%
IF %VDebug% EQU 1 ECHO [VDebug:FIND] Content-2: %VValue%
IF %VDebug% EQU 1 ECHO [VDebug:FIND] Param-1: %1
IF %VDebug% EQU 1 ECHO [VDebug:FIND] Param-2: %2

IF %VTreeName% EQU %2 (
ENDLOCAL & (
EXIT /B 0
)
)

IF %VValue% GTR %1 (
IF DEFINED %1R (
ENDLOCAL & (
CALL :FFIND %1R %VValue%
)
) ELSE (
ENDLOCAL & (
EXIT /B 1
)
)
)

IF %VValue% LSS %1 (
IF DEFINED %1L (
ENDLOCAL & (
CALL :FFIND %1L %VValue%
)
) ELSE (
ENDLOCAL & (
EXIT /B 1
)
)
)

EXIT /B

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: @function This function adds a new node to the tree.
:: @parameter VTreeName This parameter is the name of the node.
:: @parameter VValue This parameter is the value of the node.
:: @return None
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:FINSERT VTreeName VValue
SETLOCAL
SET /A VValue=%~2

IF NOT DEFINED %~1 (
ENDLOCAL & (
SET "%~1=%VValue%"
EXIT /B 0
)
)

IF %VValue% GEQ %~1R (
IF NOT DEFINED %~1R (
ENDLOCAL & (
SET %~1R=%VValue%
EXIT /B 0
)
) ELSE (
ENDLOCAL & (
CALL :FINSERT %~1R %VValue%
)
)
)

IF %VValue% LSS %~1L (
IF NOT DEFINED %~1L (
ENDLOCAL & (
SET %~1L=%VValue%
EXIT /B 0
)
) ELSE (
ENDLOCAL & (
CALL :FINSERT %~1R %VValue%
)
)
)
EXIT /B 0

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: @function This function prints the names and values of the node nodes in the tree.
:: @parameter VTreeName This parameter is the name of the node.
:: @return None
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:FPRINT VTreeName
ECHO.
SET VTreeName
EXIT /B 0

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: @function This function deletes all nodes in a tree.
:: @parameter VTreeName This parameter is the name of the node.
:: @return None
:: @todo Investigate the feasibility of this method.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:FDELETE VTreeName
EXIT /B 0
26 changes: 26 additions & 0 deletions [02].DateAndTime/DateParts.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
:: ===========================================================================================================
:: @file DateParts.bat
:: @brief Gets date parts
:: @usage DateParts.bat
:: @see https://github.com/sebetci/batch.script/[02].DateAndTime/DateParts.bat
:: @reference https://stackoverflow.com/a/28250863/15032688
:: @reference https://www.dostips.com/forum/viewtopic.php?f=3&t=4555
:: @reference https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/makecab
:: @todo
:: ===========================================================================================================

@ECHO OFF
PUSHD "%TEMP%"

MAKECAB /D RPTFILENAME=~.RPT /D INFFILENAME=~.INF /F NUL >NUL

FOR /F "TOKENS=3-7" %%A IN ('FIND /I "MAKECAB"^<~.RPT') DO (
SET "VWeekDay=%%A"
SET "VCurrentDate=%%E-%%B-%%C"
SET "VCurrentTime=%%D"
)

DEL ~.*
POPD
ECHO %VWeekDay% %VCurrentDate% %VCurrentTime%
GOTO :EOF
35 changes: 35 additions & 0 deletions [02].DateAndTime/GetTime.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
:: ===========================================================================================================
:: @file GetTime.bat
:: @brief Gets time using typeperf command with put some effort to be made fast and
:: maximum compatible.
:: @usage GetTime.bat
:: @see https://github.com/sebetci/batch.script/[02].DateAndTime/GetTime.bat
:: @reference https://www.progress.com/blogs/understanding-iso-8601-date-and-time-format
:: @reference https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/typeperf
:: @todo
:: ===========================================================================================================

@ECHO OFF
SETLOCAL
:: Check if Windows is XP and use XP valid counter for UDP performance
:: In Windows 10 the variable "USERDOMAIN_ROAMINGPROFILE" returns the device name.
IF DEFINED USERDOMAIN_ROAMINGPROFILE (SET "V=V4" & ECHO Device Name: %USERDOMAIN_ROAMINGPROFILE%) ELSE (SET "V=")

:: The "typeperf" command writes performance counter data to the command window or to a supported log file format.
FOR /F "SKIP=2 DELIMS=," %%# IN ('TYPEPERF "\UDP%V%\*" -SI 0 -SC 1') DO (
IF NOT DEFINED VMonth (
FOR /F "TOKENS=1-7 DELIMS=.:/ " %%A IN (%%#) DO (
SET VMonth=%%A
SET VDate=%%B
SET VYear=%%C
SET VHour=%%D
SET VMinute=%%E
SET VSecond=%%F
SET VMillisecond=%%G
)
)
)

:: ISO 8601 Date and Time Format
ECHO %VYear%-%VMonth%-%VDate% %VHour%:%VMinute%:%VSecond%.%VMillisecond%
ENDLOCAL
Loading