Título: Ayuda para comparar dos archivos txt y extraer el contenido de uno de ellos??
Publicado por: valeeen en 14 Julio 2015, 16:41 pm
Hola amigos!!! tengo un problemilla... tenia este script en vbs que me servia para extraer el hostname de un equipo y meterlo como una variable en WinPE Function UserExit(sType, sWhen, sDetail, bSkip) oLogging.CreateEntry "entered UserExit ", LogTypeInfo UserExit = Success End Function
Function GetOfflineComputername() On Error Goto 0
If oEnvironment.Item("OSVERSION")="WinPE" Then Dim CompName : CompName = "" Dim ret, sOldSystem : sOldSystem = "" For Each drv In Array("C", "D", "E", "F") If ofso.FileExists(drv & ":\windows\system32\config\system") Then sOldSystem = drv & ":\windows\system32\config\system" Exit For End If Next oLogging.CreateEntry "Mounting Offline registry " & sOldSystem, LogTypeInfo ret = oShell.Run ("reg load HKLM\z " & sOldSystem, 0, True) If ret = 0 Then CompName = oShell.RegRead("HKLM\z\ControlSet001\Services\Tcpip\Parameters\Hostname") If CompName <> "" Then oLogging.CreateEntry "Found old computername '" & CompName & "'", LogTypeInfo Else oLogging.CreateEntry "Old computername name could not be found", LogTypeWarning End If Else oLogging.CreateEntry "Could not mount offline registry " & sOldSystem, LogTypeWarning End If oShell.Run "REG UNLOAD HKLM\Z", 0, True Else CompName = oShell.ExpandEnvironmentStrings("%Computername%") End If
GetOfflineComputername = CStr(CompName) End Function
Este me funciona perfectamente, peeeeero ahora necesito hacer lo siguiente... Tengo un numero o nombre en un txt, solo ese número (ejemplo: AAAA0001) En otro archivo txt tengo el siguiente esquema: AAAA0001#BBBB0001#CCCC0001#DDDD0001 EEEE0001#FFFF0001#GGGG0001#HHHH0001 Así muchas lineas... tengo que comprobar en que linea esta el contenido del primer TXT y después quedarme SOLO con el segundo valor (BBBB0001) y guardarlo en una variable (o meterlo en el script de arriba de alguna manera fijo).... Hasta ahora lo que he conseguido es esto: dim oshell dim fich, fs, fich2, ncx, linea
'ubicación de ficheros nombre_fich="c:\fichero1.txt" nombre_fich2="c:\fichero2.txt"
set oshell=createobject("WScript.shell") set fs=createobject("scripting.FileSystemObject")
'comprobamos que existan los ficheros if not fs.FileExists(nombre_fich) then wscript.echo "no existe el fichero "& nombre_fich WScript.Quit 4 end if
if not fs.FileExists(nombre_fich2) then wscript.echo "no existe el fichero "& nombre_fich2 WScript.Quit 4 end if
'Si hemos llegado hasta aquí es que existe y lo abrimos set fich=fs.OpenTextFile (nombre_fich, 1, "True" )
' Almacenamos en la variable linea el contenido de fichero1 Do While fich.atEndOfStream <> True linea=fich.ReadLine ' Ahora mostramos la línea leída wscript.echo linea Loop
set fich2=fs.OpenTextFile (nombre_fich2, 1, "True" )
Do While fich2.atEndOfStream <> True linea2=fich2.ReadLine ' Ahora mostramos la línea leída wscript.echo linea2 ' Parseamos los campos campos = split(linea2,"#") for each b in campos wscript.echo "valor del campo: "&b next Loop
wscript.echo linea wscript.echo linea2
fich.close
Vale, consigo imprimir la linea del fichero1 y ademas imprimir todo el fichero2, pero me he quedado estancado jeje. Me ayudáis?? Muchas gracias!! Un saludo
Título: Re: Ayuda para comparar dos archivos txt y extraer el contenido de uno de ellos??
Publicado por: pkj en 14 Julio 2015, 17:50 pm
Supongo que solo te falta añadir una linea para conseguir el valor que buscas. Algo así en esta zona: Do While fich2.atEndOfStream <> True linea2=fich2.ReadLine ' Ahora mostramos la línea leída wscript.echo linea2 ' Parseamos los campos campos = split(linea2,"#") for each b in campos wscript.echo "valor del campo: "&b next if campos(0)=linea then valorbuscado=campos(1): exit do Loop wscript.echo linea wscript.echo linea2 wscript.echo valorbuscado
Saludos
Título: Re: Ayuda para comparar dos archivos txt y extraer el contenido de uno de ellos??
Publicado por: valeeen en 14 Julio 2015, 18:06 pm
Supongo que solo te falta añadir una linea para conseguir el valor que buscas. Algo así en esta zona: Do While fich2.atEndOfStream <> True linea2=fich2.ReadLine ' Ahora mostramos la línea leída wscript.echo linea2 ' Parseamos los campos campos = split(linea2,"#") for each b in campos wscript.echo "valor del campo: "&b next if campos(0)=linea then valorbuscado=campos(1): exit do Loop wscript.echo linea wscript.echo linea2 wscript.echo valorbuscado
Saludos Gracias pkj, te refieres a esta no? if campos(0)=linea then valorbuscado=campos(1): exit do
para cambiar la función del primer script, y ajustarlo con lo del segundo seria algo así? Function UserExit(sType, sWhen, sDetail, bSkip) oLogging.CreateEntry "entered UserExit ", LogTypeInfo UserExit = Success End Function
Function GetOfflineComputername() On Error Goto 0
If oEnvironment.Item("OSVERSION")="WinPE" Then Dim CompName : CompName = "" Dim oshell Dim fich, fs, fich2, ncx, linea 'ubicación de ficheros nombre_fich="c:\fichero1.txt" nombre_fich2="c:\fichero2.txt"
set oshell=createobject("WScript.shell") set fs=createobject("scripting.FileSystemObject")
'comprobamos que existan los ficheros if not fs.FileExists(nombre_fich) then wscript.echo "no existe el fichero "& nombre_fich WScript.Quit 4 end if
if not fs.FileExists(nombre_fich2) then wscript.echo "no existe el fichero "& nombre_fich2 WScript.Quit 4 end if
'Si hemos llegado hasta aquí es que existe y lo abrimos set fich=fs.OpenTextFile (nombre_fich, 1, "True" )
' Almacenamos en la variable linea el contenido de fichero1 Do While fich.atEndOfStream <> True linea=fich.ReadLine Loop
set fich2=fs.OpenTextFile (nombre_fich2, 1, "True" )
Do While fich2.atEndOfStream <> True linea2=fich2.ReadLine
' Parseamos los campos campos = split(linea2,"#") for each b in campos next if campos(0)=linea then valorbuscado=campos(1): exit do Loop
fich.close
CompName = valorbuscado
GetOfflineComputername = CStr(CompName) End Function
He quitado los echo porque no me interesa para el script, pero si para comprobar que funciona fuera jeje. Muchas gracias por la ayuda!
|