a3deluxe Posted August 13, 2013 at 09:43 PM Report #521659 Posted August 13, 2013 at 09:43 PM Boas, Queria adicionar a este código, se o If Not Directory.Exists, não fazer nada. porque o directory de destino é uma pendrive, se a Pendrive não existir ele dá erro. queria que se a Pendrive não existir ele não desse erro e continua-se. // If Directory.Exists(BackupInPath) Then If Not Directory.Exists(BackupOutPath2) Then Directory.CreateDirectory(BackupOutPath2) For Each F As String In Directory.GetFiles(BackupInPath) File.Copy(F, Path.GetFullPath(BackupOutPath2) & "\" & Path.GetFileName(F), True) Next End If Obrigado
cdaniel.marques Posted August 13, 2013 at 09:49 PM Report #521662 Posted August 13, 2013 at 09:49 PM (edited) Experimenta assim: If Directory.Exists(BackupInPath) Then If Directory.Exists("f:\") Then 'Verfica se a Drive F Existe, só executa código se existir If Not Directory.Exists(BackupOutPath2) Then Directory.CreateDirectory(BackupOutPath2) For Each F As String In Directory.GetFiles(BackupInPath) File.Copy(F, Path.GetFullPath(BackupOutPath2) & "\" & Path.GetFileName(F), True) Next End If End If Edited August 13, 2013 at 09:54 PM by cdaniel.marques
a3deluxe Posted August 13, 2013 at 10:16 PM Author Report #521665 Posted August 13, 2013 at 10:16 PM mais uma vez obrigado pela ajuda cdaniel.marques funcionou! É possivel de se fazer, quando coloca a pendrive ele por exemplo atribui a letra G, ele mudar para a letra F? isto porque nem sempre ele atribui a mesma letra , neste caso a letra F. abracos
cdaniel.marques Posted August 13, 2013 at 10:53 PM Report #521670 Posted August 13, 2013 at 10:53 PM (edited) Não sei se é a melhor maneira da fazer 😕 , mas deixo a sugestão: Criar uma array com a Slot's mais provaveis e percorrer com um ciclo For Each ' Array Drives Dim letras() As String = {"f:\", "g:\", "h:\", "i:\"} If Directory.Exists(BackupInPath) Then For Each drive As String In letras 'Percorre as letras do array num ciclo For Each If Directory.Exists(drive) Then 'Verfica se a Drive F Existe, só executa código se existir If Not Directory.Exists(BackupOutPath2) Then Directory.CreateDirectory(BackupOutPath2) For Each F As String In Directory.GetFiles(BackupInPath) File.Copy(F, Path.GetFullPath(BackupOutPath2) & "\" & Path.GetFileName(F), True) Next End If Next End If Edited August 13, 2013 at 11:00 PM by cdaniel.marques
Tiago Simões Marques Posted August 14, 2013 at 09:07 AM Report #521694 Posted August 14, 2013 at 09:07 AM Sugestão: Porque é que não fazes um dialog para quando o utilizador for fazer o backup seleccionar a pen drive, e se a estrutura de backup não existir ele criar, porque ser tiveres varias pen's no mesmo pc vai-te dar problemas. Espero que ajude, Cumprimentos, Tiago Simões Marques
a3deluxe Posted August 14, 2013 at 09:44 AM Author Report #521705 Posted August 14, 2013 at 09:44 AM vou tentar com a solução do cdaniel.marques Quanto a fazes um dialog, não dá porque o programa em questão arranca com o windows e faz a copia de ficheiros para a pendrive automaticamente.
nelsonr Posted August 14, 2013 at 09:55 AM Report #521708 Posted August 14, 2013 at 09:55 AM Ou então colocas um ficheiro identificador na raiz da pendrive e a aplicação procura as várias drives existentes e usa a que encontrar esse ficheiro.
a3deluxe Posted August 14, 2013 at 10:03 AM Author Report #521709 Posted August 14, 2013 at 10:03 AM nelsonr essa ideia era optima. como posso fazer? Cumprimentos
nelsonr Posted August 14, 2013 at 10:24 AM Report #521710 Posted August 14, 2013 at 10:24 AM Agora não tenho aqui o VB.NET para testar, mas basicamente depois de "If Directory.Exists(drive) Then" podes ter uma linha do tipo: If File.Exists(String.Format("{0}backup.ini", drive)) then ' ...
a3deluxe Posted August 14, 2013 at 10:28 AM Author Report #521712 Posted August 14, 2013 at 10:28 AM vou pesquisar por esse código que me indicas-te. obrigado depois posto aqui o que consegui fazer.
Tiago Simões Marques Posted August 14, 2013 at 10:31 AM Report #521715 Posted August 14, 2013 at 10:31 AM Sugestão: Fazes um ficheiro txt e colocas na pen. Depois com o código que já tens fazes um if para cada ficheiro: If File.Exists("H:\Validador.txt") Then ElseIf File.Exists("G:\Validador.txt") then End If Espero que me esteja a fazer entender Tiago Simões Marques
a3deluxe Posted August 14, 2013 at 10:54 AM Author Report #521719 Posted August 14, 2013 at 10:54 AM Testei com este código para ver o resultado. // ' Array Drives Dim letras() As String = {"f:\", "g:\", "h:\", "i:\"} If Directory.Exists(BackupInPath) Then For Each drive As String In letras 'Percorre as letras do array num ciclo For Each If Directory.Exists(drive) Then 'Verfica se a Drive F Existe, só executa código se existir If Not Directory.Exists(BackupOutPath2) Then Directory.CreateDirectory(BackupOutPath2) For Each F As String In Directory.GetFiles(BackupInPath) File.Copy(F, Path.GetFullPath(BackupOutPath2) & "\" & Path.GetFileName(F), True) Next End If Next End If se a drive F existir, funciona bem depois mudei a letra da drive de F para G e já deu este erro. // If Not Directory.Exists(BackupOutPath2) Then Directory.CreateDirectory(BackupOutPath2) não encontrou a drive G dentro da pen existe um ficheiro mas não tem extensao, vou utilizar o proprio ficheiro que é copiado. // If File.Exists("H:\filebackup") Then ElseIf File.Exists("G:\filebackup") then End If
Tiago Simões Marques Posted August 14, 2013 at 11:11 AM Report #521722 Posted August 14, 2013 at 11:11 AM Já viste o valor que esta a passar na variavel: BackupOutPath2 Podes informar qual é? Tiago Simões Marques
a3deluxe Posted August 14, 2013 at 11:29 AM Author Report #521726 Posted August 14, 2013 at 11:29 AM qual é o valor? a variavel é esta // Public BackupInPath As String = "J:\Data" Public BackupOutPath2 As String = "d:\Data"
cdaniel.marques Posted August 14, 2013 at 01:10 PM Report #521734 Posted August 14, 2013 at 01:10 PM (edited) qual é o valor? // Public BackupInPath As String = "J:\Data" Public BackupOutPath2 As String = "d:\Data" A que se refere a variavel BackupInPath (Drive J)? Uma pen, um disco fisico, uma pen que tu não sabes que drive vai usar? O mesmo para BackupOutPath2 (Drive D). É um disco sempre com a mesma letra, uma pen com várias letras possiveis? Edited August 14, 2013 at 01:10 PM by cdaniel.marques
Tiago Simões Marques Posted August 14, 2013 at 01:42 PM Report #521738 Posted August 14, 2013 at 01:42 PM (edited) Public BackupInPath As String = "Data" Public BackupOutPath2 As String = "Data" Depois quando tiver a fazer o ciclo tens que colocar a letra & "\" File.Copy(F, Path.GetFullPath(Letra & "\" & BackupOutPath2) & "\" & Path.GetFileName(F), True) Edited August 14, 2013 at 01:44 PM by Tiago Simões Marques Tiago Simões Marques
a3deluxe Posted August 14, 2013 at 02:19 PM Author Report #521742 Posted August 14, 2013 at 02:19 PM Sim o Backupinpath e sempre a mesma (drive J) a variavel BackupOupath2 e varia, por ser penusb. qual a melhor maneira de fazer? cumprimentos
Tiago Simões Marques Posted August 14, 2013 at 02:40 PM Report #521746 Posted August 14, 2013 at 02:40 PM Public BackupInPath As String = "J:\Data" Public BackupOutPath2 As String No ciclo colocas BackupOutPath2 = Letra & "\" & "Data" Tiago Simões Marques
a3deluxe Posted August 15, 2013 at 12:26 AM Author Report #521781 Posted August 15, 2013 at 12:26 AM Tiago Simões Marques onde coloco o // BackupOutPath2 = Letra & "\" & "Data" e esta variael fica assim: // Public BackupOutPath2 As String
nelsonr Posted August 15, 2013 at 10:30 AM Report #521791 Posted August 15, 2013 at 10:30 AM Boas, isso já está grande confusão. Fiz um exemplo completo, vê se ajuda. O que este código faz é passar por todas as drives existentes, e caso encontre a pasta definida para destino de backups (ex:F:\Backups), copia os ficheiros da pasta definida como origem ' Indicar a pasta que se pretende fazer segurança (origem) Dim caminhoOrigem As String = "C:\Dados" ' Indicar a pasta que tem de existir na drive onde vai colocar os backups Dim pastaDestino As String = "Backups" ' Se o caminho origem existir If Directory.Exists(caminhoOrigem) Then ' Passar em todas as drives disponíveis For Each drive As DriveInfo In DriveInfo.GetDrives() ' Verificar se existe na drive atual a pasta para guardar as seguranças (ex: D:\Backups) If Directory.Exists(String.Format("{0}{1}", drive.RootDirectory, pastaDestino)) Then ' Passa por todos os ficheiros na pasta de origem For Each ficheiro As String In Directory.GetFiles(caminhoOrigem) ' Copia da pasta origem para a pasta destino File.Copy(ficheiro, String.Format("{0}{1}\{2}", drive.RootDirectory, pastaDestino, Path.GetFileName(ficheiro))) Next ' Como já encontrou a drive destino, sai do ciclo de drives Exit For End If Next End If
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now