Archivo bat que sustituye cadenas
2 Respuestas
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.
- Compartir respuesta
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
.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
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
Saludos,
GGG
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
- Compartir respuesta
Hola, "Alex star". Supongo que entiendes bien el español porque veo que has contestado a tres preguntas muy distintas formuladas en español. No acabo de entender que sentido tiene contestar a unas preguntas muy antiguas, pero en el caso de esta, en concreto, tu solución no vale. Solo tienes que intentar aplicarla en el contexto indicado por la persona que la formuló. De todas forma, bienvenido. - gggirald