Foro de elhacker.net

Programación => Scripting => Mensaje iniciado por: Eleкtro en 20 Septiembre 2012, 07:52 am



Título: [BATCH] [APORTE] TextCutter (Delimita texto de un archivo y lo corta en trozos)
Publicado por: Eleкtro en 20 Septiembre 2012, 07:52 am
He visto estos últimos años a mucha gente preguntando como delimitar un texto para cortarlo en trozos, y generar los subarchivos delimitados, así que me he visto en la casi necesidad de crear esta función para ese propósito, así les será más fácil.

Código:
@Echo OFF
Title TEXTCUTTER [By Elektro H@cker]

REM Call :TEXTCUTTER "ARCHIVO" "DELIMITADOR (A)" "DEIMITADOR (B)"

:: Ejemplo
Call :TEXTCUTTER "TEST.XML" "<fdaDeployJob" "</fdaDeployJob>"

Pause&Exit

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:TEXTCUTTER
Echo # # # # # # # #
Echo #             #
Echo # TEXT CUTTER # by Elektro H@cker
Echo #             #
Echo # # # # # # # # | MORE

Setlocal enabledelayedexpansion

Set "FILE=%~nx1"
SET "Delimiter_A=%~2"
SET "Delimiter_B=%~3"

:: Comprobaciones iniciales
If Exist "%FILE%" (
Echo # Procesar archivo : "%FILE%"
Echo # Delimitar desde  : "%Delimiter_A%"
Echo # Delimitar hasta  : "%Delimiter_B%" | MORE
) ELSE (Echo Archivo "%FILE%" no encontrado & GOTO:EOF)

Type "%FILE%" | FIND "%Delimiter_A%" >NUL || (Echo No se ha encontrado ninguna cadena con el delimitador (A^): "%Delimiter_A%" & GOTO:EOF)
Type "%FILE%" | FIND "%Delimiter_B%" >NUL || (Echo No se ha encontrado ninguna cadena con el delimitador (B^): "%Delimiter_B%" & GOTO:EOF)

:: Creamos un archivo temporal y le agregamos X lineas en blanco para evitar errores en el SORT de Batch.
REM Ajustamos el número de lineas a agregar para agilizar el proceso de generación de archivos.
REM 100 lineas si el archivo original tiene menos de 100 lineas.
REM 1.000 lineas si el archivo original tiene entre 100 y 999 lineas.
REM 10.000 lineas si el archivo original tiene entre 1.000 y 9.999 lineas.
REM 100.000 lineas si el archivo original tiene entre 10.000 y 99.999 lineas.
REM 1.000.000 lineas si el archivo original tiene entre 100.000 y 999.999 lineas o más de 1.000.000 de lineas.
For /F %%a in ('Type "%FILE%" ^| find /V /C ""') do (Echo %%a>"%TEMP%\%FILE%" & FOR %%? IN ("%TEMP%\%FILE%") DO (SET /A "longitud=%%~z? - 3"))
Echo+ > "%TEMP%\%FILE%"
IF "%LONGITUD%" LEQ "2" (Set /A "LINES=100")
IF "%LONGITUD%" EQU "3" (Set /A "LINES=1000")
IF "%LONGITUD%" EQU "4" (Set /A "LINES=10000")
IF "%LONGITUD%" EQU "5" (Set /A "LINES=100000")
IF "%LONGITUD%" GEQ "6" (Set /A "LINES=1000000")
Echo Generando un archivo temporal, espere...
For /L %%X in (2,1,%LINES%) Do (Echo+ >> "%TEMP%\%FILE%")

:: Eliminamos las lineas en blanco del archivo original y copiamos el resto en el archivo temporal.
Type "%FILE%" | FINDSTR "." >> "%TEMP%\%FILE%"

:: Obtenemos el número de las lineas que contienen los delimitadores [A] y [B].
For /F "Delims=:" %%X in ('findstr /I /N "%Delimiter_A%" "%TEMP%\%FILE%"') do (Set /A "NUM_A+=1" && Set "Delimiter_A_!NUM_A!=%%X")
For /F "Delims=:" %%X in ('findstr /I /N "%Delimiter_B%" "%TEMP%\%FILE%"') do (Set /A "NUM_B+=1" && Set "Delimiter_B_!NUM_B!=%%X")

:: Cortamos y generamos los archivos.
Echo+ & Echo Generando los archivos, espere... | MORE

For /L %%X in (1,1,%NUM_B%) Do (
For /F "Tokens=* Delims=:" %%@ in ('Type "%TEMP%\%FILE%"') do (

Set /A "LINE+=1"
SET "String=%%@"

IF NOT "!LINE!" GTR "!Delimiter_B_%%X!" (
IF "!LINE!" GEQ "!Delimiter_A_%%X!" (
IF NOT "!STRING!" EQU " " (
ECHO !STRING!>> "%~n1_%%X_%~x1"
)
)
)
)
Set /A "LIN_A=!Delimiter_A_%%X! - %LINES%", "LIN_B=!Delimiter_B_%%X! - %LINES%"
Set /A "LINE=0"

Echo [+] "%~n1_%%X_%~x1"
Echo     (Linea !LIN_A! hasta Linea !LIN_B!^) | MORE
)
Setlocal disabledelayedexpansion

Echo Listo.
GOTO:EOF

(http://img845.imageshack.us/img845/4466/captura2o.png)

PD: Recuerden, así pueden ocultar la salida:
Código
  1. Call :TEXTCUTTER "ARCHIVO" "DELIMITADOR (A)" "DEIMITADOR (B)" >NUL

Espero que les séa de ayuda.
Saludos.



El archivo que he usado en el ejemplo:
Test.XML
Código
  1. ----------------------------------------------
  2. <fdaDeployJob xml:lang="es-ES">
  3.  <fileInfo>
  4.   <displayName>Plus_IMS_ARG_DDDLEG_002_A_20120801.ZIP</displayName>
  5.   <description>DATOS AR_DDDPLUS Argentina Ambiente plus Agosto 2012 CLIENT SERVICE, IMSHEALTH
  6.    <additionalInfo></additionalInfo>
  7.    <loginRequired>0</loginRequired>
  8.    <approved>1</approved>
  9.    <emailNotification>1</emailNotification>
  10.   <activeDate>2012/09/19</activeDate>
  11.   <expirationDate>2012/10/30</expirationDate>
  12.    <fileRule>
  13.      <productRestrictions>
  14.      </productRestrictions>
  15.      <companyRestrictions>
  16.      </companyRestrictions>
  17.      <productCompanyRestrictions>
  18.      </productCompanyRestrictions>
  19.      <individualRestrictions>
  20.       <individualEmail owner="1">aduran@ar.imshealth.com</individualEmail>  
  21.       <individualEmail owner="0">mechenique@ar.imshealth.com</individualEmail>
  22.      </individualRestrictions>
  23. <fileUploader> </fileUploader>
  24. </fileRule>
  25.  </fileInfo>
  26. </fdaDeployJob>
  27. ----------------------------------------------  
  28. <fdaDeployJob xml:lang="en-EN">
  29.  <fileInfo>
  30.   <displayName>Plus_IMS_ELEKTRO_H@CKER.ZIP</displayName>
  31.   <description>blablablabla
  32.    <additionalInfo></additionalInfo>
  33.    <loginRequired>0</loginRequired>
  34.    <approved>1</approved>
  35.    <emailNotification>1</emailNotification>
  36.   <activeDate>2011/11/22</activeDate>
  37.   <expirationDate>2011/11/22</expirationDate>
  38.    <fileRule>
  39.      <productRestrictions>
  40.      </productRestrictions>
  41.      <companyRestrictions>
  42.      </companyRestrictions>
  43.      <productCompanyRestrictions>
  44.      </productCompanyRestrictions>
  45.      <individualRestrictions>
  46.       <individualEmail owner="1">www.elhacker.net</individualEmail>    
  47.       <individualEmail owner="0">Elektro H@cker</individualEmail>
  48.      </individualRestrictions>
  49. <fileUploader> </fileUploader>
  50. </fileRule>
  51.  </fileInfo>
  52. </fdaDeployJob>
  53. ----------------------------------------------