Looks good but now I get a
An unhandled exception of type 'System.IO.IOException'
occurred in mscorlib.dll
Additional information: Cannot create a file when that
file already exists.
error. When I look at the break the strNewPath value
shows the same name as the first file processed in the
series. Shouldn't that Ticks value coming back be unique?
Here's the current code:
Dim strPath As String = "C:\Out\"
Dim strFiles() As String = Directory.GetFiles(strPath)
For Each strFile As String In strFiles
Dim dtNow As Date = DateTime.Now
Dim strTime As String = dtNow.Ticks.ToString
Dim strOldPath As String = strFile
Dim strNewPath As String = strPath & strTime & ".jpg"
File.Move(strOldPath, strNewPath)
Next strFile
-----Original Message-----
*Yikes* Don't use the FileSystemObject, it never was a
good tool (I hearflames), and is certainly obsolete (more flames).
Use the managed classes to rename files:
Imports System.IO
..
..
..
Dim strPath As String = "C:\Temp\"
Dim strFiles() As String = Directory.GetFiles(strPath)
For Each strFile As String In strFiles
Dim dtNow As Date = DateTime.Now
Dim strTime As String = dtNow.Year.ToString +
dtNow.Month.ToString +dtNow.Month.ToString
strTime &= dtNow.Ticks.ToString
Dim strOldPath As String = strPath & strFile
Dim strNewPath As String = strPath & strTime & ".jpg"
File.Move(strOldPath, strNewPath)
Next
--
Happy to help,
-- Tom Spink
(th**********@ntlworld.com)
"Go down with your server"
http://dotnetx.betasafe.com >> On The Mend
Please respond to the newsgroup,
so all can benefit
"Zachariah" <za*********@yahoo.com> wrote in message
news:03****************************@phx.gbl... I have code that renames files in a folder. Here it is:
Dim strPath As String
Dim objFSO As New Scripting.FileSystemObject
Dim objFolder
Dim objFile
Dim strOldPath As String
Dim strNewPath As String
Dim strTime As String
strPath = "C:\Out\"
objFSO = CreateObject("Scripting.FileSystemObject")
objFolder = objFSO.GetFolder(strPath)
For Each objFile In objFolder.Files
'Get time
strTime = Format _
(Microsoft.VisualBasic.Timer, "00000.00000")
strTime = Format(Today(), "yyyyMMdd") + Trim(Mid _
(strTime, 1, (InStr(strTime, ".") - 1))) + Mid _
(strTime, (InStr(strTime, ".") + 1), (Len
(strTime) _ - InStr(strTime, ".") + 1))
strOldPath = strPath & objFile.Name
'strNewPath = strPath & strTime & objFile.Name
strNewPath = strPath & strTime & ".jpg"
Microsoft.VisualBasic.Rename(strOldPath, strNewPath)
Next objFile
objFile = Nothing
objFolder = Nothing
objFSO = Nothing
The line
strNewPath = strPath & strTime & ".jpg"
is causing a
An unhandled exception of
type 'System.ArgumentException' occurred in microsoft.visualbasic.dll
Additional information: Procedure call or argument is
not valid.
message. If I used the commented line above it the code
works but I'm stuck with new filenames that still
include the old file names in them. I want to eliminate
the old file names but if I take out the objFile.Name
reference the code breaks. Is there a workaround for
this?
.