Archivo bat que sustituye cadenas

Con lo que respecta al bat que sustituye cadenas como hacer para que éste reemplaze dentro de las cadenas de éstos archivos.

2 Respuestas

Respuesta

Sure, here's an example of a batch file that replaces a string in a text file:

@echo off setlocal enableextensions enabledelayedexpansion set "search=oldstring" set "replace=newstring" set "inputfile=input.txt" set "outputfile=output.txt" if exist "%outputfile%" del "%outputfile%" for /f "delims=" %%i in ('type "%inputfile%" ^& break ^> "%inputfile%"') do ( set "line=%%i" set "line=!line:%search%=%replace%!" echo(!line!>> "%outputfile%" ) echo "String replaced successfully"

In this example, you'll need to replace the "oldstring" and "newstring" values with the strings you want to replace and the strings you want to replace them with, respectively. You'll also need to replace the "input.txt" and "output.txt" values with the names of your input and output files, respectively.

The batch file works by reading each line of the input file, replacing the search string with the replace string using the "set" command and then writing the modified line to the output file. The "type" command is used to read the input file, and the "break" command is used to avoid a trailing blank line in the output file.

When you run the batch file, it will create a new file with the modified string. The original file will not be modified.

Note that this batch file will only replace the first occurrence of the search string on each line. If you need to replace all occurrences, you'll need to modify the batch file accordingly in frases motivacional.

Respuesta
-1
En principio conozco dos soluciones para lo que pides. La primera no tiene limitaciones conocidas en cuanto a los caracteres que puede sustituir, pero no funciona más allá de Windows XP de 32 bits (no funciona con Vista ni con Windows 7 porque utiliza el comando EDLIN que creo que ya no existe en esas versiones del sistema operativo). La segunda funciona en cualquier entorno CMD pero da problemas cuando la cadena a sustituir incluye un "=" o un "*" y no distingue mayúsculas y minúsculas.
Primera solución:
@echo off
if "%3" == "" goto ayuda
copy nul + nul esc.txt >nul 2>nul
for /f %%i in (esc.txt) do echo.1r%1%%i%2%%ie|edlin %3 >nul
del esc.txt
goto fin
:Ayuda
Echo Reemplaza una cadena por otra en todo el contenido de un archivo
echo Formato: %0 cadorig cadsust archivo
echo Ejemplo:
echo %0 de DE kk.txt
:Fin
Segunda solución:
@echo off
Setlocal EnableDelayedExpansion
del kkk.txt
if "%3" == "" goto ayuda
set cadorig=%1
set cadorig=%cadorig:"=%
set cadsust=%2
set cadsust=%cadsust:"=%
for /f "tokens=* delims=" %%i in (%3) do (set ANT=%%i&echo !ANT:%cadorig%=%cadsust%! >> kkk.txt)
copy /Y kkk.txt %3
goto fin
:ayuda
echo Reemplaza una cadena por otra en todo el contenido de un archivo
echo Formato: %0 cadorig cadsust archivo
echo Si las cadenas contienen espacios deben escribirse entrecomilladas
echo No funciona si la cadena original contiene un "=" o un "*"
Echo Ejemplo:
echo %0 de ppkktt kk.txt
:Fin
Mira a ver si alguna de ellas resuelve tu problema.
Saludos,
GGG
Hola Amigo el código funciona bien cuando son cadenas diferentes pero yo tengo el siguiente texto el cual es sólo una cadena y no funciona, lo único que quiero hacer es reemplazar todos los .000 por .00
.000
2.0 "PAGO EN PARCIALIDADES" "INGRESO" 15210 "1" 187252 "585" "06/06/2011" "30/05/2011" "45 DIAS FECHA FACTURA" "PAGO PARCIAL NO. 03" "QUE LIQUIDA LA FACTURA 143807" " DE FECHA 110606" "CON UN IMPORTE DE $ 0000008656764" "" "" "REICHHOLD QUIMICA DE MEXICO, S.A. DE C.V." "BOULEVARD ARTURO MONTIEL ROJAS" "10006" "" "PARQUE INDUSTRIAL ATLACOMULCO" "ATLACOMULCO" "ESTADO DE MEXICO," "MEXICO" "50458" "RQM6102086F8" "01 (712) 122 95 00" "01 (712) 122 11 70" "PLANTA ATLACOMULCO" "BOULEVARD ARTURO MONTIEL ROJAS" "10006" "" "PARQUE INDUSTRIAL ATLACOMULCO" "ATLACOMULCO" "ESTADO DE MEXICO," "MEXICO" "50458" "01 (712) 122 95 00" "01 (712) 122 11 70" "MEADE ALONSO JORGE MARTIN" "PERIFERICO PONIENTE" "755" "" "EL MEXQUITAL" "JARAL PAISANOS" "SAN LUIS POTOSI" "MEXICO" "78166" "MEAJ630406MHA" 40110 "MEADE ALONSO JORGE MARTIN" "PERIFERICO PONIENTE" "755" "" "EL MEZQUITAL" "" "SAN LUIS POTOSI" "MEXICO" "" "" "PAGO" "" .00 1.00 1.00 25198.84 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .000 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" .00 "" .00 "" .00 "" .00 "" 21723.13 "IVA" 16.00 3475.71 "" .00 .00 25198.84 "VEINTICINCO MIL CIENTO NOVENTA Y OCHO PESOS*********84/100 *M.N. ***************" 1.00 "PES" .000 .000
Muchas gracias
Efectivamente la primera solución no funciona en este caso. No acabo de entenderlo y lo voy a estudiar a ver si averiguo la razón.
Pero he probado la segunda solución en el caso de tu ejemplo y parece funcionar bien.
Copio y pego la ejecución en mi PC:
d:\bat>remplaza1 .000 .00 modelo.txt
1 archivos copiados.
d:\bat>type modelo.txt
.000
2.0 "PAGO EN PARCIALIDADES" "INGRESO" 15210 "1" 187252 "585" "06/06/2011" "30/05
/2011" "45 DIAS FECHA FACTURA" "PAGO PARCIAL NO. 03" "QUE LIQUIDA LA FACTURA 143
807" " DE FECHA 110606" "CON UN IMPORTE DE $ 0000008656764" "" "" "REICHHOLD QUI
MICA DE MEXICO, S.A. DE C.V." "BOULEVARD ARTURO MONTIEL ROJAS" "10006" "" "PARQU
E INDUSTRIAL ATLACOMULCO" "ATLACOMULCO" "ESTADO DE MEXICO," "MEXICO" "50458" "RQ
M6102086F8" "01 (712) 122 95 00" "01 (712) 122 11 70" "PLANTA ATLACOMULCO" "BOUL
EVARD ARTURO MONTIEL ROJAS" "10006" "" "PARQUE INDUSTRIAL ATLACOMULCO" "ATLACOMU
LCO" "ESTADO DE MEXICO," "MEXICO" "50458" "01 (712) 122 95 00" "01 (712) 122 11
70" "MEADE ALONSO JORGE MARTIN" "PERIFERICO PONIENTE" "755" "" "EL MEXQUITAL" "J
ARAL PAISANOS" "SAN LUIS POTOSI" "MEXICO" "78166" "MEAJ630406MHA" 40110 "MEADE A
LONSO JORGE MARTIN" "PERIFERICO PONIENTE" "755" "" "EL MEZQUITAL" "" "SAN LUIS P
OTOSI" "MEXICO" "" "" "PAGO" "" .00 1.00 1.00 25198.84 "" "" "" .00 "" "" "" ""
"" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" ""
"" .00 "" "" "" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00
"" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" ""
"" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "
" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" ""
.00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "
" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" .00 .00 .00 .
00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" ""
"" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" .00 .00 .00 .00 "" "" "" .
00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 ""
"" "" "" "" "" "" .00 "" "" "" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "
" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" ""
"" "" .00 "" "" "" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .0
0 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .000 "" "" "" "" "" "" "" .00 ""
"" "" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "
" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" ""
"" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .0
0 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" .00 "" .00 "" .0
0 "" .00 "" 21723.13 "IVA" 16.00 3475.71 "" .00 .00 25198.84 "VEINTICINCO MIL CI
ENTO NOVENTA Y OCHO PESOS*********84/100 *M.N. ***************" 1.00 "PES" .000
.000
d:\bat>remplaza1 .000 .00 modelo.txt
1 archivos copiados.
d:\bat>type modelo.txt
.00
2.0 "PAGO EN PARCIALIDADES" "INGRESO" 15210 "1" 187252 "585" "06/06/2011" "30/05
/2011" "45 DIAS FECHA FACTURA" "PAGO PARCIAL NO. 03" "QUE LIQUIDA LA FACTURA 143
807" " DE FECHA 110606" "CON UN IMPORTE DE $ 0000008656764" "" "" "REICHHOLD QUI
MICA DE MEXICO, S.A. DE C.V." "BOULEVARD ARTURO MONTIEL ROJAS" "10006" "" "PARQU
E INDUSTRIAL ATLACOMULCO" "ATLACOMULCO" "ESTADO DE MEXICO," "MEXICO" "50458" "RQ
M6102086F8" "01 (712) 122 95 00" "01 (712) 122 11 70" "PLANTA ATLACOMULCO" "BOUL
EVARD ARTURO MONTIEL ROJAS" "10006" "" "PARQUE INDUSTRIAL ATLACOMULCO" "ATLACOMU
LCO" "ESTADO DE MEXICO," "MEXICO" "50458" "01 (712) 122 95 00" "01 (712) 122 11
70" "MEADE ALONSO JORGE MARTIN" "PERIFERICO PONIENTE" "755" "" "EL MEXQUITAL" "J
ARAL PAISANOS" "SAN LUIS POTOSI" "MEXICO" "78166" "MEAJ630406MHA" 40110 "MEADE A
LONSO JORGE MARTIN" "PERIFERICO PONIENTE" "755" "" "EL MEZQUITAL" "" "SAN LUIS P
OTOSI" "MEXICO" "" "" "PAGO" "" .00 1.00 1.00 25198.84 "" "" "" .00 "" "" "" ""
"" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" ""
"" .00 "" "" "" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00
"" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" ""
"" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "
" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" ""
.00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "
" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" .00 .00 .00 .
00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" ""
"" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" .00 .00 .00 .00 "" "" "" .
00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 ""
"" "" "" "" "" "" .00 "" "" "" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "
" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" ""
"" "" .00 "" "" "" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .0
0 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 ""
"" "" "" "" "" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" ""
"" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "
" .00 .00 .00 .00 "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00
"" "" "" "" "" "" "" .00 "" "" "" "" "" "" "" .00 "" "" "" "" .00 "" .00 "" .00
"" .00 "" 21723.13 "IVA" 16.00 3475.71 "" .00 .00 25198.84 "VEINTICINCO MIL CIE
NTO NOVENTA Y OCHO PESOS*********84/100 *M.N. ***************" 1.00 "PES" .00 .0
0
Según puede verse ha reemplazado las cuatro ocurrencias de ".000" del archivo original por ".00"
Mira a ver si consigues lo mismo.
Saludos,
GGG
Ya he visto por qué no funciona la primera solución: es que el comando EDLIN de MSDOS en el que se basa el truco no trata líneas mayores de 253 caracteres y la segunda línea de tu archivo tiene muchos más (tiene 2564 caracteres). Por eso solo hace la primera sustitución, la de la primera línea, que cumple la limitación.
Saludos,
GGG
OK amigo muchas gracias, crees que exista la posibilidad de editar la capacidad del EDLIN para poder hacer lo que quiero?
No. El EDLIN es un producto muy antiguo que no tiene posibilidades, que yo sepa, de cambiar sus limitaciones.
Pero, como te decía, la otra solución funciona y, en el caso de tus archivos no debería dar problema puesto que parece que no se van a dar las situaciones que hacen que no funcione correctamente.
Prueba con esa solución y si tienes algún problema me lo comentas.
Saludos,
GGG
Veo que ha pasado bastante tiempo sin que hayas dicho nada. ¿Conseguiste resolver tu problema o ha dejado de interesarte el asunto?
Saludos,
GGG

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas