Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FILE NAME CHANGES HELP!!! 4

Status
Not open for further replies.

kimmole

Technical User
May 9, 2002
49
GB
i'm trying to automate the naming of files from access...
i enter the path and file name of an image
eg... c:\pics\original\file001
and the database generates a new file name based on criteria entered into it...
so far so good, but i want to be able to click a button and have the database rename the file.......
how?
all ideas welcome..... thanks in advance... kimbo:)))
 
Hi!

Several ways of renaming files, I prefer using the filesystemobject. To use it, you need to set the reference to the sripting runtime library. How: In any module Tools | References, check the 'Microsoft Scripting Runtime' library). I'm a bit to conservative to use the rename method, but rather first copy it (perhaps checking whether there's 'nother file with the same name), then delete.

You're not elaborating much on the method you want to use for "automaticly" ranaming (how to generate the new name), som I'm just providing the methods. sNewName is the full path and "tweaked" name of the new file, sOldName is the full path and name of the original file.

[tt]dim fs as filesystemobject
dim sNewName as string
dim sOldName as string
set fs=new filesystemobject

if not fs.fileexist(sNewName) then
fs.copyfile sOldName,sNewName
fs.delete sOldName
else
msgbox "File alredy exists... try again"
end if
set fs=nothing[/tt]

HTH Roy-Vidar
 
thanks.... i'm using a concatenation of keywords to fill in a field in a table.... this field contains the path and new filename i want...
the way i see it working is for mw to place a number of image files in a folder ..say c:\images for renaming
then i go to the database ake a ll the entries relevant to each image and create the nnew name fo4r each of the images in the folder then when i click on a button i want the newly generated names to be applied to the relevant files...
ps i'm still using macros so maybe you could tell me exactly where i past the code... on the button or ake the button... on click run code...
thanks again ...sorry to be so dumb....

bestest... kimbo:))
 
Hmmmm... some of this I don't fully understand, some of this is beyond my skills or it's stuff I use 3'rd party tools to handle.

I'll try to give it a go, with the following limitations:
* creating an unbound form with an unbound control, a listbox and a button
* path of the files is "c:\images" (both for the old, and the new)
* providing a listbox with the filenames in that folder
* provide a way to rename selected file with name stored in a control on your form

You'll have to either experiment with this, buy book(s), post new threads to continue...

You're saying you're a macro-user. Hope you'll convert to VBA - when you're starting to get the hang of it, there are lot's of opportunities, easier maintainence, possibility of error handling...

OK - create a new form in design view (no wizard, no recordsource)
Save it, and use for instance the name "frmTestRename"

Turn of the control vizard (button no 2 on the toolbox)

Add a listbox control (you'll find it as button no 10 on the toolbox)
In the properties of the listbox (giving the tab name first, then the property):
(some of these values should be what is stated by default, but then just use them to check)
* other - name: lstFileName
* data - Row Source Type: Value List
* data - Bound Column: 1
* format - column count: 1
* format - heigth 5cm
* format - width: 5cm

Add a command button (button no 11 on the toolbox)
In the properties of the button
* other - name: cmbRename
* format - caption: &Rename selected file

Add a text control
In the properties of the text control
* other - name: txtNewName

Save the form. Now the "interesting part" begins. The coding. There are several "unknowns" here. We don't know yet what files reside in the specified path, the list doesn't have any rowsource... That'll be the first. I've decided to populate the list when the form opens. So for the form properties, select the event tab, find the on load event (that event is supposedly better for "data" thingies than the on open event). When invoking the event, select Code Builder. The "top" and "bottom" lines show up, and we're ready to start - but first - we'll need to store the initial path somewhere. I choose to declare a module level variable. At the top of the module, first check that the following lines are present (if they're not, paste them in - especially the second one is helpful, by "demanding" that any variables must be explicitly declared):

[tt]Option Compare Database
Option Explicit[/tt]

Just below those, type the following lines:
[tt]Private mstrPath As String
' Creates a variable available as long as the form is open[/tt]

Then enter the sub Form_Load, and paste the code below.

[tt] mstrPath = "c:\images\" ' "hardcoding" path
PopulateList ' calling routine to populate list[/tt]

Here, because I need to repopulate the list whenever a file is changed, I've put the code for populating the list in a separate module. Just place the cursor belowe any existing line in the module, and paste:

[tt]Private Sub PopulateList()
Dim fs As FileSystemObject
Dim fld As Folder
Dim fls As Files
Dim fl As File
Dim strListSource As String

Me!lstFileName.RowSource = "" ' Clearing previous rowsource

Set fs = New FileSystemObject
Set fld = fs.GetFolder(mstrPath)
Set fls = fld.Files

For Each fl In fls
' Getting all files, and assigning them to listbox
strListSource = strListSource & Chr(34) & fl.Name & Chr(34) & ","
Next

' Removing last comma (,) from string
' NOTE
' Some countries regional settings uses other separators
' than comma (,), my country included, we use semicolon (;)
' If so, change the comma to your separator in the line
' starting with
' strListSource = strListSource &...

strListSource = Mid$(strListSource, 1, Len(strListSource) - 1)

Set fs = Nothing
Set fld = Nothing
Set fls = Nothing
Set fl = Nothing
Me!lstFileName.RowSource = strListSource
End Sub[/tt]

Explaining the object types and methods will take up to much space. I've commented some on the building of the listbox rowsource. Hit F1 to get help on whatever you wan't to know more about.

Then there's the "renaming" code. This will be almost the same as in previous post, xcept some additional testing. On the rename buttons on click event, paste the following code:

[tt] Dim fs As FileSystemObject
Set fs = New FileSystemObject

If Len(Me!lstFileName & "") > 0 And Len(Me!txtNewName & "") > 0 Then
' Checking if a filename is selected, and there's a value for new name
If Not fs.FileExists(mstrPath & Me!txtNewName) Then
' checking if a file with the same name alredy exist
fs.CopyFile mstrPath & Me!lstFileName, mstrPath & Me!txtNewName
' copy selected file
fs.DeleteFile mstrPath & Me!lstFileName
' deleting original file
PopulateList
' Populating the list again with new names
Else
MsgBox "A file with the that name alredy exists!", _
vbExclamation, "File exists"
End If
Else
MsgBox "You must select a file and/or" & vbCr & vbLf & _
"type a new name first!", _
vbExclamation, "Select a file!"
End If
Set fs = Nothing
[/tt]

You should perhaps add some error handling too.

Now, this probably doesn't solve your challenge, but I hope it might be considered somewhere to start:)

Roy-Vidar
 
thanks again...... this forum is great.

but.....here is how i need to work it.. i load the images to a given location ...always the same, then i open the database and enter the creators details on a form with a sub form related one to many
which contains the image details... i then enter the file names which i have just placed in the folder. then i click a buton which runs an update query to create the new file name and path...(so far all this works fine)... what i now need to do is tag onto the end of this a command to alter the name of the file in the folder.
afterwards i will edit the files to a new location and the originals will be deleted.


thanks again and hope what i'm trying to do makes more sence now.

ps how do i get an image box to load the image into the form when its inits new location... the path will always be constant and the image names unique?

cheers.... kimbo:)))
 
Hi again!

Lets hope someone else visits this thread. I think we've come (hmmm... rather I've come) to the "...some of this I don't fully understand, some of this is beyond my skills or it's stuff I use 3'rd party tools to handle." part, as stated above;-)

Roy-Vidar
 
woops ... thanks again roy and this time i remembered the star...
bestest... kimbo:)))
 
Roy,

Great post...one of the best I've seen you do yet. This is working miracles for me.

Thanks!

-Patrick

Nine times out of ten, the simplest solution is the best one.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top