473,396 Members | 1,929 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,396 software developers and data experts.

Rename files from VBA based on a query

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
Jan 21 '14 #1
10 6744
zmbd
5,501 Expert Mod 4TB
Use the oldschool NAME() (click me (^_^) )

You may find this article handy too: http://bytes.com/topic/access/insigh...i-o-statements


BTW: This is one reason one should not use "Name," "Dir," "Date," etc.. as field names:
Access 2007 reserved words and symbols
AllenBrowne- Problem names and reserved words in Access
Jan 21 '14 #2
ADezii
8,834 Expert 8TB
The basic concept is as follows:
  1. Create a Recordset based on the Articles Table.
  2. Loop thru the Recordset and determine if the [OldName] Field is NULL, if it is skip this Record.
  3. 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.
  4. Assuming both conditions are True, copy the File [OldFile] to the same Folder renaming it [NewFile]
  5. Demo Code follows to get you started, any questions feel free to ask.
    Expand|Select|Wrap|Line Numbers
    1. Const conIMAGE_PATH As String = "C:\Images\"
    2. Dim MyDB As DAO.Database
    3. Dim rst As DAO.Recordset
    4.  
    5. Set MyDB = CurrentDb
    6. Set rst = MyDB.OpenRecordset("SELECT * FROM Articles", dbOpenForwardOnly)
    7.  
    8. With rst
    9.   Do While Not .EOF
    10.     'Does the File exist in C:\Images and is there a NewName for the File?
    11.     If Dir(conIMAGE_PATH & ![OldName]) <> "" And Not IsNull(![NewName]) Then
    12.       'Copy Source to Destination essentially making a Copy of the File ([OldName]
    13.       'in the same Folder but under a New Name ([NewName])
    14.       FileCopy conIMAGE_PATH & ![OldName], conIMAGE_PATH & ![NewName]
    15.     End If
    16.       .MoveNext
    17.   Loop
    18. End With
    19.  
    20. rst.Close
    21. Set rst = Nothing
  6. Given the following Articles Table only Files 1.bmp, 4.bmp, and 5.bmp will be copied/renamed.
    Expand|Select|Wrap|Line Numbers
    1. ID    OldName    NewName
    2. 1     1.bmp      One.bmp
    3. 2     2.bmp    
    4. 3                Three.bmp
    5. 4     4.bmp      Four.bmp
    6. 5     5.bmp      Five.bmp
    7. 6     6.bmp    
    8.  
Jan 21 '14 #3
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.
Attached Files
File Type: zip Images.zip (324.1 KB, 274 views)
Jan 22 '14 #4
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.
Jan 22 '14 #5
ADezii
8,834 Expert 8TB
The Code has been tested and is fully operational.
  1. Is your Table named Articles and does it contain two TEXT Fields named OldName and NewName?
  2. Does the Folder Images exist in the Root Directory of Drive C:, namely C:\Images\?
  3. 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?
  4. Do you have corresponding NewName Values in the Articles Table for at least some of their counterparts, as in:
    Expand|Select|Wrap|Line Numbers
    1. MyPicture.jpg ==> MyPicture_2.jpg
  5. 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?
  6. 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.
  7. Let us know how you make out.
    Expand|Select|Wrap|Line Numbers
    1. On Error GoTo Err_cmdCopyRename_Click
    2. Const conIMAGE_PATH As String = "C:\Images\"
    3. Dim MyDB As DAO.Database
    4. Dim rst As DAO.Recordset
    5.  
    6. Set MyDB = CurrentDb
    7. Set rst = MyDB.OpenRecordset("SELECT * FROM Articles", dbOpenForwardOnly)
    8.  
    9. If (rst.BOF And rst.EOF) Then
    10.   MsgBox "There are no Files to Copy", vbExclamation, "No Files Fouond"
    11.     Exit Sub
    12. End If
    13.  
    14. With rst
    15.   Do While Not .EOF
    16.     If Not IsNull(![OldName]) Then
    17.       'Does the File exist in C:\Images and is there a NewName for the File?
    18.       If Dir(conIMAGE_PATH & ![OldName]) <> "" And Not IsNull(![NewName]) Then
    19.         'Copy Source to Destination essentially making a Copy of the File ([OldName]
    20.         'in the same Folder but under a New Name ([NewName])
    21.         FileCopy conIMAGE_PATH & ![OldName], conIMAGE_PATH & ![NewName]
    22.       End If
    23.     End If
    24.       .MoveNext
    25.   Loop
    26. End With
    27.  
    28. MsgBox "File Copy/Rename Operation successful", vbInformation, "Process Completed"
    29.  
    30. rst.Close
    31. Set rst = Nothing
    32.  
    33. Exit_cmdCopyRename_Click:
    34.   Exit Sub
    35.  
    36. Err_cmdCopyRename_Click:
    37.   MsgBox Err.Description, vbExclamation, "Error in cmdCopyRename_Click()"
    38.     If Not rst Is Nothing Then
    39.       rst.Close: Set rst = Nothing
    40.     End If
    41.       Resume Exit_cmdCopyRename_Click
Jan 22 '14 #6
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
Jan 22 '14 #7
ADezii
8,834 Expert 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. Base File name for [OldName] & "_2" & ".Extension"
Jan 22 '14 #8
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:
Expand|Select|Wrap|Line Numbers
  1. Table STUDENTS:  
  2. Student            OldExped    NewExped
  3. Smith, John    3567        1245676
  4. Martin, Adam    3568        2878398
  5. Presley, Elvis    3893        1365443
  6. 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
Jan 23 '14 #9
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).
Jan 23 '14 #10
Thank you for your help
Jul 6 '18 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

11
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....
1
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...
2
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...
16
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...
2
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...
1
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...
5
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...
11
by: ABC | last post by:
How to rename files with support wildcard ?
1
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
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
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
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,...

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.