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
277 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
  • 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 51

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
 
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 51

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 51

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 51

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now