Renaming within a file iteration

  • Thread starter Thread starter Zachariah
  • Start date Start date
Z

Zachariah

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?
 
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
 
I added a MsgBox() line and it functions when that's in
there. Is it possible the code is running too fast for
the tick value to have changed from the previous iteration?
 
I tossed a Thread.Sleep(5) into the For...Next loop and it
works, but I would like to know why I have to do that.
 
You were correct, because the code is cycling so fast, the ticks value
hasn't changed. Why not put an increment counter in there instead:

Dim I As Integer
..
..
..
For Each strFile As String In strFiles

I += 1

Dim dtNow As Date = DateTime.Now
Dim strTime As String = dtNow.Year.ToString + dtNow.Month.ToString +
dtNow.Month.ToString

strTime &= I.ToString

Dim strOldPath As String = strPath & strFile
Dim strNewPath As String = strPath & strTime & ".jpg"

File.Move(strOldPath, strNewPath)

Next

--
Happy to help,
-- Tom Spink
([email protected])

"Go down with your server"

http://dotnetx.betasafe.com >> On The Mend

Please respond to the newsgroup,
so all can benefit
 
Back
Top