A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.
How do I work through files in a FileListBox and rename them?
Example: I have files that I don’t want others to see what type of files they are. Then I want to copy these files to an external device (programmatically). Currently I’m using the FileSystemObject to copy the files from my harddrive to the external device, the files on my harddrive is of an unknown type. On the external device I want to rename the file extention to make it usable.
How do I work through a list of files in a list box and change the file extentions?
How can you rename the file with the appropriate extension if you don't know the file type?
For example, my Firefox browser's cache has a lot of files in it with no extension - if I want to copy them to a thumb (usb) drive, and be able to use them in another application, I need to know what the hell they are. Some are GIF images, some are Shockwave Flash files - you get my drift.
Oh, I see what you're asking now - well, the only way I can think of is to use some external resource like www.wotsit.org - open the file for binary, check the initial x# bytes, and copy the file from the source to the destination with the appropriate file extension.
For example, GIF files have a HEADER block in the first 6 bytes - the ASCII string "GIF87a" or "GIF89a". If your file has these 6 bytes at the beginning, you do a:
filecopy "c:\myfiles\somefile" "e:\myfiles\somefile.gif"
But you will need to decipher the file format in order to give the destination file the correct file extension.
Monday, June 12, 2006
Use the FileSystemObject in the Microsoft Scripting Runtime and include scrrun.dll in your installation.
Dim FSO As FileSystemObject
Dim T As TextStream
Dim F As File
Set FSO = New FileSystemObject
If FSO.FileExists("C:\fsotest.txt") Then FSO.DeleteFile ("C:\fsotest.txt")
Set T = FSO.CreateTextFile("C:\testfso.txt", True)
T.WriteLine "Hello World"
Set F = FSO.GetFile("C:\testfso.txt")
F.Name = "fsotest.txt"
Set F = Nothing
Set T = FSO.OpenTextFile("C:\fsotest.txt")
Set T = Nothing
Set FSO = Nothing
I should've explained better.
I know what the file extentions are, but on the harddrive its an "invalid" extention since I don't want somebody opening files they shouldn't be opening.
Once the files get moved to the external device, the extensions must be set to valid.
As example: an MP3 player with MP3 files. On the harddrive the files must be of an unknown extension (say .xxx), once the mp3's are copied to the mp3 player (via USB), the files must be given back their proper extention to be able to work.
How would I do that?
Ha! Got it! solution is actually quite simple: it's just the change the file name. And this is how it's done:
(lbFile being the FileListBox)
(ttt is the old extention and mp3 is the new extention)
Dim iCnt As Integer
Dim sFilename As String
Dim sNewName As String
Dim fSys As FileSystemObject
Dim sMyFile As File
Set fSys = New FileSystemObject
For iCnt = 0 To lbFile.ListCount - 1
sFilename = lbFile.Path & "\" & lbFile.List(iCnt)
Set sMyFile = fSys.GetFile(sFilename)
sNewName = Replace(sFilename, "ttt", "mp3")
Set fSys = Nothing
This topic is archived. No further replies will be accepted.Other recent topics
Powered by FogBugz