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

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
ArcherialAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Bill PrewConnect With a Mentor Commented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
Steve KnightIT ConsultancyCommented:
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
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
Steve KnightConnect With a Mentor IT ConsultancyCommented:
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
 
ArcherialAuthor Commented:
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
 
Bill PrewCommented:
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
 
ArcherialAuthor Commented:
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
 
Bill PrewCommented:
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
 
ArcherialAuthor Commented:
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
 
Bill PrewConnect With a Mentor Commented:
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
 
ArcherialAuthor Commented:
Thank you it works, gets a bit stuck on read only files but appart from that does as expected.


0
 
ArcherialAuthor Commented:
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
 
Steve KnightIT ConsultancyCommented:
Thanks, no problem - VBScript being proper programming language is always more controllable when special characters are involved asyou can see!

Steve
0
All Courses

From novice to tech pro — start learning today.