![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Log name of file that can't be moved to a different directory First of all, let me thank you in advance for your help. I have a script that finds all of the .doc files in a folder, and moves them to a different folder (working perfectly). However, if one of the .doc files is opened (locked), it cannot be moved. I need the script to not crash (as it runs over and over throughout the day automatically with little to no supervision), and log the name of the file that it cannot move. Right now, I have it logging the time/date and the error number encountered, but cannot figure out how to get it to log the name of the file (my script listed below). If I can get this to work, the next step will be to get the scipt to attempt moving this file and logging the error only a specific number of times (i.e. 15), and then stop attempting to move it, as to not load the log up with the same error message hundreds-thousands of times. If anybody can help with EITHER of these procedures, it would be greatly appreciated. Here's my script so far: Option Explicit Dim objFSO2, objFile, fso, FileSet, Path, File, DestPath Const ForAppending = 8 Set objFSO2 = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO2.OpenTextFile("c:\website\log.txt", ForAppending) Call MoveLabels objFile.Close '################################################# ' Move Labels '################################################# Sub MoveLabels Err.Clear On Error Resume Next Path = "C:\Website\" DestPath = "C:\Website\Folder\" FileSet = GetDirContents(Path) For each File in FileSet Set File = fso.GetFile(Path & "\" & File) If File.Type = "Microsoft Word Document" Then File.Move DestPath End If Next ' E R R O R H A N D L I N G If Err.Number <> 0 Then objFile.WriteLine Now objFile.WriteLine Err.Number objFile.WriteBlankLines (1) End if End Sub '################################################# ' Function '################################################# Function GetDirContents(FolderPath) Dim FileCollection, aTmp(), i Set fso = CreateObject("Scripting.FileSystemObject") Set FileCollection = fso.GetFolder(FolderPath).Files Redim aTmp(FileCollection.count - 1) i = -1 For Each File in FileCollection i = i + 1 aTmp(i) = File.Name Next |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Log name of file that can't be moved to a different directory On Jul 22, 10:02*am, Robert Jacobs <robertjacob...@xxxxxx> wrote: Quote: > First of all, let me thank you in advance for your help. > > I have a script that finds all of the .doc files in a folder, and > moves them to a different folder (working perfectly). *However, if one > of the .doc files is opened (locked), it cannot be moved. *I need the > script to not crash (as it runs over and over throughout the day > automatically with little to no supervision), and log the name of the > file that it cannot move. *Right now, I have it logging the time/date > and the error number encountered, but cannot figure out how to get it > to log the name of the file (my script listed below). > > If I can get this to work, the next step will be to get the scipt to > attempt moving this file and logging the error only a specific number > of times (i.e. 15), and then stop attempting to move it, as to not > load the log up with the same error message hundreds-thousands of > times. > > If anybody can help with EITHER of these procedures, it would be > greatly appreciated. *Here's my script so far: > > Option Explicit > Dim objFSO2, objFile, fso, FileSet, Path, File, DestPath > Const ForAppending = 8 > > Set objFSO2 = CreateObject("Scripting.FileSystemObject") > Set objFile = objFSO2.OpenTextFile("c:\website\log.txt", ForAppending) > > Call MoveLabels > objFile.Close > I think you're working too hard. I see no need to make a list of the file's names and paths and then re-instantiate the file object from that list. If you were renaming files, that would be a good idea, but here I believe something like this will do what you want ... Option Explicit Dim objFSO, objLogFile, sPath, sDestPath Const ForAppending = 8 sSrcPath = "C:\Website\" sDestPath = "C:\Website\Folder\" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objLogFile = objFSO.OpenTextFile(sSrcPath & "log.txt", _ ForAppending) MoveLabels objFSO, objLogFile, sSrcPath, sDestPath objLogFile.Close '################################################# ' Move Labels '################################################# Sub MoveLabels(fso, oLog, sSrc, SDest) Dim oFile For each oFile in fso.GetFolder(sSrc).Files If oFile.Type = "Microsoft Word Document" Then On Error Resume Next oFile.Move sDest ' E R R O R H A N D L I N G If Err.Number <> 0 Then oLog.WriteLine Now oLog.WriteLine Err.Number & ", " & Err.Description oLog.WriteLine oFile.Path ' or oFile.Name oLog.WriteBlankLines (1) End if On Error Goto 0 End If Next End Sub Note that I passed the arguments to the MoveLabels subroutine explicitly, rather relying on global variables. I believe this is a better way to use subroutines. It makes its needs more evident and will assist in later maintenance. Tom Lavedas *********** |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| User directory has to be moved from c drive | Vista General | |||
| Moved Contacts folder to Windows Directory and cant move it back | Vista file management | |||
| Setting up a directory and moving a file into that directory from | Vista General | |||
| Setting up a directory and moving a file into that directory from | Vista General | |||
| File Manager doesn't display WHAT file is being copied/moved!?!?!?! | Vista file management | |||