472,143 Members | 1,154 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

Renaming within a file iteration

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?
Nov 19 '05 #1
5 3535
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?

.

Nov 19 '05 #2
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?
-----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?

.

Nov 19 '05 #3
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.
Nov 19 '05 #4
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
(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:0b****************************@phx.gbl...
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.

Nov 19 '05 #5
Thanks man, that solved my woes.
Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by hokiegal99 | last post: by
reply views Thread by MikeY | last post: by
5 posts views Thread by masood.iqbal | last post: by
reply views Thread by Andy | last post: by
1 post views Thread by MikeY | last post: by
8 posts views Thread by BillCo | last post: by
11 posts views Thread by andreyvul | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.