elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Los 10 CVE más críticos (peligrosos) de 2020


+  Foro de elhacker.net
|-+  Programación
| |-+  Scripting
| | |-+  [BATCH] [APORTE] TextCutter (Delimita texto de un archivo y lo corta en trozos)
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [BATCH] [APORTE] TextCutter (Delimita texto de un archivo y lo corta en trozos)  (Leído 2,487 veces)
Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.809



Ver Perfil
[BATCH] [APORTE] TextCutter (Delimita texto de un archivo y lo corta en trozos)
« 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



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. ----------------------------------------------


« Última modificación: 20 Septiembre 2012, 07:57 am por EleKtro H@cker » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
BHB Convierte archivo a texto y lo reconstruye en batch 2.0 « 1 2 »
Scripting
carlitos.dll 10 15,164 Último mensaje 24 Agosto 2008, 17:17 pm
por Martín Fierro
[BATCH]Mostrar Suma de las lineas de un archivo de texto. « 1 2 »
Scripting
FranciskoAsdf 10 11,293 Último mensaje 1 Julio 2011, 09:27 am
por WaAYa HaCK
Extraer trozos de una cadena de texto en C
Programación C/C++
davidnew 8 29,409 Último mensaje 10 Abril 2012, 15:57 pm
por Synth3tik0
[BATCH] [APORTE] TextFont Installer (Instalador automático de fuentes de texto)
Scripting
Eleкtro 1 4,537 Último mensaje 12 Septiembre 2017, 19:26 pm
por lahero85
[BATCH][Aporte]MORF:Transforma texto Unicode a ANSI
Scripting
Segadorfelix 7 6,686 Último mensaje 10 Diciembre 2012, 01:13 am
por Segadorfelix
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines