Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
284 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 59

Accepted Solution

by:
Bill Prew earned 1800 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 200 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 59

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 59

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 59

Assisted Solution

by:Bill Prew
Bill Prew earned 1800 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

782 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