473,372 Members | 1,240 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 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 3593
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
0
by: MikeY | last post by:
Hopefully someone can help, I have a listview box where I display my desired files. I single click on the desired file to be renamed and I rename it with a new name. My problem arises when the...
5
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at...
0
by: Andy | last post by:
Hello: I am using System.Web.Mail.MailMessage. Currently: when I am attaching a file to the object, I am renaming the file using the FileInfo.Copy method and attaching the new file to the...
1
by: MikeY | last post by:
Hopefully someone can help, I have a listview box where I display my desired files. I single click on the desired file to be renamed and I rename it with a new name. My problem arises when the...
8
by: BillCo | last post by:
I'm updating a legacy app with table naming that makes baby jesus cry. It's a bit of a spider web though... no telling when and where the tables will be called by name. So I wrote this for renaming...
34
by: Tom | last post by:
I'd greatly appreciate advice and code snippets on how to create a ram disk within a C/C++ program. I also need to be able to determine the free space. Thanks in advance for any help.
1
by: GeoDW | last post by:
Hell All, I have looked around and not found the solution I am looking for within the old threads. Here is my problem: I have a directory full of .img and .rrd files that have long filenames...
11
by: andreyvul | last post by:
What I'm trying to do is have the preprocessor parse one file twice. The file has three parts, and each is dependent on the previous. Example (file name is foo.c): #ifndef ONCE /* first part */...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.