Advertisement
npocmaka

ArrayList.bat

Jun 21st, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Winbatch 11.44 KB | None | 0 0
  1. @echo off
  2. rem ---- allows to call a subroutine inside FOR loop ----
  3. rem ---- thanks to Aacini ----
  4. rem --- by Vasil "npocmaka" Arnaudov
  5. if /I "%1" equ "call" shift & shift & goto %2
  6.  
  7. rem ==============================
  8. rem ==  checking paramateres    ==
  9. rem ==============================
  10.  
  11. if "%1" equ "-?" (
  12.     call :help
  13. )
  14.  
  15. if "%1" equ "?" (
  16.     call :help
  17. )
  18.  
  19. if "%1" equ "/?" (
  20.     call :help
  21. )
  22.  
  23. if "%1" equ "h" (
  24.     call :help
  25. )
  26.  
  27. if "%1" equ "-h" (
  28.     call :help
  29. )
  30.  
  31. if "%1" equ "help" (
  32.     call :help
  33. )
  34.  
  35. if "%1" equ "-help" (
  36.     call :help
  37. )
  38.  
  39. if "%1" equ "" (
  40.     call :help
  41. )
  42.  
  43. if "%1" equ "remote_machine" (
  44.     if "%~2" equ "" (
  45.         call :help
  46.         goto :eof
  47.     ) else (
  48.         call :remote_macine %2
  49.     )
  50. )
  51.  
  52. if "%1" equ "add" (
  53.     if "%~2" equ "" (
  54.         call :help
  55.         goto :eof
  56.     ) else (
  57.         call :add "%~2"
  58.     )
  59. )
  60.  
  61. if "%1" equ "getvalueat" (
  62.     if "%~3" equ "" (
  63.         call :help
  64.         goto :eof
  65.     ) else (
  66.         call :getvalueat %2 %3
  67.     )
  68. )
  69.  
  70. if "%1" equ "length" (
  71.     if "%~2" equ "" (
  72.         call :help
  73.         goto :eof
  74.     ) else (
  75.         call :length %2
  76.     )
  77. )
  78.  
  79. if "%1" equ "fast_list" (
  80.     call :fast_list
  81. )
  82.  
  83. if "%1" equ "list" (
  84.     call :list
  85. )
  86.  
  87. if "%1" equ "indexof" (
  88.     if "%~2" equ "" (
  89.         call :help
  90.         goto :eof
  91.     ) else (
  92.         call :indexof %2
  93.     )
  94. )
  95.  
  96. if "%1" equ "lastindexof" (
  97.     if "%~2" equ "" (
  98.         call :help
  99.         goto :eof
  100.     ) else (
  101.         call :lastindexof %2
  102.     )
  103. )
  104.  
  105. if "%1" equ "contains" (
  106.     if "%~2" equ "" (
  107.         call :help
  108.         goto :eof
  109.     ) else (
  110.         call :contains %2
  111.     )
  112. )
  113.  
  114. if "%1" equ "pop" (
  115.     call :pop
  116. )
  117.  
  118. if "%1" equ "queue" (
  119.     call :queue
  120. )
  121.  
  122. if "%1" equ "deleteat" (
  123.     if "%~2" equ "" (
  124.         call :help
  125.         goto :eof
  126.     ) else (
  127.         call :deleteat %2
  128.     )
  129. )
  130.  
  131. if "%1" equ "push" (
  132.     if "%~2" equ "" (
  133.         call :help
  134.         goto :eof
  135.     ) else (
  136.         call :push %2
  137.     )
  138. )
  139.  
  140. if "%1" equ "insert" (
  141.     if "%~3" equ "" (
  142.         call :help
  143.         goto :eof
  144.     ) else (
  145.         call :insert %2 %3
  146.     )
  147. )
  148.  
  149. if "%1" equ "isempty" (
  150.     call :isempty
  151. )
  152.  
  153. if "%1" equ "clear" (
  154.     call :clear
  155. )
  156.  
  157. if "%1" equ "clear" (
  158.     call :clear
  159. )
  160.  
  161. goto :eof
  162.  
  163. rem ================================
  164. rem === subroutines definitions ====
  165. rem ================================
  166.  
  167. rem --- if it is not set will be ignored ---
  168. :remote_macine [%1 - the remote machine]
  169.     set remote_machine=\\%~1
  170. goto eof
  171.  
  172. :add [%1 - item to add]
  173. setlocal
  174.     for /f "tokens=1-2 delims==" %%J in ('at %remote_machine% 00:00 /every:31 "%~1"') do (
  175.         echo added item %%K : %~1
  176.         goto :endadd
  177.     )
  178.     :endadd
  179. endlocal & goto :eof
  180.  
  181.  
  182. :getvalueat [%1 - position of the item;%2 - variable to store value at]
  183. setlocal
  184.     set /a errorlevel=0
  185.     set var=%2
  186.    
  187.     for /f "tokens=* delims= " %%E in ('at %remote_machine%  %1 ^| find "The AT job ID does not exist" ') do (
  188.         if "%%E" NEQ "" (
  189.             echo IndexOutOfBoundException
  190.             goto :eof
  191.         )
  192.      )
  193.    
  194.    
  195.     for /f "tokens=1,* delims=: " %%I in ('at %remote_machine%  %1 ^| find "Command:"') do (
  196.         if "%%I" EQU "" (
  197.             echo IndexOutOfBoundException
  198.             goto :eof          
  199.         )
  200.         set "variable=%%~J" >nul
  201.     )
  202.    
  203.    
  204. endlocal & call set  "%var%=%variable%"
  205. goto :eof
  206.  
  207. :length [%1 - value to store length at ] - returns 0 on empty.
  208. setlocal
  209.     set var=%1
  210.     set /a variable=0
  211.     setlocal ENABLEDELAYEDEXPANSION
  212.     for /f "eol=- tokens=1 delims= " %%L in ('at %remote_machine%  ^| find "Each"') do (
  213.         set /a variable=!variable!+1
  214.     )
  215.     endlocal & set variable=%variable%
  216. endlocal & call set /a "%var%=%variable%"
  217. goto :eof
  218.  
  219. :fast_list - list elements and numbers
  220. setlocal
  221.   rem -- check if there's local time settings hour suffix
  222.     for /f "tokens=1,2,3 delims= " %%T in ('date /t') do (
  223.         if "%%V" EQU "" (
  224.             set /a last_column=5
  225.         ) else (
  226.             set /a last_column=6
  227.         )
  228.     )
  229.    
  230.    
  231.    if %last_column% EQU 5 (
  232.         for /f "eol=- tokens=1-6,* delims= " %%A in ('at %remote_machine%  ^| find "Each"') do (
  233.             echo %%A : %%~F
  234.         )
  235.     )
  236.    
  237.     if %last_column% EQU 6 (
  238.         for /f "eol=- tokens=1-5,* delims= " %%A in ('at %remote_machine%  ^| find "Each"') do (
  239.             echo %%A : %%~F
  240.         )
  241.     )
  242.    
  243.  
  244. endlocal
  245. goto :eof
  246.  
  247. :list - list elements and numbers
  248. setlocal
  249.     call :length len
  250.     setlocal ENABLEDELAYEDEXPANSION
  251.     for /l %%L in (1,1,!len!) do (
  252.         call :getvalueat %%L val >nul
  253.         echo %%L : !val!
  254.     )
  255.     endlocal
  256. endlocal
  257. goto :eof
  258.  
  259.  
  260.  
  261. :indexof [%1 - check if is contained] - echoes first index
  262. setlocal
  263.     rem -- check if there's local time settings hour suffix
  264.    
  265.     for /f "tokens=1,2,3 delims= " %%T in ('date /t') do (
  266.         if "%%V" EQU "" (
  267.             set /a last_column=5
  268.         ) else (
  269.             set /a last_column=6
  270.         )
  271.     )
  272.     set /a index=-1
  273.    
  274.     if %last_column% EQU 5 (
  275.         for /f "eol=- tokens=1-6,* delims= " %%A in ('at %remote_machine%  ^| find "Each"') do (
  276.                     if "%%~F" EQU "%~1" (
  277.                         set /a index=%%A >nul
  278.                         goto :endcheck
  279.                     )
  280.         )
  281.     )
  282.    
  283.     if %last_column% EQU 6 (
  284.         for /f "eol=- tokens=1-5,* delims= " %%A in ('at %remote_machine%  ^| find "Each"') do (
  285.                     if "%%~F" EQU "%~1" (
  286.                         set /a index=%%A
  287.                         goto :endcheck
  288.                     )
  289.         )
  290.     )
  291.    
  292.     :endcheck
  293. endlocal &  echo %index%
  294. goto :eof
  295.  
  296.  
  297. :lastindexof [%1 - check if is contained] - echoes last index
  298. setlocal
  299.     rem -- check if there's local time settings hour suffix
  300.    
  301.     for /f "tokens=1,2,3 delims= " %%T in ('date /t') do (
  302.         if "%%V" EQU "" (
  303.             set /a last_column=5
  304.         ) else (
  305.             set /a last_column=6
  306.         )
  307.     )
  308.    
  309.     set /a index=-1
  310.    
  311.    if %last_column% EQU 5 (
  312.         for /f "eol=- tokens=1-6,* delims= " %%A in ('at %remote_machine%  ^| find "Each"') do (
  313.                     if "%%~F" EQU "%~1" (
  314.                         set /a index=%%A >nul
  315.                     )
  316.         )
  317.     )
  318.    
  319.     if %last_column% EQU 6 (
  320.         for /f "eol=- tokens=1-5,* delims= " %%A in ('at %remote_machine%  ^| find "Each"') do (
  321.                     if "%%~F" EQU "%~1" (
  322.                         set /a index=%%A
  323.                     )
  324.         )
  325.     )
  326.    
  327.     :endcheck
  328. endlocal &  echo %index%
  329. goto :eof
  330.  
  331. :contains [%1 - check if is contained] set errorlevel to -1 if is contained
  332. setlocal
  333.     set /a errorlevel=0
  334.     set result=
  335.     for /f "usebackq" %%C in (`%~f0 call :indexof %~1`) do (
  336.         set result=%%C
  337.     )
  338.    
  339.     if "%result%" NEQ "-1" (
  340.         set /a errorlevel=1
  341.     ) else (
  342.         set /a errorlevel=0
  343.     )
  344. endlocal & set /a errorlevel=%errorlevel%
  345. goto :eof
  346.  
  347.  
  348. :pop - deletes and echoes the first element in the list
  349. setlocal
  350.        
  351.     if "%1" EQU "" (
  352.         call :length left
  353.     ) else (
  354.         set /a left=%1 > nul
  355.     )
  356.    
  357.     if %left% equ 0 (
  358.         call at %remote_machine%  /delete /yes
  359.         goto :eof
  360.     )
  361.    
  362.  
  363.     call :getvalueat %left% current_element
  364.     call :pop %left%-1
  365.    
  366.     rem :: get first element
  367.     if %left% EQU 1 (
  368.         echo %current_element%
  369.         goto :eof
  370.     )
  371.  
  372.     at %remote_machine%  00:00 /every:31 %current_element% >nul
  373.    
  374. endlocal
  375. goto :eof
  376.  
  377. :queue - delete and return the last element in the list
  378. setlocal
  379.    
  380.     if [%len%] EQU [] (
  381.         call :length len
  382.     )
  383.    
  384.     if "%len%" EQU "0" (
  385.         goto :eof
  386.     )
  387.    
  388.     if "%1" EQU "" (
  389.         call :length left
  390.     ) else (
  391.         set /a left=%1 > nul
  392.     )
  393.     if %left% equ 0 (
  394.         call at %remote_machine%  /delete /yes
  395.         goto :eof
  396.     )
  397.    
  398.     call :getvalueat %left% current_element
  399.     call :queue %left%-1
  400.  
  401.     if %left% EQU %len% (
  402.         echo %current_element%
  403.         goto :eof
  404.     )
  405.  
  406.     at %remote_machine%  00:00 /every:31 "%current_element%" >nul
  407.    
  408. endlocal
  409. goto :eof
  410.  
  411. :deleteat [%1 - number of the element to be deleted] - returns the value of deleted element
  412. setlocal
  413.     set /a deleteat=%1
  414.        
  415.     if "%2" EQU "" (
  416.         call :length left
  417.     ) else (
  418.         set /a left=%2 > nul
  419.     )
  420.     if %left% equ 0 (
  421.         call at %remote_machine%  /delete /yes
  422.         goto :eof
  423.     )
  424.        
  425.     call :getvalueat %left% current_element
  426.     call :deleteat %deleteat% %left%-1
  427.    
  428.     if %left% EQU %deleteat% (
  429.         echo %current_element%
  430.         goto :eof
  431.     )
  432.  
  433.     at %remote_machine%  00:00 /every:31 "%current_element%" >nul
  434.    
  435. endlocal
  436. goto :eof
  437.  
  438. :push [%1 - set this value on the first position]
  439. setlocal
  440.     set  "push=%~1"
  441.        
  442.     if "%2" EQU "" (
  443.         call :length left
  444.     ) else (
  445.         set /a left=%2 > nul
  446.     )
  447.     if %left% equ 0 (
  448.         at %remote_machine%  /delete /yes
  449.         at %remote_machine% 00:00 /every:31 %push% >nul
  450.         goto :eof
  451.     )
  452.    
  453.     call :getvalueat %left% current_element
  454.     call :push "%push%" %left%-1
  455.    
  456.     at %remote_machine%  00:00 /every:31 "%current_element%" >nul
  457.    
  458. endlocal
  459. goto :eof
  460.  
  461. :insert [%1 - value to insert , %2 - position to insert ]
  462. setlocal
  463.     set  "insert=%~1"
  464.     set  /a pos=%2
  465.    
  466.    
  467.     if [%len%] EQU [] (
  468.         call :length len
  469.         setlocal enabledelayedexpansion
  470.         if !pos! GTR !len! (
  471.             echo echo IndexOutOfBoundException
  472.             goto :eof
  473.         )
  474.         endlocal
  475.     )  
  476.    
  477.    
  478.     if "%3" EQU "" (
  479.         call :length left
  480.     ) else (
  481.         set /a left=%3 > nul
  482.     )
  483.     if %left% equ 0 (
  484.         at %remote_machine%  /delete /yes
  485.         goto :eof
  486.     )
  487.    
  488.     call :getvalueat %left% current_element
  489.     call :insert "%insert%" %pos% %left%-1
  490.    
  491.     if %left% EQU %pos% (
  492.         at %remote_machine%  00:00 /every:31 "%insert%" >nul
  493.     )
  494.    
  495.     at %remote_machine%  00:00 /every:31 "%current_element%" >nul
  496.    
  497. endlocal
  498. goto :eof
  499.  
  500. :clear - clears the list
  501.     at %remote_machine% /delete /yes >nul
  502. goto :eof
  503.  
  504. :isempty - checks if the list is empty
  505. setlocal
  506.     call :length len
  507.     if "%len%" EQU "0" (
  508.         echo YES
  509.     ) else (
  510.         echo NO
  511.     )
  512. endlocal
  513. goto :eof
  514.  
  515. :size - echoes the length
  516. setlocal
  517.     call :length len
  518.     echo %len%
  519. endlocal
  520. goto :eof
  521.  
  522. :help
  523. echo %~n0  is a script that uses AT command for enumerated list.
  524. echo you can have only one instance of this list - except if you're
  525. echo not using remote host for storage (see remote_host key switch).
  526. echo The list will be persisted on the system even after restart.
  527. echo Following command swithes are slow are not recommended for often usage:
  528. echo pop,queue,deleteat,push,insert - as they are using recursion and delete and
  529. echo preset the values again (at does not reuse jobs ids even if the jobs are deleted ).
  530. echo Use this script only on your own responsibility.For more info check commands bellow.
  531. echo\
  532. echo %~n0 ^| [-? ^|? ^|/? ^|-h ^|-help ^|help ^|h]  prints this message
  533. echo    - prints this message
  534. echo\
  535. echo %~n0 add item   - adds an item to the list.Avoid following sympls
  536. echo      as they are special symbols for cmd.exe :  ( , ), ^&, ^| , ^> , ^< , ^^ , %% , ^= , : , ~ .
  537. echo\
  538. echo %~n0 getvalueat position variable_name - gets the value
  539. echo    at given possition and store the result to the variable name.
  540. echo    If outflows the list prinst "indexOutOfBoundException".
  541. echo\
  542. echo %~n0 length variable_name - get the length and stores the result in variable_name.
  543. echo     If list is empty sets 0.
  544. echo\
  545. echo %~n0 remote_macine machine_name - sets remote machine where the list is stored.
  546. echo\
  547. echo %~n0 fastlist - lists elements using internal AT command listing
  548. echo      the elements will be not sorted by number but will run faster than list.
  549. echo\
  550. echo %~n0 list - lists all elements.
  551. echo\
  552. echo %~n0 indexof value - returns the possition of first appearance of the given value.
  553. echo\
  554. echo %~n0 lastindexof value - returns the possition of last appearance of given value.
  555. echo\
  556. echo %~n0 contains value - check if the given value is contained in the
  557. echo      list.Set ERRORLEVEL to 1 if so.
  558. echo\
  559. echo %~n0 pop  - deletes the first element and echoes it's value.
  560. echo\
  561. echo %~n0 queue - deletes the last element and echoes it's value
  562. echo\
  563. echo %~n0 deleteat position  - deletes an element on given position
  564. echo     and echoes it's value.
  565. echo %~n0 push value - set an element on first possition with given value.
  566. echo\
  567. echo %~n0 insert value position - insert an element with the given position and value.
  568. echo\
  569. echo %~n0 clear - clears the list.
  570. echo\
  571. echo %~n0 size - echoes the size (length).
  572. echo\
  573. echo %~n0 isempty - checks if the list is empty .
  574. echo\  
  575. goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement