LOCALIZAR ARQUIVO NA PASTA

PEGUDO 20/04/2010 09:28:43
#339770
Pessoal,
tenho um programa que faz backup automaticamente.

Eis meu código:

[txt-color=#0000f0]Private Sub [/txt-color]Main()
[txt-color=#0000f0]Dim[/txt-color] FileSistem [txt-color=#0000f0]As New [/txt-color]FileSystemObject
[txt-color=#0000f0]Dim[/txt-color] Pasta [txt-color=#0000f0]As[/txt-color] Folder
[txt-color=#0000f0]Dim[/txt-color] Arquivos [txt-color=#0000f0]As[/txt-color] Files
[txt-color=#0000f0]Dim[/txt-color] AccApp [txt-color=#0000f0]As[/txt-color] New Access.Application
[txt-color=#0000f0]Dim[/txt-color] Arquivo [txt-color=#0000f0]As[/txt-color] File, Backup As String, ListaDoBackup() As String
[txt-color=#0000f0]Dim[/txt-color] Mensagem [txt-color=#0000f0]As[/txt-color] VbMsgBoxResult
[txt-color=#0000f0] Dim [/txt-color]Resposta [txt-color=#0000f0]As[/txt-color] VbMsgBoxResult

Mensagem = MsgBox([Ô]O banco de dados será compactado. Deseja continuar?[Ô], vbYesNo, [Ô]Compactar[Ô])

[txt-color=#0000f0]Select Case [/txt-color]Mensagem
[txt-color=#0000f0] Case [/txt-color]vbYes
[txt-color=#007100][ô]Compacta o banco de dados principal e cria um backup compactado[/txt-color]
AccApp.CompactRepair App.Path & [Ô]\data.mdb[Ô], App.Path & [Ô]\data_backup.mdb[Ô], True

[txt-color=#007100][ô]Apaga o banco de dados principal[/txt-color]
Kill App.Path & [Ô]\data.mdb[Ô]

[txt-color=#007100][ô]Recupera o nome do backup do banco de dados[/txt-color]
[txt-color=#0000f0]Set [/txt-color]Arquivo = FileSistem.GetFile(App.Path & [Ô]\data_backup.mdb[Ô])

[txt-color=#007100] [ô]Renomeia o backup do banco de dados[/txt-color]
Arquivo.Name = [Ô]data.mdb[Ô]

[txt-color=#0000f0]set[/txt-color] AccApp = [txt-color=#0000f0]Nothing
End Select
End Sub[/txt-color]



Como eu faço para retornar, em uma variável, qualquer arquivo com a extenção [Ô].bkp[Ô]?

ROBSON220BASS 20/04/2010 10:55:50
#339777
uso isso para retornar fotos, talvez servirá pra vc.

Private Sub cmd_Click()

Dim filebox As OPENFILENAME
Dim fname As String
Dim result As Long
Dim stringIMG As String


With filebox
.lStructSize = Len(filebox)
.hwndOwner = Me.hwnd
.hInstance = 0
.lpstrFilter = [Ô]Arquivos de Imagens (*.bmp;*.jpg;*.jpeg;*.gif;*.ico) [Ô] & vbNullChar & [Ô]*.bmp;*.jpg;*.jpeg;*.gif;*.ico[Ô]
.nMaxCustomFilter = 0
.nFilterIndex = 1
.lpstrFile = Space(256) & vbNullChar
.nMaxFile = Len(.lpstrFile)
.lpstrFileTitle = Space(256) & vbNullChar
.nMaxFileTitle = Len(.lpstrFileTitle)
.lpstrTitle = [Ô]Escolha a imagem[Ô] & vbNullChar
.flags = OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY
.nFileOffset = 0
.nFileExtension = 0
.lCustData = 0
.lpfnHook = 0
End With
result = GetOpenFileName(filebox)
stringIMG = Mid(filebox.lpstrFile, 1, InStr(filebox.lpstrFile, vbNullChar) - 1)
If result = 0 Then
Exit Sub
Else
Image7.Picture = LoadPicture(stringIMG)
End If
End Sub


em um modulo:

[ô]---------------------------------------------------------------------------------------------------------------------------------
Public Declare Function GetOpenFileName Lib [Ô]comdlg32.dll[Ô] Alias [Ô]GetOpenFileNameA[Ô] (lpofn _
As OPENFILENAME) As Long

Public Declare Sub keybd_event Lib [Ô]user32[Ô] (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Global Fotos As String


Public Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustomFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Public Const OFN_FILEMUSTEXIST = &H1000
Public Const OFN_HIDEREADONLY = &H4
Public Const OFN_PATHMUSTEXIST = &H800

[ô]---------------------------------------------

Public Const F0_DELETE = &H3
Public Const F0F_ALLOWUNDO = &H40
Public Const FOF_CREATEPROGRESSDLG As Long = &H0

Public Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Boolean
hNameMappings As Long
lpszProgressTitle As String
End Type

Public Declare Function SHFileOperation Lib [Ô]shell32.dll[Ô] Alias [Ô]SHFileOperationA[Ô] (lpFileOp As SHFILEOPSTRUCT) As Long
PEGUDO 20/04/2010 12:23:20
#339783
Problema resolvido:

Utilizei uma collection para buscar todos os arquivos da pasta;
Joguei o conteúdo da collection para dentro de uma variável;
Fiz uma Split desta variável, separando os itens pelo delimitador [Ô]\[Ô];
Rodei um For...Next no Array criado;
Depois utilizei uma função Right(arquivo,4) para coletar apenas os arquivos que tivessem [Ô].bkp[Ô].

O código final ficou assim:

[txt-color=#0000f0]Set[/txt-color] Pasta = FileSistem.GetFolder(App.Path)
[txt-color=#0000f0]Set[/txt-color] Arquivos = Pasta.Files

[txt-color=#0000f0]For Each [/txt-color]Elemento [txt-color=#0000f0]In[/txt-color] Arquivos
Backup = Elemento
ListaDeBackup = Split(Backup, [Ô]\[Ô], , vbTextCompare)

[txt-color=#0000f0]For[/txt-color] i = [txt-color=#0000f0]LBound[/txt-color](ListaDeBackup) [txt-color=#0000f0]To UBound[/txt-color](ListaDeBackup)
[txt-color=#0000f0]If[/txt-color] Right(ListaDeBackup(i), 4) = [Ô].bkp[Ô] [txt-color=#0000f0]Then[/txt-color]
Backup = ListaDeBackup(i)
[txt-color=#0000f0]Exit For
End If
Next
If[/txt-color] Backup = ListaDeBackup(i) [txt-color=#0000f0]Then Exit For
Next[/txt-color]


Valeu a todos

Tópico encerrado , respostas não são mais permitidas