Advertisement
npocmaka

split2.0.bat

Jun 24th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @echo off
  2.  
  3. setlocal enableExtensions
  4. rem :-----------------------------------------
  5. rem : check if should pront the help message
  6. rem :-----------------------------------------
  7. if "%~2" equ "" goto :help
  8. for %%H in (/h -h /help -help) do (
  9.     if /I "%~1" equ "%%H" goto :help
  10. )
  11. if not exist "%~1" echo file does not exist & exit /b 1
  12.  
  13.  
  14. rem :-----------------------------------------
  15. rem : validate input
  16. rem :-----------------------------------------
  17. set /a size=%~2
  18. if not defined size echo something wrong with size parameter & exit /b 2
  19. if %size%0 LSS 00 echo not a valid number passed as a parameter & exit /b 3
  20.  
  21. rem : -- two hex symbols and an empty space are 1 byte
  22. rem : -- so the sum of all hex symbols
  23. rem : -- per part should be tripled
  24. set /a len=%size%*2
  25. set "file=%~dfn1"
  26.  
  27. for %%F in ("%file%") do set fn=%%~nxF
  28.  
  29. rem : -- clear temp data
  30. del /F /Q "%temp%\file" >nul 2>&1
  31. del /F /Q  "%temp%\fn.p.*" >nul 2>&1
  32. certutil -encodehex -f "%file%" "%temp%\file" >nul
  33. set "part=1"
  34.  
  35. setlocal enableDelayedExpansion
  36.     set "hex_str="
  37.     set hex_len=0
  38.     break>%temp%\fn.p.!part!
  39.    
  40. rem : -- reads the hex encoded file
  41. rem : -- and make it on a parts that will
  42. rem : -- decoded with certutil
  43.  
  44. rem :-- the delimitier is <tab> wich separates
  45. rem :-- line number from the rest of the information
  46. rem :-- in the hex file
  47. for /f "usebackq tokens=2 delims=   " %%A in ("%temp%\file") do (
  48.     set "line=%%A"
  49.     rem : -- there's a double space inthe middle of the line
  50.     rem :-- so here the line is get
  51.     set hex_str=!hex_str!!line:~0,48!
  52.     rem :-- empty spaces are cleared
  53.     set hex_str=!hex_str: =!
  54.    
  55.     rem :-- the length of the hex line
  56.     set /a hex_len=hex_len+32
  57.    
  58.     rem : -- len/size is reached
  59.     rem : -- and the content is printed to a hex file
  60.     if !hex_len! GEQ !len! (
  61.         set /a rest=hex_len-len
  62.         for %%A in (!rest!) do (
  63.             (echo(!hex_str:~0,-%%A!)>>%temp%\fn.p.!part!
  64.             rem : -- the rest of the content of the line is saved
  65.             set hex_str=!hex_str:~-%%A!
  66.             set /a hex_len=hex_len-rest
  67.         )
  68.         certutil -decodehex -f %temp%\fn.p.!part! %fn%.part.!part! >nul
  69.         echo -- !part!th part created --
  70.         rem :-- preprarin next hex file
  71.         set /a part=part+1
  72.         break>%temp%\fn.p.!part!
  73.         rem :-- reinitilization of the len/size of the file part
  74.         set /a len=%size%*2
  75.     )
  76.     rem : -- a buffer that not allows to
  77.     rem : -- to enter too long commmands
  78.     rem : -- used to reduce disk operations
  79.     if !hex_len! GEQ 7800 (
  80.         (echo(!hex_str!)>>%temp%\fn.p.!part!
  81.         set "hex_str="
  82.         set hex_len=0
  83.         rem :-- the size that need to be reached is reduces
  84.         rem :-- as there's alredy part of the part of the file
  85.         rem :-- added to the hex file
  86.         set /a len=!len!-7800
  87.         if !len! LSS 0 set len=0
  88.  
  89.     )
  90.  
  91. )
  92. rem : -- adding the rest of the file
  93. echo !hex_str!>>%temp%\fn.p.!part!
  94. certutil -decodehex -f %temp%\fn.p.!part! %fn%.part.!part! >nul
  95. echo -- !part!th part created --
  96.  
  97. rem : -- clear created temp data
  98. del /F /Q  %temp%\fn.p.* >nul 2>&1
  99. del /F /Q  %temp%\file >nul 2>&1
  100. endlocal
  101. endlocal
  102.  
  103. goto :eof
  104. rem :-----------------------------------------
  105. rem : help message
  106. rem :-----------------------------------------
  107.  
  108. :help
  109. echo\
  110. echo Splits a file on parts by given size in bytes
  111. echo\
  112. echo\
  113. echo    %0 file size
  114. echo\
  115. echo\
  116. echo by Vasil "npocmaka" Arnaudov
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement