Jump to content

Recommended Posts

Posted

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

  • Replies 72
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted (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 by cdaniel.marques
Posted (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 by cdaniel.marques
Posted

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
' ...
Posted

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
Posted (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 by cdaniel.marques
Posted (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 by Tiago Simões Marques

Tiago Simões Marques

Posted

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.