469,271 Members | 997 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

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 16863
-----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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by glub glub | last post: by
3 posts views Thread by rn5a | last post: by
5 posts views Thread by mythili123 | last post: by
2 posts views Thread by =?iso-8859-1?b?cultaQ==?= | last post: by
1 post views Thread by lukas | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.