Ms-Dos - MS-Dos, Bat Simple?

 
Vista:

MS-Dos, Bat Simple?

Publicado por WiKe (2 intervenciones) el 16/11/2016 13:45:42
Hola a todos

Lo que quiero es muy facil de entender, lo dificil que no se bien k comandos usar en bat para conseguirlo

Tengo varios fichero TXT con unas 20.000 lineas cada uno, el problema que algunos tienen una sangria de 10 caracteres, y otros de 16, y otros de 5... lo que al trabajar con todos me produce errores

Me gustaria saber como crear un bat (por si me lo quereis explicar) o que bat necesito

lo que necesito es un bat, al que yo le pase un fichero de texto, y me pregunte un valor numerico, y me elimine esos X caracteres de TODAS las lineas:


EJ 1:
TENGO:
ARCHIVOACAMBIAR.TXT
123456789
123456789copyright (c) 2001 Fabrice Bellard
123456789
123456789This file is part of FFmpeg.
123456789
123456789FFmpeg is free software; you can redistribute it and/or
123456789modify it under the terms of the GNU Lesser General Public
123456789License as published by the Free Software Foundation; either
123456789version 2.1 of the License, or (at your option) any later version.


//ESTE TEXTO NO LO KIERO, ES SOLO A MODO INFORMATIVO: (ME PREGUNTA NUMERO DE CARACTERES Y PARA ESTE EJEMPLO ELIJO "9")

LA SALIDA QUE QUIERO;
ARCHIVOCAMBIADO.TXT
copyright (c) 2001 Fabrice Bellard

This file is part of FFmpeg.

FFmpeg is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

//ESTE TEXTO NO LO KIERO, ES SOLO A MODO INFORMATIVO: (SE HAN ELIMINADO 9 CARACTERES DE CADA LINEA)

EJ 2:
TENGO:
ARCHIVOACAMBIAR.TXT
123456789012
123456789012copyright (c) 2001 Fabrice Bellard
123456789012
123456789012This file is part of FFmpeg.
123456789012
123456789012FFmpeg is free software; you can redistribute it and/or
123456789012modify it under the terms of the GNU Lesser General Public
123456789012License as published by the Free Software Foundation; either
123456789012version 2.1 of the License, or (at your option) any later version.

//ESTE TEXTO NO LO KIERO, ES SOLO A MODO INFORMATIVO: (ME PREGUNTA NUMERO DE CARACTERES Y PARA ESTE EJEMPLO ELIJO "12")

LA SALIDA QUE QUIERO;
ARCHIVOCAMBIADO.TXT
copyright (c) 2001 Fabrice Bellard

This file is part of FFmpeg.

FFmpeg is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

//ESTE TEXTO NO LO KIERO, ES SOLO A MODO INFORMATIVO: (SE HAN ELIMINADO 12 CARACTERES DE CADA LINEA)



Muchas gracias por adelantado
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

MS-Dos, Bat Simple?

Publicado por WiKe (2 intervenciones) el 16/11/2016 15:44:32
Como nadie responde, respondere yo mismo:

Eh creado este bat:

1
2
3
4
5
6
7
8
@setlocal enableextensions enabledelayedexpansion
@echo off
for /f "delims=" %%a in (avformat0.txt) do (
    set variable=%%a
	echo.!variable!
    set variable=!variable:~1!
     echo.!variable! >> avformat.txt
)


1.- segun eh visto, el " set variable=!variable:~1! " no funciona a menos que primero declaremos el " @setlocal enableextensions enabledelayedexpansion "

2.- al no poner ninguna ruta " for /f "delims=" %%a in (avformat0.txt) do ( " entiendo que el BAT deve estar en la misma carpeta que el archivo "avformat0.txt"

como estoy un poco verde con bats lo eh dejado así aunque se podrian mejorar 3 puntos;

3.- entiendo que el BAT deve estar en la misma carpeta que el archivo "avformat0.txt", aunque lo suyo seria que pudieras cojer el archivo "avformat0.txt" y arrastrarlo al BAT para que se ejecute sobre ese fichero

4.- he puesto: " set variable=!variable:~1! " aunque lo suyo, para lo que yo queria, seria que te preguntara el numero de caracteres a eliminar... y queria un control preciso, de esta manera me elimina 1 solo caracter, y si quiero eliminar mas, simplemente tengo que hacer mas pasadas

5.- he puesto que me genere un archivo nuevo, y ya, aunque al final podría copiar el NUEVO cn nombre ANTIGUO y borrar el ANTIGUO, para que de esta manera tengamos "el mismo" fichero
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Antoni Masana
Val: 1.419
Oro
Ha mantenido su posición en Ms-Dos (en relación al último mes)
Gráfica de Ms-Dos

MS-Dos, Bat Simple?

Publicado por Antoni Masana (811 intervenciones) el 17/11/2016 07:57:51
He realizado algunos cambios en tu BAT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
@echo off
 
setlocal enableextensions enabledelayedexpansion
 
Set Quita=%1
Set File=%2
 
if "%Quita%" == ""  goto Manual
if "%File%"  == ""  goto Manual
 
if %Quita%  GTR 20  goto Error_Num
if %Quita%  LSS  1  goto Error_Num
if not exist %File% goto Error_Fil
GOTO BEGIN
 
:MANUAL
   echo.
   set /p Quita=Columnas a eliminar...
   echo.
   if "%Quita%" == ""  goto :EOF
 
   set /p File=Fichero a tratar......
   echo.
   if "%File%"  == ""  goto :EOF
 
   if %Quita%  GTR 20  goto Error_Num
   if %Quita%  LSS  1  goto Error_Num
   if not exist %File% goto Error_Fil
   GOTO BEGIN
 
:BEGIN
   copy %File% %File%.bak >nul
   del  %File%
 
   for /f "delims=" %%a in (%File%.bak) do (
      set variable=%%a
 
      if %Quita% EQU  1  set variable=!variable:~1!
      if %Quita% ==   2  set variable=!variable:~2!
      if %Quita% EQU  3  set variable=!variable:~3!
      if %Quita% ==   4  set variable=!variable:~4!
      if %Quita% EQU  5  set variable=!variable:~5!
      if %Quita% ==   6  set variable=!variable:~6!
      if %Quita% EQU  7  set variable=!variable:~7!
      if %Quita% ==   8  set variable=!variable:~8!
      if %Quita% EQU  9  set variable=!variable:~9!
      if %Quita% ==  10  set variable=!variable:~10!
      if %Quita% EQU 11  set variable=!variable:~11!
      if %Quita% ==  12  set variable=!variable:~12!
      if %Quita% EQU 13  set variable=!variable:~13!
      if %Quita% ==  14  set variable=!variable:~14!
      if %Quita% EQU 15  set variable=!variable:~15!
      if %Quita% ==  16  set variable=!variable:~16!
      if %Quita% EQU 17  set variable=!variable:~17!
      if %Quita% ==  18  set variable=!variable:~18!
      if %Quita% EQU 19  set variable=!variable:~19!
      if %Quita% ==  20  set variable=!variable:~20!
 
      echo.!variable!>> %File%
   )
   GOTO:EOF
 
:Error_Fil
   echo.
   echo El fichero %File% no existe.
   GOTO Sintaxis
 
:Error_Num
   echo.
   echo El Numero de columnas (%Quita%) es mayor de 20.
   GOTO Sintaxis
 
:Sintaxis
   echo.
   echo.Sintaxis:  %0  num_col  File
   echo.
   GOTO:EOF

Cosas que hace:

- Se han de poner 2 parametros, el numero de columnas a quitar y el fichero origen.
- Verifica los parametros
- Ayuda de sintaxis.
- Realiza una copia del fichero añadiendo la extenssión .BAK
- Puedes quitar de 1 a 20 caracteres, para mas caracteres tendras que modificar la linea 9 y 54 y despúes añadir IF debajo de la 41. La opción de comparar puede ser por dos signos de igual " == " o " EQU ". por eso he puesto los dos.


COMENTARIOS:

El BAT puede estar en otra parte, por ejemplo en un directorio de utilidades y tenerlo en el PATH o llamarlo poniendo la ruta.

No es necesario estar en el directorio del fichero de texto.

1
C:\> Quita  12 C:\Ficheros\Fichero.txt

Lo de arrastrar el fichero no funciona.

Puedes utilizarlo abriendo una ventana de MS-DOS y ejecutar el BAT con los parámetros. o ejecutarlo sin parámetros y que te los pida

Saludos.
\\//_
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar