Solved

How do i read a file path and file name from a text log and delete that file

Posted on 2011-02-25
13
281 Views
Last Modified: 2013-11-05
Hi Experts,

I have been using the Office File Converter to convert some of our older 2003 documents to the new Office 2010 document formats. The problem is that the conversion tool does not replace the file but saves a copy of the updated version of the file.

As the conversion tool is not saving logs anymore i have piped out the text on the screen.

I would like a script that will check to see if the string line starts with "Converting: " signalling the conversion was sucessfull and to capture the string after it all the way till a .doc .xls .ppt and to delete the file.

The the goal is to delete the original file from the file path in the log :)

Is this at all possible?
I have attached a sample log file.

Thanks in advance
Converting: \\Server-ntf01\e$\Shared\SHARED\Test convert\Test2\Minutes_Meetings\Team Meeting\Team Meetings Minutes 2010\Minutes_20101214.doc Writing converted file to: \\Server-ntf01\e$\Shared\SHARED\Test convert\Test2\Minutes_Meetings\Team Meeting\Team Meetings Minutes 2010\Minutes_20101214.docx
Converting: \\Server-ntf01\e$\Shared\SHARED\Test convert\Test2\Minutes_Meetings\Team Meeting\Team Meetings Minutes 2010\Minutes_20101221.doc Writing converted file to: \\Server-ntf01\e$\Shared\SHARED\Test convert\Test2\Minutes_Meetings\Team Meeting\Team Meetings Minutes 2010\Minutes_20101221.docx

Open in new window

log.txt
0
Comment
Question by:Archerial
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
13 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 34984872
Try this for starters.  Amend the log file to point to your output. It first of all uses a for loop to read

each full line that contains "Converting: " into %full% and then removes the word "Converting: " and replaces

the "Writing converted file to: " to a / character (we want something not in the filename and it shouldn't be..).  It then passes that into another for loop to split the line at the / character and leaves the filename in %%b which it then passes to a Del command.  Remove the word echo if you are happy it looks OK!

Steve

@echo off
setlocal enabledelayedexpansion
set log="2.txt"
for /f "tokens=*" %%a in ('find "Converting: " ^<%log%') do (
  set full=%%a
  set full=!full:Converting: =!
  set full=!full: Writing converted file to: =/!
  for /f "tokens=1 delims=/" %%b in ('echo !full!') do (
    echo del /Y "%%~b"
  )
)
0
 
LVL 56

Accepted Solution

by:
Bill Prew earned 450 total points
ID: 34985362
Steve,

That's a great little BAT file, nice.  When I tested it on the sample log file though it stumbled on a few of the lines, because they have some special characters in the file names that BAT files have trouble with, like &.

As a result I put together a quick VB script approach that I think should work all the time.  Save the following as a VBS file, and then run like this:

cscript EE26847036.vbs log.txt

' Make sure the input file name is specified on the command line and get it
If (WScript.Arguments.Count > 0) Then
  sInFile = WScript.Arguments(0)
Else
  WScript.Echo "No input filename specified."
  WScript.Quit
End If

' Read entire log file into a single string variable
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oInFile = oFSO.OpenTextFile(sInFile)
sData = oInFile.ReadAll
oInFile.Close
Set oInFile = Nothing

' Create regular expression template to locate converted files
Set oRegex = New RegExp
oRegex.Pattern = "Converting: (.+) Writing converted file to: "
oRegex.IgnoreCase = True
oRegex.Global = True

' Loop through each match, delete the file
for Each oMatch In oRegex.Execute(sData)
   oFSO.DeleteFile(oMatch.Submatches(0))
Next

Open in new window

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 34986081
bizarre, hadnt seen the log.txt attachment before last night, just the two lin example which of course worked fine... Agreed vbscript less likely to stumble when there is special chars involved!

Steve
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 50 total points
ID: 34986216
If you do want to go batch I think this should deal with any & characters at least by parsing them and escaping them to ^&.... though can't test it right at the mo.   If you have used any other special chars then probably bst to forget batch!

Steve

@echo off
setlocal enabledelayedexpansion
set log="2.txt"
for /f "tokens=*" %%a in ('find "Converting: " ^<%log%') do (
  set full="%%a"
  set full=!full:^&=^^^&!
  set full=!full:Converting: =!
  set full=!full: Writing converted file to: =/!
  for /f "tokens=1 delims=/" %%b in ('echo !full!') do (
    echo del /Y "%%~b"
  )
)
0
 

Author Comment

by:Archerial
ID: 34993608
Hi Thanks,

I test ran the .vbs option by billprew, it worked but stopped at an error and wont progress any futher. Error message when the script ran
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 34993789
Can you post your VBS code, since my original post didn't have a line 28, where you seem to be getting there error.

~bp
0
 

Author Comment

by:Archerial
ID: 34993884
Just a comment addded at the top and some line spacing.
'This script will read a log from the Office Conversion tool and delete the orginal files.



' Make sure the input file name is specified on the command line and get it
If (WScript.Arguments.Count > 0) Then
  sInFile = WScript.Arguments(0)
Else
  WScript.Echo "No input filename specified."
  WScript.Quit
End If

' Read entire log file into a single string variable
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oInFile = oFSO.OpenTextFile(sInFile)
sData = oInFile.ReadAll
oInFile.Close
Set oInFile = Nothing

' Create regular expression template to locate converted files
Set oRegex = New RegExp
oRegex.Pattern = "Converting: (.+) Writing converted file to: "
oRegex.IgnoreCase = True
oRegex.Global = True

' Loop through each match, delete the file
for Each oMatch In oRegex.Execute(sData)
   oFSO.DeleteFile(oMatch.Submatches(0))
Next

Open in new window

0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 34993904
Let's try and see what file it is trying to delete by adding some display logic.

for Each oMatch In oRegex.Execute(sData)
   Wscript.Echo oMatch.Submatches(0)
   oFSO.DeleteFile(oMatch.Submatches(0))
Next

~bp
0
 

Author Comment

by:Archerial
ID: 34994126
Hmm , im not sure why this is the case,

Is it possible to log the failed deleted file into file so they can be treated later?
Error--Script---msgbox.JPG
0
 
LVL 56

Assisted Solution

by:Bill Prew
Bill Prew earned 450 total points
ID: 34994217
You could do something like this:

for Each oMatch In oRegex.Execute(sData)
   If oFSO.FileExists(oMatch.Submatches(0)) Then
      oFSO.DeleteFile(oMatch.Submatches(0))
   End If
Next

~bp
0
 

Author Comment

by:Archerial
ID: 34994391
Thank you it works, gets a bit stuck on read only files but appart from that does as expected.


0
 

Author Closing Comment

by:Archerial
ID: 34994403
Thank you both for your assistance.

The script does as expected, i part credited steve as he did respond with a batch script as requested but as i didnt know the limitations of batch files :S
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 34995997
Thanks, no problem - VBScript being proper programming language is always more controllable when special characters are involved asyou can see!

Steve
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question