473,511 Members | 15,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rename Files in a Directory

I have Access XP. I know Visual Basic quite well. One thing I know Access
can do--and I can't quite figure out how--is to rename the files in a
directory.

If the file is named "aug01_003.jpg" I want to rename it 2004_08_003.jpg. I
can understand how to do it to this extent:

Ch Dir () ' whatever directory these files are located in
Dim stOldName as string, stNewName as string
stOldName = 'whatever syntax would go here to 'sense' the "next" file
stNewName = "2004_08_" & mid(stOldName,7,3) & ".JPG"
Name stOldName as stNewName

I could see everything after Ch Dir being contained in a loop which tells it
basically to do this to every file until the end of the directory is
reached. That's where I am stuck, as well as what I set stOldName equal to.

Tips?

LRH
Nov 13 '05 #1
4 17837
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

dim strNewName as string
dim strOldName as string

' Get the 1st file in the strPathName that
' has "aug01_" as it's first characters

strOldName = Dir(strPathName & "\aug01_*.jpg")

' Loop until the file(s) aren't found

do while len(strOldName)>0

strNewName = "2004_08_mid(strOldName,7,3) & ".jpg"

' Get the next file in strPathName
strOldName = Dir()

loop

For more info see the VBA Help article "Dir Function."

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQQ6DyoechKqOuFEgEQJTDACg5fM+pCEzvkXs1xymfuNJt5 CvcwIAn0gA
2Q6HE6vakmcHgdo0O7HyhXlq
=7HSp
-----END PGP SIGNATURE-----
Larry R Harrison Jr wrote:
I have Access XP. I know Visual Basic quite well. One thing I know Access
can do--and I can't quite figure out how--is to rename the files in a
directory.

If the file is named "aug01_003.jpg" I want to rename it 2004_08_003.jpg. I
can understand how to do it to this extent:

Ch Dir () ' whatever directory these files are located in
Dim stOldName as string, stNewName as string
stOldName = 'whatever syntax would go here to 'sense' the "next" file
stNewName = "2004_08_" & mid(stOldName,7,3) & ".JPG"
Name stOldName as stNewName

I could see everything after Ch Dir being contained in a loop which tells it
basically to do this to every file until the end of the directory is
reached. That's where I am stuck, as well as what I set stOldName equal to.


Nov 13 '05 #2
You can read all of the files in a directory as:

Dim strFolder As String
Dim strFile As String

strFolder = "C:\Some Folder\"
strFile = Dir$(strFolder) & "*.*"
Do While Len(strFile) > 0
Debug.Print strFile
strFile = Dir$()
Loop

I think in this case, you might need to build an array of existing files,
though, as I'm not sure whether Dir is smart enough to ignore the renamed
files.

Dim intLoop
Dim strFolder As String
Dim strFile As String
Dim strNewName As String
Dim strExistingFiles() As String

intLoop = 0
ReDim strExistingFiles(0 to intLoop + 9)
strFolder = "C:\Some Folder\"
strFile = Dir$(strFolder) & "*.*"
Do While Len(strFile) > 0
If (intLoop Mod 10) = 0 Then
ReDim Preserve strExistingFiles(0 to intLoop + 9)
End If
strExistingFiles(intLoop) = strFile
intLoop = intLoop + 1
strFile = Dir$()
Loop

ReDim Preserve strExistingFiles(0 to (intLoop - 1))

For intLoop = LBound(strExistingFiles) To UBound(strExistingFiles)
' Figure out what strNewName should be
Name strFolder & strExistingFiles(intLoop) As strFolder & strNewName
Next intLoop

Note that this is untested "aircode".

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

"Larry R Harrison Jr" <no***@noone.com> wrote in message
news:TtuPc.16546$Xn.13470@fed1read05...
I have Access XP. I know Visual Basic quite well. One thing I know Access
can do--and I can't quite figure out how--is to rename the files in a
directory.

If the file is named "aug01_003.jpg" I want to rename it 2004_08_003.jpg. I can understand how to do it to this extent:

Ch Dir () ' whatever directory these files are located in
Dim stOldName as string, stNewName as string
stOldName = 'whatever syntax would go here to 'sense' the "next" file
stNewName = "2004_08_" & mid(stOldName,7,3) & ".JPG"
Name stOldName as stNewName

I could see everything after Ch Dir being contained in a loop which tells it basically to do this to every file until the end of the directory is
reached. That's where I am stuck, as well as what I set stOldName equal to.
Tips?

LRH

Nov 13 '05 #3
Hmm, okay, with some Google searching and some tweaks of my own, I have the
fix.

I hope (a) the fact that it seems I frequently find a fix after posting
doesn't dissaude the lurkers from offering tips if they think of any and (b)
the code I have is the BEST for doing this.

Regardless, it does work.

Notes:

* The files were in the "C:\temp" folder

* It takes a file with the name "mar07_001.jpg" and makes it
"2004_03_07_001.jpg" It would probably have to be modified to fit other
patterns

* Related: I have to specify the pattern; it doesn't, say, "figure out" the
"07" portion by doing an Instr search for the _ character or whatever. You
have to SPECIFY the pattern

* The ChDir command may not be necessary

* I set stOldName = filename; that isn't necessary, I just did that because
I like using "stOldName" to make things obvious

* the stNameOnly simply makes it easier to figure out the pattern, because I
don't have to remember the filepath when figuring out the numerical position
of what is located where. So that's why the stPath=Right.... etc etc command
is there, so that I can use it in the "stNewName = " command and it makes it
easier to figure out what characters are located where when I don't have to
take the filepath into account with it

Anyway. Here is the code; I put this within a command button on a form:

ChDir ("c:\TEMP\")
Dim stNewName As String, stOldName
Dir_List = "C:\temp\"
Directory = Dir(Dir_List)
If Len(Directory) > 0 Then
Do
filename = "c:\temp\" & Directory
stOldName = filename
stNameOnly = Right(stOldName, Len(stOldName) - 8)
'MsgBox "stnameOnly = " & stNameOnly
stNewName = "C:\temp\2004_03_" & Mid(stNameOnly, 4, 6) & ".jpg"
Name stOldName As stNewName
Directory = Dir
Loop Until Len(Directory) = 0 'And stFirst <> filename
End If

Since this is something I seem to do pretty often, I'm considering adding
controls to the form which "preview" what it would look like, and modifying
the code to derive its parameters from the controls. That would reduce the
possibility of mistakes and make it even easier to use.

Thanks for the tips anyway. If anyone has any suggestions on how this code
could be better, by all means let me know.
Nov 13 '05 #4
The only thing is, that apparently under some obscure
circumstances the order of the files in your directory
may change while you work, so that a DIR loop misses some.
So people commonly make a list or array of file names
before starting.

If anyone has any idea WHAT obscure circumstances might
cause a DIR loop to fail, I'd be interested to know.

(david)
"Larry R Harrison Jr" <no***@noone.com> wrote in message
news:7xvPc.16674$Xn.1003@fed1read05...
Hmm, okay, with some Google searching and some tweaks of my own, I have the fix.

I hope (a) the fact that it seems I frequently find a fix after posting
doesn't dissaude the lurkers from offering tips if they think of any and (b) the code I have is the BEST for doing this.

Regardless, it does work.

Notes:

* The files were in the "C:\temp" folder

* It takes a file with the name "mar07_001.jpg" and makes it
"2004_03_07_001.jpg" It would probably have to be modified to fit other
patterns

* Related: I have to specify the pattern; it doesn't, say, "figure out" the "07" portion by doing an Instr search for the _ character or whatever. You
have to SPECIFY the pattern

* The ChDir command may not be necessary

* I set stOldName = filename; that isn't necessary, I just did that because I like using "stOldName" to make things obvious

* the stNameOnly simply makes it easier to figure out the pattern, because I don't have to remember the filepath when figuring out the numerical position of what is located where. So that's why the stPath=Right.... etc etc command is there, so that I can use it in the "stNewName = " command and it makes it easier to figure out what characters are located where when I don't have to take the filepath into account with it

Anyway. Here is the code; I put this within a command button on a form:

ChDir ("c:\TEMP\")
Dim stNewName As String, stOldName
Dir_List = "C:\temp\"
Directory = Dir(Dir_List)
If Len(Directory) > 0 Then
Do
filename = "c:\temp\" & Directory
stOldName = filename
stNameOnly = Right(stOldName, Len(stOldName) - 8)
'MsgBox "stnameOnly = " & stNameOnly
stNewName = "C:\temp\2004_03_" & Mid(stNameOnly, 4, 6) & ".jpg" Name stOldName As stNewName
Directory = Dir
Loop Until Len(Directory) = 0 'And stFirst <> filename
End If

Since this is something I seem to do pretty often, I'm considering adding
controls to the form which "preview" what it would look like, and modifying the code to derive its parameters from the controls. That would reduce the
possibility of mistakes and make it even easier to use.

Thanks for the tips anyway. If anyone has any suggestions on how this code
could be better, by all means let me know.

Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6393
by: glub glub | last post by:
i'm trying to make a program that works as Replace works in MS Word but this is for use with files, not a text document. FolderBrowserDialog1.ShowDialog() txtPath.Text =...
5
2704
by: Tony Meyer | last post by:
On Windows, if I do os.rename(old, new) where old is a file that is in-use (e.g. python itself, or a dll that is loaded), I would expect that an error would be raised (e.g. as when os.remove is...
3
3601
by: rn5a | last post by:
An inquisitive question...... A ListBox lists all the directories & files residing in a directory on the server. Assume that the ListBox lists 2 directories & 4 files. Also assume that one of...
1
2136
by: codemaster | last post by:
Hi, I am a total novice to perl. I am moving files from one directory to another and trying to rename a file appending the timestamp. But for some reason, my code is not able to rename. ...
5
2276
by: mythili123 | last post by:
The script will rename all files in the current directory whose names contain the first argument by replacing that part of the filename with the second argument. so far i could just get the file...
2
3613
by: =?iso-8859-1?b?cultaQ==?= | last post by:
Hi, I would like to rename files (jpg's ones) using a text file containing the new names... Below is the code that doesn't work : ***** #!/usr/bin/python #-*- coding: utf-8 -*- from os...
2
1512
by: shapper | last post by:
Hello, I am using Visual Studio 2008 and Web Deployment Projects:...
1
1608
by: lukas | last post by:
hello, i recently had the job of having to rename about 200 files. The source for the renaming was a bunch of names in a file. I know next to nothing when it comes to bash scripting (which would...
1
5896
by: Alien | last post by:
Hi, I am new to PHP and trying to move a zip file from one directory to another. I researched on the web on this and all fingers pointed to this rename() function. I tried writing a PHP script...
0
7245
marktang
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,...
0
7144
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...
0
7356
Oralloy
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,...
0
7427
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7512
tracyyun
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...
0
5671
agi2029
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,...
0
3227
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...
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.