Advertisement
npocmaka

EncoDetect

Apr 28th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @echo off
  2. :detect_encoding
  3. setLocal
  4. if "%1" EQU "-?" (
  5.     endlocal
  6.     call :help
  7.     exit /b 0
  8. )
  9. if "%1" EQU "-h" (
  10.     endlocal
  11.     call :help
  12.     exit /b 0
  13. )
  14. if "%1" EQU "" (
  15.     endlocal
  16.     call :help
  17.     exit /b 0
  18. )
  19.  
  20.  
  21. if not exist "%1" (
  22.     echo file does not exists
  23.     endlocal
  24.     exit /b 54
  25. )
  26.  
  27. if exist "%1\" (
  28.     echo this cannot be used against directories
  29.     endlocal
  30.     exit /b 53
  31. )
  32.  
  33. if "%~z1" EQU "0" (
  34.     echo empty files are not accepted
  35.     endlocal
  36.     exit /b 52
  37. )
  38.  
  39.  
  40.  
  41. set "file=%~snx1"
  42. del /Q /F "%file%.hex" >nul 2>&1
  43.  
  44. certutil -f -encodehex %file% %file%.hex>nul
  45.  
  46. rem -- find the first line of hex file --
  47.  
  48. for /f "usebackq delims=" %%E in ("%file%.hex") do (
  49.     set "f_line=%%E" > nul
  50.     goto :enfdor
  51. )
  52. :enfdor
  53. del /Q /F "%file%.hex" >nul 2>&1
  54.  
  55. rem -- check the BOMs --
  56. echo %f_line% | find "ef bb bf"     >nul && echo utf-8     &&endlocal && exit /b 1
  57. echo %f_line% | find "ff fe 00 00"  >nul && echo utf-32 LE &&endlocal && exit /b 5
  58. echo %f_line% | find "ff fe"        >nul && echo utf-16    &&endlocal && exit /b 2
  59. echo %f_line% | find "fe ff 00"     >nul && echo utf-16 BE &&endlocal && exit /b 3
  60. echo %f_line% | find "00 00 fe ff"  >nul && echo utf-32 BE &&endlocal && exit /b 4
  61.  
  62. echo ASCII & endlocal & exit /b 6
  63.  
  64.  
  65.  
  66. endLocal
  67. goto :eof
  68.  
  69. :help
  70. echo.
  71. echo  %~n0 file - Detects encoding of a text file
  72. echo.
  73. echo for each encoding you will recive a text responce with a name and a errorlevel codes as follows:
  74.  
  75. echo     1 - UTF-8
  76. echo     2 - UTF-16 BE
  77. echo     3 - UTF-16 LE
  78. echo     4 - UTF-32 BE
  79. echo     5 - UTF-32 LE
  80. echo     6 - ASCII
  81.  
  82. echo for empty files you will receive error code 52
  83. echo for directories  you will receive error code 53
  84. echo for not existing file  you will receive error code 54
  85. goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement