Advertisement
npocmaka

csv_splitter.bat

Jan 15th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @echo off
  2. rem -- splits CSV file based on the values of the
  3. rem    first two entries on each line
  4. rem --
  5. if "%~2" equ "" (
  6.     echo ERROR: Destination folder is not set
  7.     call :help
  8.     goto :eof
  9. )
  10.  
  11. if not exist "%~1" (
  12.     echo ERROR: Source file does not exist
  13.     call :help
  14.     goto :eof
  15. )
  16.  
  17. if not exist "%~2\" (
  18.     mkdir "%~2\"
  19. )
  20.  
  21. rem -- deleting all content in result folder to prevent
  22. rem    data collisions
  23. rem --
  24. del "%~2\*" /Q /F >nul 2>&1
  25. set result_dir="%~2"
  26. rem -- getting the short name of source file to deal with eventual
  27. rem    spaces in its name
  28. rem --
  29. set sourse=%~1
  30. echo\
  31. echo -- START SPLITTING `%~f1` --
  32. setlocal
  33.  
  34. rem  -- reading the first line of the CSV file --
  35. setlocal EnableDelayedExpansion
  36. for /f "usebackq delims=" %%a in ("!sourse!") do (
  37.   endlocal
  38.   set "firstline=%%a"
  39.   goto :endloop
  40. )
  41. :endloop
  42.  
  43. rem -- processing the lines of the CSV --
  44. for /f "usebackq skip=1 tokens=*" %%L in ("%sourse%") do (
  45.     for /f "tokens=1,2 delims=, " %%S in ("%%L") do (
  46.         rem -- the result file for each date is called
  47.         rem    after the first two values of the CSV line
  48.         rem    wich is also used as a notification for a
  49.         rem    changed date
  50.         rem --
  51.         if not exist %result_dir%\%%S%%T.txt (
  52.             echo -- PROCESSING %%S%%T.txt --
  53.             SETLOCAL ENABLEDELAYEDEXPANSION
  54.             echo !firstline!>!result_dir!\%%S%%T.txt
  55.             ENDLOCAL   
  56.         )
  57.         ( echo %%L>>%result_dir%\%%S%%T.txt)
  58.     )
  59. )
  60. endlocal
  61. echo -- SPLITTING FINISHED --
  62. call :info  
  63. goto :eof
  64.  
  65. :help
  66. echo USAGE:
  67. echo\
  68. echo     %~n0%~x0 source_file destination_dir
  69. echo\
  70. echo    source_file     - path to the .csv file to process
  71. echo    destination_dir - directory where the splited
  72. echo                      files will be stored.If does not exist
  73. echo                      will be created.
  74. echo                      All content there will be deleted
  75. echo\
  76. echo NOTE: For file names with spaces enclose patameters in double quotes
  77. call :info  
  78. goto :eof
  79.  
  80. :info
  81. echo\
  82. echo\
  83. echo  -- Ver. 0.13
  84. echo  -- Created by Vasil "npocmaka" Arnaudov
  85. echo  -- Contact at npocmaka@gmail.com
  86. echo\
  87. goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement