Advertisement
MrPolywhirl

ListFindItem.bat

Oct 22nd, 2013
1,421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @echo off
  2. setlocal enableDelayedExpansion
  3. cls
  4.  
  5. :: ------------------------------------------------------------------
  6. :: Sample Output
  7. :: ------------------------------------------------------------------
  8. :: [2013/10/22 23:07:26] "Finding item 1..."
  9. :: [2013/10/22 23:07:26] "Call -> :find ["("foo" "bar" "baz")" 1]"
  10. :: [2013/10/22 23:07:26] "The value is: "bar""
  11. :: ------------------------------------------------------------------
  12.  
  13. :: ------------------------------------------------------------------
  14. :: Variable declarations
  15. :: ------------------------------------------------------------------
  16. set RETURN=-1
  17. set MY_LIST=("foo" "bar" "baz")
  18. set TARGET_INDEX=1
  19.  
  20. :: -------------------------------------------------------------------
  21. :: Main procedure
  22. :: -------------------------------------------------------------------
  23. call :log "Finding item %TARGET_INDEX%..."
  24. call :find "%MY_LIST%" %TARGET_INDEX%
  25. call :log "The value is: %RETURN%"
  26. goto Exit
  27.  
  28. :: -------------------------------------------------------------------
  29. :: Function declarations
  30. :: -------------------------------------------------------------------
  31. :find
  32. call :log "Call -> :find [%*]"
  33. set RETURN=-1
  34. set /a i=0
  35. for %%a in %~1 do (
  36.     if !i! == %~2 (
  37.         set RETURN=%%a
  38.     )
  39.     set /a i=!i!+1
  40. )
  41. goto:EOF
  42.  
  43. :printDate
  44. for /f "tokens=2-4 delims=/ " %%a in ('echo %DATE%') do (
  45.   set mydate=%%c/%%a/%%b)
  46. for /f "tokens=1-3 delims=/:./ " %%a in ('echo %TIME%') do (
  47.   set mytime=%%a:%%b:%%c)
  48. echo|set /p="[%mydate% %mytime%] "
  49. goto:EOF
  50.  
  51. :log
  52. call :printDate & echo %*
  53. goto:EOF
  54.  
  55. :: -------------------------------------------------------------------
  56. :: End of script
  57. :: -------------------------------------------------------------------
  58.  
  59. :Exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement