Jump to content

Recommended Posts

Posted
Hi all I would know how to extract robocopy one line or more lines in a file .txt or to look after certain characters such as TEST OK or something. Thanks in advance ..
Guest TheLibrarian
Posted

Assuming the robocopy log file is called robocopy.log

 

findstr /i /c:"TEST OK" robocopy.log

 

That would output all lines in the log file with the case insensitive string of TEST OK.

 

You can pipe that command to another file if needs be.

Posted

Not sure what you're doing, but I used to use robocopy to backup servers and the following subroutine was used to scan the logs (which could be massive!) to find the error line. It's called with the name of the log file to check; oFSO is the filesystemobject and it's writing a block of HTML in a variable called sSummary. This is then written to a web page that can be quickly checked to see if there are errors.

 

It starts by scanning backwards looking for the normal ending block (it has a row of dashes then the stats; this should happen less than 20 lines from the end of the file). If it doesn't find this then it knows that the backup failed to complete.

 

If robocopy completed, the script then keeps scanning the log looking for "ERROR: RETRY LIMIT EXCEEDED" - if it finds this then it writes the previous lines to the sSummary variable.

 

grep and findstr work well and they're quick; the downside is that they don't give you context (so you may know an error occurred but you don't know what it was). This was designed so that IT Support staff could quickly glance at a web page and see if there were any errors and what they were.

 


 sub ScanFile(sFile)
 	iLine = 0
 	set oScanFile = oFSO.OpenTextFile(sfile, 1)
   sText=oScanFile.readall
   aFileLines=split(sText,vbcrlf)
 	oScanFile.Close
 	iLine = Ubound(aFileLines)
 	sSummary=""
 	do while left(aFileLines(iLine),10)<>string(10,"-") and iFooter<20
 	 sSummary=aFileLines(iLine) & vbcrlf & sSummary
 	 iLine=iLine-1
 	 iFooter=iFooter+1
 	loop
 	if iFooter>=20 then
     iSlash=instrrev(sFile,"\")
     sJob=mid(sFile,iSlash+1,len(sFile)-iSlash-10)
     sSummary="Backup not completed"
     sSummary=sSummary & "
To re-run this backup, type \\student\adminscripts\back.wsf /job:" & sJob & ""
     sSummary=sSummary & " at a prompt on computer " & sBackupServer & ""
     iLine=Ubound(afileLines)
 	else
 	 	sSummary="" & sSummary & ""
 	end if
   ofile.writeline ""
 	For l=iLine to LBound(aFileLines) Step -1
   	if instr(aFileLines(l),"ERROR: RETRY LIMIT EXCEEDED.")>0 then
   	   oFile.writeLine aFileLines(l-3)
   	   oFile.writeLine aFileLines(l-2)
 '  	   oFile.writeLine aFileLines(l-1)
   	   oFile.writeLine aFileLines(l)
   	   l=l-4
   	end if
   	if instr(aFileLines(l)," ERROR ")<>0 then
     '  if isdate(left(aFileLines(l),8)) then
   	   oFile.writeLine aFileLines(l)
        oFile.writeLine aFileLines(l+1)

   '    end if
     end if
 	Next
 	oFile.write "" & sSummary
 end sub

  • Thanks 1
Posted

:( is not working .. this 2 post :( offff

i wanna extract one line or more lines in a file .txt and put in the another .txt file whit a robocopy.... EX:

 

in the a.txt is a line like "Text OK - is done", but not only ... there are several lines

 

and i wanna copy this line "Text OK - is done" and put in the b.txt file. in the another location...

 

:( sorry for my bad english .. i`m romanian guy ... THANKS in advence .. i`m w8ing replay ... :(

Posted

Not sure I understand. What has robocopy got to do with this? Are you just copying files with robocopy but you also want to check the contents of the file?

 

What I posted before was assuming you were running robocopy and getting a log fille - you then wanted to get something out of the log.

 

Could you try and describe a bit more what you're trying to do?

Posted

From my understanding of the op's request - he has a number of files (type unknown) that he wants to test for, extract and write to another file, a specific set of characters.

 

e.g. File A contains: Blah blah blah Test OK blah blah blah.

Procedure would extract "Test OK" and write it to File B

 

Although like you I am unsure of the exact purpose of such a project.

 

Again from my understanding of what Robocopy does, I don't think that this is the correct tool. It surely should be more of a search tool/script that would carry out such a procedure?

Posted

Sounds like grep then:

 

grep "Text OK" c:\foldername\*.* > c:\temp\results.txt

 

will search all files in c:\foldername and, if they contain "Text OK" will write their name to c:\temp\results.txt (it will do a bit more than that - you'll get the name of the file and the line containing the text. If you just want the file name you can use cut:

[code]
grep "Text OK" c:\foldername\*.* | cut -d : 1-2 c:\temp\results.txt

cut is saying that the text input to it is delimited with : and you want to keep fields 1 and 2 (so if you have a line like C:\folder1\folder2\file.txt:Text OK then you get c:\folder1\folder2\file.txt - field 1 is the "c"; field 2 is everything else up to the next :)

 

grep and cut are standard on Unix/Linux but are freely available for Windows - Browse UnxUtils Files on SourceForge.net

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...