Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Debugging Help Please! :) File renaming...

Reply
 
Old 09-13-2008   #1 (permalink)
onebucktraffic


 
 

Debugging Help Please! :) File renaming...

Thanks for reading this.

This code segment is part of a larger script that moves PST files so
that they are accessible to users anywhere in the network. The rest of
the script works but this is making me nuts. This is supposed to
determine if two or more files have the same name, and if so, rename
them to include the file dat or timestamp as well. It doesn't work.
Can some offer some guidance?

DI



'[Rename Code] - Renames PST files with timestamp
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root
\cimv2")

Set FileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='y:\mailbox'} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile In FileList
strDate = Left(objFile.FileName & "_" & objFile.CreationDate, 30)
strNewName = objFile.Drive & objFile.Path & _
strDate & "." & "pst"
strNameCheck = Replace(strNewName, "\", "\\")

i = 1
Do While True
Set colFiles = objWMIService.ExecQuery _
("Select * from Cim_Datafile Where Name = '" &
strNameCheck & "'")
If colFiles.Count = 0 Then
errResult = objFile.Rename(strNewName)
Exit Do
Else
i = i + 1
strNewName = objFile.Drive & objFile.Path & _
strDate & "_" & i & "." & "pst"
strNameCheck = Replace(strNewName, "\", "\\")
End If
Loop
Next
'[End Rename Code]

My System SpecsSystem Spec
Old 09-13-2008   #2 (permalink)
Pegasus \(MVP\)


 
 

Re: Debugging Help Please! :) File renaming...


<onebucktraffic@xxxxxx> wrote in message
news:5e1841f9-2082-4583-8b4f-3930157b6bf4@xxxxxx
Quote:

> Thanks for reading this.
>
> This code segment is part of a larger script that moves PST files so
> that they are accessible to users anywhere in the network. The rest of
> the script works but this is making me nuts. This is supposed to
> determine if two or more files have the same name, and if so, rename
> them to include the file dat or timestamp as well. It doesn't work.
> Can some offer some guidance?
>
> DI
How can you have two files of the same name in the same folder?


My System SpecsSystem Spec
Old 09-13-2008   #3 (permalink)
Dakota Interactive


 
 

Re: Debugging Help Please! :) File renaming...

On Sep 13, 2:12*pm, "Pegasus \(MVP\)" <I....@xxxxxx> wrote:
Quote:

> <onebucktraf...@xxxxxx> wrote in message
>
> news:5e1841f9-2082-4583-8b4f-3930157b6bf4@xxxxxx
>
Quote:

> > Thanks for reading this.
>
Quote:

> > This code segment is part of a larger script that moves PST files so
> > that they are accessible to users anywhere in the network. The rest of
> > the script works but this is making me nuts. This is supposed to
> > determine if two or more files have the same name, and if so, rename
> > them to include the file dat or timestamp as well. It doesn't work.
> > Can some offer some guidance?
>
Quote:

> > DI
>
> How can you have two files of the same name in the same folder?
They do not originate from the same folder, but they will end up in
the same folder and therefore need different names.
My System SpecsSystem Spec
Old 09-13-2008   #4 (permalink)
Pegasus \(MVP\)


 
 

Re: Debugging Help Please! :) File renaming...


"Dakota Interactive" <onebucktraffic@xxxxxx> wrote in message
news:74ae843c-3d34-4cb6-8074-5d8ddbd0da9c@xxxxxx
On Sep 13, 2:12 pm, "Pegasus \(MVP\)" <I....@xxxxxx> wrote:
Quote:

> <onebucktraf...@xxxxxx> wrote in message
>
> news:5e1841f9-2082-4583-8b4f-3930157b6bf4@xxxxxx
>
Quote:

> > Thanks for reading this.
>
Quote:

> > This code segment is part of a larger script that moves PST files so
> > that they are accessible to users anywhere in the network. The rest of
> > the script works but this is making me nuts. This is supposed to
> > determine if two or more files have the same name, and if so, rename
> > them to include the file dat or timestamp as well. It doesn't work.
> > Can some offer some guidance?
>
Quote:

> > DI
>
> How can you have two files of the same name in the same folder?
They do not originate from the same folder, but they will end up in
the same folder and therefore need different names.
===========
This is what I suspected but so far I am unable to see the flow of your
logic. You wrote "If two or more files have the same name". This implies
that you must perform a comparison, then take appropriate action if two
names are the same. I am unable to see any code in your script that performs
this file name comparison.


My System SpecsSystem Spec
Old 09-13-2008   #5 (permalink)
Dakota Interactive


 
 

Re: Debugging Help Please! :) File renaming...

My apologies... here is the entire thing, fully commented.

'[Comments]
'Moves existing PST files to new proper location - C:\Documents and
Settings\%username%\mailbox\
'Renames PST files w/ file_timemstamp.pst to prevent duplicate name
issues

'[Start Script]
Dim objFSO, objFolder, objShell, colFiles, objSubFolder, objSubFile,
fso, strDirectory, wshShell, objWMIService

Set fso = CreateObject("Scripting.FileSystemObject")
Set WshNetwork = WScript.CreateObject("Wscript.Network")
Set wshShell = WScript.CreateObject("WScript.Shell")

strUserID = WSHNetwork.UserName

'[Local Map] - Used to trick the WMI call to see a %username% call as
a physical directory
If fso.DriveExists("Y:") Then
WshNetwork.RemoveNetworkDrive "Y:",True,True
End If

WSHSHell.Run "subst Y: /D"
WSHSHell.Run "subst Y: ""C:\Documents and Settings\%username%"""
'[End Local Map]

'[Definition paths]
strpath = "C:\Documents and Settings\"
strpath2 = "\mailbox\"
strpath3 = "\\fs2\Documents\"
strpath4 = "\My Documents\"

strfinalpath = strpath & strUserID
'C:\Documents and Settings\%username%\
strfinalpath2 = strpath & strUserID & strpath2
'C:\Documents and Settings\%username%\mailbox\
strfinalpath3 = strpath3 & strUserID & strpath4
'\\fs2\Documents\%username%\My Documents\

strFolder = strfinalpath
strTar = strfinalpath2
strFolder2 = strfinalpath3
strTar2 = strfinalpath2
'[End definition paths]

'[Folder Creation]
strDirectory = strfinalpath + "\mailbox"
Set objFSO = CreateObject("Scripting.FileSystemObject")
'[End Folder Creation]

'[Start Folder/Content Confirmation] - Confirms if \mailbox has been
generated already w/ PST files
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
End If

If err.number = vbEmpty then
Set objShell = CreateObject("WScript.Shell")
Else WScript.echo "VBScript Error: " & err.number
End If

Set confirmation = fso.getfolder(strfinalpath2)
For Each File in confirmation.Files
If fso.GetExtensionName(File)="pst" Then
WSHSHell.Run "subst Y: /D"
wscript.quit
End If
Next
'[End Confirmation Code]

'[Start First Move Code] - Moves files from %username% First
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)
Set colFiles = objFolder.Files

For Each File in colFiles
set objFile = objFSO.GetFile(strFolder & "\" & File.Name)
Next

If lcase(Right(objFile.Name,3)) = "pst" Then
objFSO.MoveFile objFile, strTar & "N1-" & objfile.name
'objFSO.DeleteFile objFile, true
End If

ScanSubFolders(objFolder)

Sub ScanSubFolders(objFolder)

Set colFolders = objFolder.SubFolders
For Each objSubFolder In colFolders

Set colFiles = objSubFolder.Files
For Each objFile in Colfiles
If lcase(Right(objFile.Name,3)) = "pst" Then
objFSO.MoveFile objFile, strTar & "N2-" & objfile.name
'objFSO.DeleteFile objFile, true
End If
Next
ScanSubFolders(objSubFolder)
Next
End Sub
'[End First Move Code]

'[Rename Code - First Run] - Renames PST files with timestamp
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root
\cimv2")

Set FileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='y:\mailbox'} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile In FileList
strDate = Left(objFile.FileName & "_" & objFile.CreationDate, 32)
strNewName = objFile.Drive & objFile.Path & _
strDate & "." & "pst"
strNameCheck = Replace(strNewName, "\", "\\")

i = 1
Do While True
Set colFiles = objWMIService.ExecQuery _
("Select * from Cim_Datafile Where Name = '" &
strNameCheck & "'")
If colFiles.Count = 0 Then
errResult = objFile.Rename(strNewName)
Exit Do
Else
i = i + 1
strNewName = objFile.Drive & objFile.Path & _
strDate & "_" & i & "." & "pst"
strNameCheck = Replace(strNewName, "\", "\\")
End If
Loop
Next
'[End Rename Code - First Run]

'[Start Second Move Code] - Moves displaced files from MyDocuments
Redirection Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder2)
Set colFiles = objFolder.Files

For Each File in colFiles
set objFile = objFSO.GetFile(strFolder2 & "\" & File.Name)
Next

If lcase(Right(objFile.Name,3)) = "pst" Then
objFSO.MoveFile objFile, strTar & "N3-" & objfile.name
'objFSO.DeleteFile objFile, true
End If

ScanSubFolders(objFolder)

Sub ScanSubFolders(objFolder)

Set colFolders = objFolder.SubFolders
For Each objSubFolder In colFolders

Set colFiles = objSubFolder.Files
For Each objFile in Colfiles
If lcase(Right(objFile.Name,3)) = "pst" Then
objFSO.MoveFile objFile, strTar & "N4-" & objfile.name
'objFSO.DeleteFile objFile, true
End If
Next
ScanSubFolders(objSubFolder)
Next
End Sub
'[End Second Move Code]

'[Rename Code - Second Run] - Renames PST files with timestamp
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root
\cimv2")

Set FileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='y:\mailbox'} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile In FileList
strDate = Left(objFile.FileName & "_" & objFile.CreationDate, 30)
strNewName = objFile.Drive & objFile.Path & _
strDate & "." & "pst"
strNameCheck = Replace(strNewName, "\", "\\")

i = 1
Do While True
Set colFiles = objWMIService.ExecQuery _
("Select * from Cim_Datafile Where Name = '" &
strNameCheck & "'")
If colFiles.Count = 0 Then
errResult = objFile.Rename(strNewName)
Exit Do
Else
i = i + 1
strNewName = objFile.Drive & objFile.Path & _
strDate & "_" & i & "." & "pst"
strNameCheck = Replace(strNewName, "\", "\\")
End If
Loop
Next
'[End Rename Code - Second Run]

'[Release Local Map] - Releases local directory to drive map
WSHSHell.Run "subst Y: /D"
'[End Release Local Map]

'[Confirmation Code] - Provides dialog to confirm when files have been
relocated
'strComputer="."
'set objWMIService = GetObject("winmgmts:\\" & strComputer)
' Set colOperatingSystems = objWMIService.ExecQuery _
' ("Select * from Win32_OperatingSystem")

' For Each objOS in colOperatingSystems
' dtmLastBootUpTime = ConvWMIDateTime(objOS.LocalDateTime,
"ISO8601")
' Wscript.Echo "Completed at " & dtmLastBootUpTime
' Next
'
'Function ConvWMIDateTime(sDMTFformat, iNamedFormat)
'
' Dim sYear, sMonth, sDay, sHour, sMinutes, sSeconds
' sYear = mid(sDMTFformat, 1, 4)
' sMonth = mid(sDMTFformat, 5, 2)
' sDay = mid(sDMTFformat, 7, 2)
' sHour = mid(sDMTFformat, 9, 2)
' sMinutes = mid(sDMTFformat, 11, 2)
' sSeconds = mid(sDMTFformat, 13, 2)
'
' ConvWMIDateTime = sHour & ":" & sMinutes & ":" & sSeconds & " " _
' & sMonth & "-" & sDay & "-" & sYear
'
' If IsNumeric(iNamedFormat) Then
' If iNamedFormat >= 0 And iNamedFormat <= 4 Then
' ConvWMIDateTime = FormatDateTime(ConvWMIDateTime,
iNamedFormat)
' End If
' End If
'End Function
'[End Confirmation Code]
'[End Script]
My System SpecsSystem Spec
Old 09-13-2008   #6 (permalink)
Dakota Interactive


 
 

Re: Debugging Help Please! :) File renaming...

My apologies. Here is the complete code, fully complimented.

'[Comments]
'Moves existing PST files to new proper location - C:\Documents and
Settings\%username%\mailbox\
'Renames PST files w/ file_timemstamp.pst to prevent duplicate name
issues

'[Start Script]
Dim objFSO, objFolder, objShell, colFiles, objSubFolder, objSubFile,
fso, strDirectory, wshShell, objWMIService

Set fso = CreateObject("Scripting.FileSystemObject")
Set WshNetwork = WScript.CreateObject("Wscript.Network")
Set wshShell = WScript.CreateObject("WScript.Shell")

strUserID = WSHNetwork.UserName

'[Local Map] - Used to trick the WMI call to see a %username% call as
a physical directory
If fso.DriveExists("Y:") Then
WshNetwork.RemoveNetworkDrive "Y:",True,True
End If

WSHSHell.Run "subst Y: /D"
WSHSHell.Run "subst Y: ""C:\Documents and Settings\%username%"""
'[End Local Map]

'[Definition paths]
strpath = "C:\Documents and Settings\"
strpath2 = "\mailbox\"
strpath3 = "\\fs2\Documents\"
strpath4 = "\My Documents\"

strfinalpath = strpath & strUserID
'C:\Documents and Settings\%username%\
strfinalpath2 = strpath & strUserID & strpath2
'C:\Documents and Settings\%username%\mailbox\
strfinalpath3 = strpath3 & strUserID & strpath4
'\\fs2\Documents\%username%\My Documents\

strFolder = strfinalpath
strTar = strfinalpath2
strFolder2 = strfinalpath3
strTar2 = strfinalpath2
'[End definition paths]

'[Folder Creation]
strDirectory = strfinalpath + "\mailbox"
Set objFSO = CreateObject("Scripting.FileSystemObject")
'[End Folder Creation]

'[Start Folder/Content Confirmation] - Confirms if \mailbox has been
generated already w/ PST files
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
End If

If err.number = vbEmpty then
Set objShell = CreateObject("WScript.Shell")
Else WScript.echo "VBScript Error: " & err.number
End If

Set confirmation = fso.getfolder(strfinalpath2)
For Each File in confirmation.Files
If fso.GetExtensionName(File)="pst" Then
WSHSHell.Run "subst Y: /D"
wscript.quit
End If
Next
'[End Confirmation Code]

'[Start First Move Code] - Moves files from %username% First
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)
Set colFiles = objFolder.Files

For Each File in colFiles
set objFile = objFSO.GetFile(strFolder & "\" & File.Name)
Next

If lcase(Right(objFile.Name,3)) = "pst" Then
objFSO.MoveFile objFile, strTar & "N1-" & objfile.name
'objFSO.DeleteFile objFile, true
End If

ScanSubFolders(objFolder)

Sub ScanSubFolders(objFolder)

Set colFolders = objFolder.SubFolders
For Each objSubFolder In colFolders

Set colFiles = objSubFolder.Files
For Each objFile in Colfiles
If lcase(Right(objFile.Name,3)) = "pst" Then
objFSO.MoveFile objFile, strTar & "N2-" & objfile.name
'objFSO.DeleteFile objFile, true
End If
Next
ScanSubFolders(objSubFolder)
Next
End Sub
'[End First Move Code]

'[Rename Code - First Run] - Renames PST files with timestamp
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root
\cimv2")

Set FileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='y:\mailbox'} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile In FileList
strDate = Left(objFile.FileName & "_" & objFile.CreationDate, 32)
strNewName = objFile.Drive & objFile.Path & _
strDate & "." & "pst"
strNameCheck = Replace(strNewName, "\", "\\")

i = 1
Do While True
Set colFiles = objWMIService.ExecQuery _
("Select * from Cim_Datafile Where Name = '" &
strNameCheck & "'")
If colFiles.Count = 0 Then
errResult = objFile.Rename(strNewName)
Exit Do
Else
i = i + 1
strNewName = objFile.Drive & objFile.Path & _
strDate & "_" & i & "." & "pst"
strNameCheck = Replace(strNewName, "\", "\\")
End If
Loop
Next
'[End Rename Code - First Run]

'[Start Second Move Code] - Moves displaced files from MyDocuments
Redirection Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder2)
Set colFiles = objFolder.Files

For Each File in colFiles
set objFile = objFSO.GetFile(strFolder2 & "\" & File.Name)
Next

If lcase(Right(objFile.Name,3)) = "pst" Then
objFSO.MoveFile objFile, strTar & "N3-" & objfile.name
'objFSO.DeleteFile objFile, true
End If

ScanSubFolders(objFolder)

Sub ScanSubFolders(objFolder)

Set colFolders = objFolder.SubFolders
For Each objSubFolder In colFolders

Set colFiles = objSubFolder.Files
For Each objFile in Colfiles
If lcase(Right(objFile.Name,3)) = "pst" Then
objFSO.MoveFile objFile, strTar & "N4-" & objfile.name
'objFSO.DeleteFile objFile, true
End If
Next
ScanSubFolders(objSubFolder)
Next
End Sub
'[End Second Move Code]

'[Rename Code - Second Run] - Renames PST files with timestamp
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root
\cimv2")

Set FileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='y:\mailbox'} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile In FileList
strDate = Left(objFile.FileName & "_" & objFile.CreationDate, 30)
strNewName = objFile.Drive & objFile.Path & _
strDate & "." & "pst"
strNameCheck = Replace(strNewName, "\", "\\")

i = 1
Do While True
Set colFiles = objWMIService.ExecQuery _
("Select * from Cim_Datafile Where Name = '" &
strNameCheck & "'")
If colFiles.Count = 0 Then
errResult = objFile.Rename(strNewName)
Exit Do
Else
i = i + 1
strNewName = objFile.Drive & objFile.Path & _
strDate & "_" & i & "." & "pst"
strNameCheck = Replace(strNewName, "\", "\\")
End If
Loop
Next
'[End Rename Code - Second Run]

'[Release Local Map] - Releases local directory to drive map
WSHSHell.Run "subst Y: /D"
'[End Release Local Map]

'[Confirmation Code] - Provides dialog to confirm when files have been
relocated
'strComputer="."
'set objWMIService = GetObject("winmgmts:\\" & strComputer)
' Set colOperatingSystems = objWMIService.ExecQuery _
' ("Select * from Win32_OperatingSystem")

' For Each objOS in colOperatingSystems
' dtmLastBootUpTime = ConvWMIDateTime(objOS.LocalDateTime,
"ISO8601")
' Wscript.Echo "Completed at " & dtmLastBootUpTime
' Next
'
'Function ConvWMIDateTime(sDMTFformat, iNamedFormat)
'
' Dim sYear, sMonth, sDay, sHour, sMinutes, sSeconds
' sYear = mid(sDMTFformat, 1, 4)
' sMonth = mid(sDMTFformat, 5, 2)
' sDay = mid(sDMTFformat, 7, 2)
' sHour = mid(sDMTFformat, 9, 2)
' sMinutes = mid(sDMTFformat, 11, 2)
' sSeconds = mid(sDMTFformat, 13, 2)
'
' ConvWMIDateTime = sHour & ":" & sMinutes & ":" & sSeconds & " " _
' & sMonth & "-" & sDay & "-" & sYear
'
' If IsNumeric(iNamedFormat) Then
' If iNamedFormat >= 0 And iNamedFormat <= 4 Then
' ConvWMIDateTime = FormatDateTime(ConvWMIDateTime,
iNamedFormat)
' End If
' End If
'End Function
'[End Confirmation Code]
'[End Script]
My System SpecsSystem Spec
Old 09-13-2008   #7 (permalink)
Pegasus \(MVP\)


 
 

Re: Debugging Help Please! :) File renaming...


"Dakota Interactive" <onebucktraffic@xxxxxx> wrote in message
news:03293d0a-8a5a-455d-9bf2-d660bf94db81@xxxxxx
Quote:

> My apologies... here is the entire thing, fully commented.
>
> '[Comments]
> 'Moves existing PST files to new proper location - C:\Documents and
> Settings\%username%\mailbox\
> 'Renames PST files w/ file_timemstamp.pst to prevent duplicate name
> issues
>
> '[Start Script]
> Dim objFSO, objFolder, objShell, colFiles, objSubFolder, objSubFile,
> fso, strDirectory, wshShell, objWMIService
>
> Set fso = CreateObject("Scripting.FileSystemObject")
> Set WshNetwork = WScript.CreateObject("Wscript.Network")
> Set wshShell = WScript.CreateObject("WScript.Shell")
>
> strUserID = WSHNetwork.UserName
>
> '[Local Map] - Used to trick the WMI call to see a %username% call as
> a physical directory
> If fso.DriveExists("Y:") Then
> WshNetwork.RemoveNetworkDrive "Y:",True,True
> End If
>
> WSHSHell.Run "subst Y: /D"
> WSHSHell.Run "subst Y: ""C:\Documents and Settings\%username%"""
> '[End Local Map]
>
> '[Definition paths]
> strpath = "C:\Documents and Settings\"
> strpath2 = "\mailbox\"
> strpath3 = "\\fs2\Documents\"
> strpath4 = "\My Documents\"
>
> strfinalpath = strpath & strUserID
> 'C:\Documents and Settings\%username%\
> strfinalpath2 = strpath & strUserID & strpath2
> 'C:\Documents and Settings\%username%\mailbox\
> strfinalpath3 = strpath3 & strUserID & strpath4
> '\\fs2\Documents\%username%\My Documents\
>
> strFolder = strfinalpath
> strTar = strfinalpath2
> strFolder2 = strfinalpath3
> strTar2 = strfinalpath2
> '[End definition paths]
>
> '[Folder Creation]
> strDirectory = strfinalpath + "\mailbox"
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> '[End Folder Creation]
>
> '[Start Folder/Content Confirmation] - Confirms if \mailbox has been
> generated already w/ PST files
> If objFSO.FolderExists(strDirectory) Then
> Set objFolder = objFSO.GetFolder(strDirectory)
> Else
> Set objFolder = objFSO.CreateFolder(strDirectory)
> End If
>
> If err.number = vbEmpty then
> Set objShell = CreateObject("WScript.Shell")
> Else WScript.echo "VBScript Error: " & err.number
> End If
>
> Set confirmation = fso.getfolder(strfinalpath2)
> For Each File in confirmation.Files
> If fso.GetExtensionName(File)="pst" Then
> WSHSHell.Run "subst Y: /D"
> wscript.quit
> End If
> Next
> '[End Confirmation Code]
>
> '[Start First Move Code] - Moves files from %username% First
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFolder = objFSO.GetFolder(strFolder)
> Set colFiles = objFolder.Files
>
> For Each File in colFiles
> set objFile = objFSO.GetFile(strFolder & "\" & File.Name)
> Next
>
> If lcase(Right(objFile.Name,3)) = "pst" Then
> objFSO.MoveFile objFile, strTar & "N1-" & objfile.name
> 'objFSO.DeleteFile objFile, true
> End If
>
> ScanSubFolders(objFolder)
>
> Sub ScanSubFolders(objFolder)
>
> Set colFolders = objFolder.SubFolders
> For Each objSubFolder In colFolders
>
> Set colFiles = objSubFolder.Files
> For Each objFile in Colfiles
> If lcase(Right(objFile.Name,3)) = "pst" Then
> objFSO.MoveFile objFile, strTar & "N2-" & objfile.name
> 'objFSO.DeleteFile objFile, true
> End If
> Next
> ScanSubFolders(objSubFolder)
> Next
> End Sub
> '[End First Move Code]
>
> '[Rename Code - First Run] - Renames PST files with timestamp
> strComputer = "."
> Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root
> \cimv2")
>
> Set FileList = objWMIService.ExecQuery _
> ("ASSOCIATORS OF {Win32_Directory.Name='y:\mailbox'} Where " _
> & "ResultClass = CIM_DataFile")
>
> For Each objFile In FileList
> strDate = Left(objFile.FileName & "_" & objFile.CreationDate, 32)
> strNewName = objFile.Drive & objFile.Path & _
> strDate & "." & "pst"
> strNameCheck = Replace(strNewName, "\", "\\")
>
> i = 1
> Do While True
> Set colFiles = objWMIService.ExecQuery _
> ("Select * from Cim_Datafile Where Name = '" &
> strNameCheck & "'")
> If colFiles.Count = 0 Then
> errResult = objFile.Rename(strNewName)
> Exit Do
> Else
> i = i + 1
> strNewName = objFile.Drive & objFile.Path & _
> strDate & "_" & i & "." & "pst"
> strNameCheck = Replace(strNewName, "\", "\\")
> End If
> Loop
> Next
> '[End Rename Code - First Run]
>
> '[Start Second Move Code] - Moves displaced files from MyDocuments
> Redirection Next
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFolder = objFSO.GetFolder(strFolder2)
> Set colFiles = objFolder.Files
>
> For Each File in colFiles
> set objFile = objFSO.GetFile(strFolder2 & "\" & File.Name)
> Next
>
> If lcase(Right(objFile.Name,3)) = "pst" Then
> objFSO.MoveFile objFile, strTar & "N3-" & objfile.name
> 'objFSO.DeleteFile objFile, true
> End If
>
> ScanSubFolders(objFolder)
>
> Sub ScanSubFolders(objFolder)
>
> Set colFolders = objFolder.SubFolders
> For Each objSubFolder In colFolders
>
> Set colFiles = objSubFolder.Files
> For Each objFile in Colfiles
> If lcase(Right(objFile.Name,3)) = "pst" Then
> objFSO.MoveFile objFile, strTar & "N4-" & objfile.name
> 'objFSO.DeleteFile objFile, true
> End If
> Next
> ScanSubFolders(objSubFolder)
> Next
> End Sub
> '[End Second Move Code]
>
> '[Rename Code - Second Run] - Renames PST files with timestamp
> strComputer = "."
> Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root
> \cimv2")
>
> Set FileList = objWMIService.ExecQuery _
> ("ASSOCIATORS OF {Win32_Directory.Name='y:\mailbox'} Where " _
> & "ResultClass = CIM_DataFile")
>
> For Each objFile In FileList
> strDate = Left(objFile.FileName & "_" & objFile.CreationDate, 30)
> strNewName = objFile.Drive & objFile.Path & _
> strDate & "." & "pst"
> strNameCheck = Replace(strNewName, "\", "\\")
>
> i = 1
> Do While True
> Set colFiles = objWMIService.ExecQuery _
> ("Select * from Cim_Datafile Where Name = '" &
> strNameCheck & "'")
> If colFiles.Count = 0 Then
> errResult = objFile.Rename(strNewName)
> Exit Do
> Else
> i = i + 1
> strNewName = objFile.Drive & objFile.Path & _
> strDate & "_" & i & "." & "pst"
> strNameCheck = Replace(strNewName, "\", "\\")
> End If
> Loop
> Next
> '[End Rename Code - Second Run]
>
> '[Release Local Map] - Releases local directory to drive map
> WSHSHell.Run "subst Y: /D"
> '[End Release Local Map]
>
> '[Confirmation Code] - Provides dialog to confirm when files have been
> relocated
> 'strComputer="."
> 'set objWMIService = GetObject("winmgmts:\\" & strComputer)
> ' Set colOperatingSystems = objWMIService.ExecQuery _
> ' ("Select * from Win32_OperatingSystem")
>
> ' For Each objOS in colOperatingSystems
> ' dtmLastBootUpTime = ConvWMIDateTime(objOS.LocalDateTime,
> "ISO8601")
> ' Wscript.Echo "Completed at " & dtmLastBootUpTime
> ' Next
> '
> 'Function ConvWMIDateTime(sDMTFformat, iNamedFormat)
> '
> ' Dim sYear, sMonth, sDay, sHour, sMinutes, sSeconds
> ' sYear = mid(sDMTFformat, 1, 4)
> ' sMonth = mid(sDMTFformat, 5, 2)
> ' sDay = mid(sDMTFformat, 7, 2)
> ' sHour = mid(sDMTFformat, 9, 2)
> ' sMinutes = mid(sDMTFformat, 11, 2)
> ' sSeconds = mid(sDMTFformat, 13, 2)
> '
> ' ConvWMIDateTime = sHour & ":" & sMinutes & ":" & sSeconds & " " _
> ' & sMonth & "-" & sDay & "-" & sYear
> '
> ' If IsNumeric(iNamedFormat) Then
> ' If iNamedFormat >= 0 And iNamedFormat <= 4 Then
> ' ConvWMIDateTime = FormatDateTime(ConvWMIDateTime,
> iNamedFormat)
> ' End If
> ' End If
> 'End Function
> '[End Confirmation Code]
> '[End Script]
After I've had a bit of a look at your code I was left somewhat confused.
Here is why:
- You declare objFSO three times. Why?
- You have two subroutines called ScanSubFolders. Why?
- You have these two lines of code:
If fso.DriveExists("Y:") Then WshNetwork.RemoveNetworkDrive
"Y:",True,True
wshShell.Run "subst Y: /D"
What is the point of removing a non-existing substitution?
- You have these three lines of code:
For Each File In colFiles
Set objFile = objFSO.GetFile(strFolder & "\" & File.Name)
Next
What is the point of repeatedly creating an object without doing
anything with it?
- The lines
strDate = Left(objFile.FileName & "_" & objFile.CreationDate, 30)
strNewName = objFile.Drive & objFile.Path & strDate & "." & "pst"
will set strNewName to a string that includes the date in universal time
format,
which is unsuitable for a file name.

Analysing and debugging some 250 lines of someone else's code can take a lot
of time unless the issue is straightforward. I suggest you describe in
functional terms what you're trying to do. I suspect that someone will then
suggest a simple solution to your problem.


My System SpecsSystem Spec
Old 09-13-2008   #8 (permalink)
Al Dunbar


 
 

Re: Debugging Help Please! :) File renaming...


"Pegasus (MVP)" <I.can@xxxxxx> wrote in message
news:uD6AeOeFJHA.616@xxxxxx
Quote:

>
> "Dakota Interactive" <onebucktraffic@xxxxxx> wrote in message
> news:03293d0a-8a5a-455d-9bf2-d660bf94db81@xxxxxx
Quote:

>> My apologies... here is the entire thing, fully commented.
<snip>
Quote:

>
> After I've had a bit of a look at your code I was left somewhat confused.
> Here is why:
> - You declare objFSO three times. Why?
> - You have two subroutines called ScanSubFolders. Why?
> - You have these two lines of code:
> If fso.DriveExists("Y:") Then WshNetwork.RemoveNetworkDrive
> "Y:",True,True
> wshShell.Run "subst Y: /D"
> What is the point of removing a non-existing substitution?
> - You have these three lines of code:
> For Each File In colFiles
> Set objFile = objFSO.GetFile(strFolder & "\" & File.Name)
> Next
> What is the point of repeatedly creating an object without doing
> anything with it?
> - The lines
> strDate = Left(objFile.FileName & "_" & objFile.CreationDate, 30)
> strNewName = objFile.Drive & objFile.Path & strDate & "." & "pst"
> will set strNewName to a string that includes the date in universal time
> format,
> which is unsuitable for a file name.
>
> Analysing and debugging some 250 lines of someone else's code can take a
> lot of time unless the issue is straightforward. I suggest you describe in
> functional terms what you're trying to do. I suspect that someone will
> then suggest a simple solution to your problem.
Exactly. Also, it is even more difficult to determine the cause of a
particular problem in someone's code when there are so many problems with
it.

Here is an earlier post:
Quote:

> This code segment is part of a larger script that moves PST files so
> that they are accessible to users anywhere in the network. The rest of
> the script works but this is making me nuts. This is supposed to
> determine if two or more files have the same name, and if so, rename
> them to include the file dat or timestamp as well. It doesn't work.
> Can some offer some guidance?
Your definition of the task is a bit suspect. If I were writing such a
script I would never even bother to figure out if more than two files had
the same name. Rather, for each file processed in turn, I would determine if
a file of the same name existed in the target folder, and then copy it under
a new name if necessary.

But, given that some files will be given new names, why not simply do that
to all of the files placed there?


/Al


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Renaming a file Vista file management
Renaming a file Vista security
renaming a file Vista file management
Renaming a file Vista file management


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46