Hi all,
Fist of all, sorry for my bad English.
I have a database with a lot of articles. Each article has an image, but this image is stored in a folder called c:\Images. Now I need to rename all the images to a new name based in a field value of the table. i.e.: Table Articles: Fields: OldName, Newname. Then I have to rename or copy to another path all files, oldname1.jpg, oldname2.jpg, etc to newname1.jpg, newname2.jpg, etc.
I wonder if is possible to make this with VBA.
Thanks in advance.
Jose
10 6744 zmbd 5,501
Expert Mod 4TB
The basic concept is as follows: - Create a Recordset based on the Articles Table.
- Loop thru the Recordset and determine if the [OldName] Field is NULL, if it is skip this Record.
- If the [OldName] Field is not NULL, see if the File exists in the C:\Images Path and that the [NewName] Field is not NULL.
- Assuming both conditions are True, copy the File [OldFile] to the same Folder renaming it [NewFile]
- Demo Code follows to get you started, any questions feel free to ask.
- Const conIMAGE_PATH As String = "C:\Images\"
-
Dim MyDB As DAO.Database
-
Dim rst As DAO.Recordset
-
-
Set MyDB = CurrentDb
-
Set rst = MyDB.OpenRecordset("SELECT * FROM Articles", dbOpenForwardOnly)
-
-
With rst
-
Do While Not .EOF
-
'Does the File exist in C:\Images and is there a NewName for the File?
-
If Dir(conIMAGE_PATH & ![OldName]) <> "" And Not IsNull(![NewName]) Then
-
'Copy Source to Destination essentially making a Copy of the File ([OldName]
-
'in the same Folder but under a New Name ([NewName])
-
FileCopy conIMAGE_PATH & ![OldName], conIMAGE_PATH & ![NewName]
-
End If
-
.MoveNext
-
Loop
-
End With
-
-
rst.Close
-
Set rst = Nothing
- Given the following Articles Table only Files 1.bmp, 4.bmp, and 5.bmp will be copied/renamed.
-
ID OldName NewName
-
1 1.bmp One.bmp
-
2 2.bmp
-
3 Three.bmp
-
4 4.bmp Four.bmp
-
5 5.bmp Five.bmp
-
6 6.bmp
-
Hi,
I've made your solution but nothing happens. If you want, I send you a example db with some photos to try.
Thanks a lot.
Regards.
Jose.
zmbd 5,501
Expert Mod 4TB
Please understand, many of us cannot or will not d/l un-requested attachments. This has absolutely nothing to do with who you are... In my case, such d/l are prohibited by my ITSec staff. For many others, it is part of "safe computing/best practices" not to d/l such files. A practical example as to why can be found here http://bytes.com/topic/access/answer...l-ms-products. this unfourtunate Member opened a file from a trusted source and is now haveing all sorts of issues!
Instead, please do the following basic troubleshooting outlined here:[*] > Before Posting (VBA or SQL) Code
You may have to repeat the compile step many times.
Once you have an error free code, please post it as described in the link.
The Code has been tested and is fully operational. - Is your Table named Articles and does it contain two TEXT Fields named OldName and NewName?
- Does the Folder Images exist in the Root Directory of Drive C:, namely C:\Images\?
- Does the C:\Images\ Folder contain Image Files and do their Base Names match exactly with the entries in the OldName Field of the Articles Table?
- Do you have corresponding NewName Values in the Articles Table for at least some of their counterparts, as in:
- MyPicture.jpg ==> MyPicture_2.jpg
- You say that nothing happens but nothing will happen. Did you look inside the C:\Images\ Folder to see the Old Files copied to their New Names?
- Replace the Original Code with the following which is exactly the same but does provide some Error Checking which hopefully will point you in the right direction.
- Let us know how you make out.
- On Error GoTo Err_cmdCopyRename_Click
-
Const conIMAGE_PATH As String = "C:\Images\"
-
Dim MyDB As DAO.Database
-
Dim rst As DAO.Recordset
-
-
Set MyDB = CurrentDb
-
Set rst = MyDB.OpenRecordset("SELECT * FROM Articles", dbOpenForwardOnly)
-
-
If (rst.BOF And rst.EOF) Then
-
MsgBox "There are no Files to Copy", vbExclamation, "No Files Fouond"
-
Exit Sub
-
End If
-
-
With rst
-
Do While Not .EOF
-
If Not IsNull(![OldName]) Then
-
'Does the File exist in C:\Images and is there a NewName for the File?
-
If Dir(conIMAGE_PATH & ![OldName]) <> "" And Not IsNull(![NewName]) Then
-
'Copy Source to Destination essentially making a Copy of the File ([OldName]
-
'in the same Folder but under a New Name ([NewName])
-
FileCopy conIMAGE_PATH & ![OldName], conIMAGE_PATH & ![NewName]
-
End If
-
End If
-
.MoveNext
-
Loop
-
End With
-
-
MsgBox "File Copy/Rename Operation successful", vbInformation, "Process Completed"
-
-
rst.Close
-
Set rst = Nothing
-
-
Exit_cmdCopyRename_Click:
-
Exit Sub
-
-
Err_cmdCopyRename_Click:
-
MsgBox Err.Description, vbExclamation, "Error in cmdCopyRename_Click()"
-
If Not rst Is Nothing Then
-
rst.Close: Set rst = Nothing
-
End If
-
Resume Exit_cmdCopyRename_Click
Thanks for your reply.
This code Works but only has changed one picture, not does the bucle.
I have to put this code on a command button into a form, don't I ?
Thanks and sorry for my "ignorance"
Jose
not does the bucle
Sorry, but I have no idea as to what you are talking about.
If it worked for one Picture then you probably do not have Values in the NewName Field for the corresponding OldName Values. You obviously cannot Copy and Rename a File if its New Name is not listed in the Articles Table unless you have some Default Renaming process inplace such as: - Base File name for [OldName] & "_2" & ".Extension"
Hi,
I will try to explain in other words:
I have an Access database with a table containing 3 fields: Student, OldExped, NewExped.
Also I have a folder where the images are stored (c: \ Images), which is now called as OldExp data field. For example: - Table STUDENTS:
-
Student OldExped NewExped
-
Smith, John 3567 1245676
-
Martin, Adam 3568 2878398
-
Presley, Elvis 3893 1365443
-
Etc.
C:\Images\3567.jpg
C:\Images\3568.jpg
C:\Images\3893.jpg
Etc.
So I need to put a command button on a form that runs a code to copy all the images in the folder c: \ Images to another folder c: \ NewImages and renowned as the NewExped field, considering the current folder almost 1000 images. This should be the result
C:\NewImages\1245676.jpg
C:\NewImages\2878398.jpg
C:\NewImages\1365443.jpg
Etc.
Thanks in advance for your patience
Jose
zmbd 5,501
Expert Mod 4TB
OK, would have been nice to know upfront that your data had only the file name without extension.
The way you worded your posts, it was implied that the field had the entire path stored.
ADezii has given you plenty of code to work with and an example of how to format the string.
What we need you to do is post your code, formatted with the [CODE/] button (it places the [code] [/code] tags around the script).
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ben |
last post by:
Greetings,
I am looking for a way to search for and delete files based on a pattern
mask. For example, the search method would find all files matching a
certain pattern containing wildcards (e.g....
|
by: Vidar Martinsen |
last post by:
Hi
I try to rename files, but I can't
I try to use the File Object, but there are no Rename function in the
object.
There is a Copy and Delete function, so:
Do I have to copy the file to a...
|
by: Alex |
last post by:
Subject: Looking for an XML (database-based) Query Reporting
Tool/advice
First off, let me apologize if this thread is somewhat off topic...
PLEASE REPLY TO: xml@solex-bi.com
I am looking...
|
by: dudufigueiredo |
last post by:
I have one folder containing mp3 files, the folder is:
C:\My Shared Folder\Rubber Soul
And the files are:
01 drive my car.mp3
02 norwegian wood.mp3
03 you won't see me.mp3
04 nowhere man.mp3...
|
by: cwhite |
last post by:
I'm having problems with a form based query
The user makes a selection from a drop box, there are only two choices:
Current
Former
the user makes a choice and clicks the preview report...
|
by: Rups |
last post by:
API FindFirstFile searches for files based on long and short names(8+3).
If i need to search files based on long names only .
How can i do ?Is there any other API which does search based on long...
|
by: SeanCly10 |
last post by:
Hi all. I don't want to sound like a complete idiot here, but I'm
somewhat limited in my coding knowledge, and I need some advice and
help.
I'm working on a database that will eventually be used...
|
by: ABC |
last post by:
How to rename files with support wildcard ?
|
by: MikeMikerson |
last post by:
Hello,
I am need to create a subform (no problem) of a form based query (a problem).
I need a form that will prompt the user to enter in a name, and the form will then display a query of the...
|
by: kalia |
last post by:
I have a text file and i want to Split the file into mulitple files based off the city and then create new files with the city name.
I am able to read the file and also chnaged the semi colon to a...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
| |