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? 5 3602
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?
.
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?
.
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
(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.
Thanks man, that solved my woes. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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.
|
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...
|
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 */...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |