Se connecter avec
S'enregistrer | Connectez-vous

test fichier ouvert

Dernière réponse : dans Programmation

Salut à tous,

J'ai une fonction copier/couper en vba et pour la partie couper, j'ai besoin de tester si le fichier est ouvert.
Voici le code
  1. 'The function copy_files carries out a copy/cut of a tree structure of files in only one folder, and it sends the number of file it copies/cuts.
  2. 'srcfolder is the folder of the source of the tree structure.
  3. 'destfolder is the destination's folder.
  4. 'subfolders is a boolean who allows the transaction in the sub folders of the source folder.
  5. 'suppr is a bolean who is allows the cut of the transaction
  6. Function copy_files(srcFolder As String, destFolder As String, subFolders As Boolean, suppr As Boolean) As Integer
  7.  
  8. On Error GoTo errHandler
  9.  
  10. Dim fso As New FileSystemObject
  11. Dim theFile As file
  12. Dim theFiles As Files
  13. Dim theFolder As Folder
  14. Dim theSubFolders As Folders
  15.  
  16. Set theFolder = fso.GetFolder(srcFolder)
  17. Set theFiles = theFolder.Files
  18. Set theSubFolders = theFolder.subFolders
  19.  
  20. If theFiles.Count > 0 Then
  21.  
  22. copy_files = theFiles.Count
  23. For Each theFile In theFiles
  24. If suppr Then
  25. fso.MoveFile theFile, destFolder & "\"
  26. Else
  27. fso.CopyFile theFile, destFolder & "\"
  28. End If
  29. Next
  30.  
  31. End If
  32.  
  33. Set theFile = Nothing
  34. Set theFiles = Nothing
  35. Set theFolder = Nothing
  36. Set fso = Nothing
  37.  
  38. If theSubFolders.Count > 0 And subFolders Then
  39.  
  40. For Each theFolder In theSubFolders
  41. If suppr Then
  42. copy_files = copy_files + copy_files(theFolder.path, destFolder, True, True)
  43. Else
  44. copy_files = copy_files + copy_files(theFolder.path, destFolder, True, False)
  45. End If
  46. Next
  47.  
  48. End If
  49.  
  50. Set theSubFolders = Nothing
  51.  
  52. Exit Function
  53.  
  54. If Not Error Then
  55. GoTo fin
  56. End If
  57.  
  58. errHandler:
  59.  
  60. copy_files = -err.Number
  61.  
  62. fin:
  63.  
  64. End Function


Donc comment faire pour tester le fichier?


Merci

Autres pages sur : test fichier ouvert

Lassé par la pub ? Créez un compte

par contre j'ai un pb avec le code, il veut pas compiler, voici le nouveau code :
  1. 'The function copy_files carries out a copy/cut of a tree structure of files in only one folder, and it sends the number of file it copies/cuts.
  2. 'srcfolder is the folder of the source of the tree structure.
  3. 'destfolder is the destination's folder.
  4. 'subfolders is a boolean who allows the transaction in the sub folders of the source folder.
  5. 'suppr is a bolean who is allows the cut of the transaction
  6. Function copy_files(srcFolder As String, destFolder As String, subFolders As Boolean, suppr As Boolean) As Integer
  7.  
  8. On Error GoTo errHandler
  9.  
  10. Dim fso As New FileSystemObject
  11. Dim theFile As file
  12. Dim theFiles As Files
  13. Dim theFolder As Folder
  14. Dim theSubFolders As Folders
  15. Dim n
  16.  
  17. Set theFolder = fso.GetFolder(srcFolder)
  18. Set theFiles = theFolder.Files
  19. Set theSubFolders = theFolder.subFolders
  20.  
  21. If theFiles.Count > 0 Then
  22.  
  23. copy_files = theFiles.Count
  24. For Each theFile In theFiles
  25. If suppr Then
  26. n = fso.GetFileName(theFile)
  27. If IsFileOpen(n) Then
  28. MsgBox "fichier en cours d'utilisation"
  29. Else
  30. fso.MoveFile theFile, destFolder & "\"
  31. End If
  32. Else
  33. fso.CopyFile theFile, destFolder & "\"
  34. End If
  35. Next
  36.  
  37. End If
  38.  
  39. Set theFile = Nothing
  40. Set theFiles = Nothing
  41. Set theFolder = Nothing
  42. Set fso = Nothing
  43.  
  44. If theSubFolders.Count > 0 And subFolders Then
  45.  
  46. For Each theFolder In theSubFolders
  47. If suppr Then
  48. copy_files = copy_files + copy_files(theFolder.path, destFolder, True, True)
  49. Else
  50. copy_files = copy_files + copy_files(theFolder.path, destFolder, True, False)
  51. End If
  52. Next
  53.  
  54. End If
  55.  
  56. Set theSubFolders = Nothing
  57.  
  58. Exit Function
  59.  
  60. If Not Error Then
  61. GoTo fin
  62. End If
  63.  
  64. errHandler:
  65.  
  66. copy_files = -err.Number
  67.  
  68. fin:
  69.  
  70. End Function
Lassé par la pub ? Créez un compte