Ver Mensaje Individual
  #4 (permalink)  
Antiguo 17/01/2010, 09:11
Avatar de pkj
pkj
 
Fecha de Ingreso: julio-2006
Ubicación: Órbita sincrónica
Mensajes: 899
Antigüedad: 17 años, 9 meses
Puntos: 29
Respuesta: como copiar carpetas en visual basic

EDITADO

Como es algo que tambien a mi me interesaba he buscado un poco más y he modificado otro código para crear una función parecida al XCopy de MS-DOS:

Código vb:
Ver original
  1. Function XCopy(srcPath As String, dstPath As String, Optional FilePat As String = "*.*", Optional IncludeSubDirs As Boolean = True, Optional Sobreescribir As Boolean = True) As Integer
  2.  
  3.   ' Ejmp:
  4.  ' XCopy "c:\p1", "d:\p1"
  5.  
  6.   ' funciona tambien en red:
  7.  ' XCopy "//PC001/C/p1", "//PC002/C/p1"
  8.  
  9.  
  10.   Const ATTR_DIRECTORY = 16
  11.  
  12.   Dim DirOK As Integer, i As Integer
  13.   Dim DirReturn As String
  14.   ReDim d(1) As String
  15.   Dim dCount As Integer
  16.   Dim CurrFile$
  17.   Dim CurrDir$
  18.   Dim dstPathBackup As String
  19.   Dim f%
  20.  
  21.   On Error Resume Next
  22.  
  23.   MkDir dstPath
  24.  
  25.   If InStr(1, srcPath, "\") Or InStr(1, srcPath, ":") Then
  26.     If Right(srcPath, 1) <> "\" Then srcPath = srcPath & "\"
  27.   ElseIf InStr(1, srcPath, "/") Then
  28.     If Right(srcPath, 1) <> "/" Then srcPath = srcPath & "/"
  29.   End If
  30.   If InStr(1, dstPath, "\") Or InStr(1, dstPath, ":") Then
  31.     If Right(dstPath, 1) <> "\" Then dstPath = dstPath & "\"
  32.   ElseIf InStr(1, dstPath, "/") Then
  33.     If Right(dstPath, 1) <> "/" Then dstPath = dstPath & "/"
  34.   End If
  35.    
  36.   On Error GoTo DirErr
  37.    
  38.   CurrDir$ = CurDir$ ' directorio actual de trabajo
  39.  srcPath = UCase$(srcPath)
  40.   dstPath = UCase$(dstPath)
  41.  
  42.   dstPathBackup = dstPath ' guardamos el directorio destino
  43.  
  44.   ' Iniciamos variables para mantener los nombres de archivos
  45.  DirReturn = Dir(srcPath & "*.*", ATTR_DIRECTORY)
  46.    
  47.   ' Buscamos todos los Subdirectorios
  48.  Do While DirReturn <> ""
  49.     ' aseguramos que no se haga nada con "." y ".."
  50.    If DirReturn <> "." And DirReturn <> ".." Then
  51.       If (GetAttr(srcPath & DirReturn) And ATTR_DIRECTORY) = ATTR_DIRECTORY Then
  52.         ' agregamos a la lista de directorios
  53.        dCount = dCount + 1
  54.         ReDim Preserve d(dCount)
  55.         d(dCount) = srcPath & DirReturn
  56.       End If
  57.     End If
  58.     DirReturn = Dir
  59.   Loop
  60.    
  61.   ' ahora hacemos que los archivos que coicidan
  62.  DirReturn = Dir(srcPath & FilePat, 0)
  63.  
  64.   ' Buscamos todos los archivos
  65.  Do While DirReturn <> ""
  66.     ' aseguramos que no es directorio
  67.    If Not ((GetAttr(srcPath & DirReturn) And ATTR_DIRECTORY) = ATTR_DIRECTORY) Then
  68.       ' es un archivo y se copia
  69.      
  70.       'Si existe miramos si se sobre-escribe
  71.      On Error Resume Next
  72.       f% = FreeFile
  73.       Open dstPath & DirReturn For Input As #f%
  74.       Close #f%
  75.       If Err <> 0 Or Sobreescribir = True Then
  76.         FileCopy srcPath & DirReturn, dstPath & DirReturn
  77.       End If
  78.     End If
  79.     DirReturn = Dir
  80.   Loop
  81.  
  82.    ' Ahora hacemos los subdirectorios
  83.  For i = 1 To dCount
  84.     If IncludeSubDirs Then
  85.       On Error GoTo PathErr
  86.       dstPath = dstPath & Right$(d(i), Len(d(i)) - Len(srcPath))
  87.       ' si el path no existe lo creamos
  88.      ChDir dstPath
  89.       On Error GoTo DirErr
  90.     Else
  91.       XCopy = True
  92.       GoTo ExitFunc
  93.     End If
  94.     DirOK = XCopy(d(i), dstPath, FilePat, IncludeSubDirs, Sobreescribir)
  95.     ' Reiniciamos dstPath al valor asignado
  96.    dstPath = dstPathBackup
  97.   Next
  98.  
  99.   XCopy = True
  100.  
  101. ExitFunc:
  102.   ChDir CurrDir$
  103.   Exit Function
  104. DirErr:
  105.   MsgBox "Error: " & Error$(Err)
  106.   XCopy = False
  107.   Resume ExitFunc
  108. PathErr:
  109.   If Err = 75 Or Err = 76 Then ' si no encontramos el path
  110.    MkDir dstPath
  111.     Resume Next
  112.   End If
  113.   GoTo DirErr
  114. End Function
  115.  
  116. Private Sub Command1_Click()
  117.   XCopy "//PC001/C/reporte diario", "//PC002/C/reporte diario"
  118. End Sub

Saludos
__________________
No hay preguntas tontas, solo gente estup..., ¡No!, ¿como era? No hay gente que pregunte a tontos... ¡Nooo!... ¡Vaya cabeza!

Última edición por pkj; 17/01/2010 a las 10:54 Razón: Mejorar