Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Searching files in sub-folders
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Contrôles ActiveX en VFP
Divers
Thread ID:
00806023
Message ID:
00806080
Vues:
17
I've a folder, which have many sub-folders. I want to check the files of all the folders and sub-folder and copy all the files one-by-one in other folder.

The easiest way to do this is to use the File System Object of Windows Script Host like so:
oFSO = CREATEOBJECT( "Scripting.FileSystemObject" )
oFSO.CopyFolder( 'd:\Fred', 'd:\Wilma' )
If you need to make some decisions on a file by file basis, you will need to gety all the files in an array using ADIR() and then check the array contents and make your decision about copying there.

You could create a 'copying' class with the required array properties and add a method called GetChildDirectories that you can call recursively to drill down into all the subdirectories to get the files like so:
LPARAMETERS tcDirectory

LOCAL lnDirCnt, laDirs[1], lnCnt, lnLen

*** add the current directory to the list
lnLen = IIF( TYPE( 'This.aDirectories[ 1 ]' # 'L', ALEN( This.aDirectories ) + 1, 1 )
DIMENSION This.aDirectories[ lnLen ]
This.aDirectories [ lnLen ] = tcDirectory

*** Now see if we have any directories under the current directory
lnDirCnt = ADIR( laDirs, tcDirectory + '*.*', 'D' )
IF lnDirCnt > 1
  FOR lnCnt = 1 TO lnDirCnt
    IF LEFT( laDirs[ lnCnt, 1 ], 1 ) # '.'
      *** Make sure we have a directory and not a file
      IF DIRECTORY( tcDirectory + laDirs[ lnCnt, 1 ] )
        This.GetChildDirectories( tcDirectory + laDirs[ lnCnt, 1 ] + '\')
      ENDIF
    ENDIF
  ENDFOR			
ENDIF	
You could also add a method called GetFileNames to get all the files in all of the subdirectories like so. This code also allows you to look for files with a single extension, so you could process, for example, only *.txt files if you wanted to.
lnDirCnt = ALEN( This.aDirectories )
FOR lnDir = 1 TO lnDirCnt
  *** Get all the files in the current direcotry
  lnFileCnt = ADIR( laFiles, This.aDirectories[ lnDir ] + '*.*' )
  *** Loop through the files and add the ones of the correct type to the array
  *** Column 1 is the file name and column 2 is the directory it is located in
  FOR lnFile = 1 TO lnFileCnt
    IF UPPER( ALLTRIM( JUSTEXT( laFiles[ lnFile, 1 ] ) ) ) == lcFileType
      lnTotalFiles = lnTotalFiles + 1
      DIMENSION This.aFileNames[ lnTotalFiles, 2 ]
      This.aFileNames[ lnTotalFiles, 1 ] = JUSTSTEM( laFiles[ lnFile, 1 ] )
      This.aFileNames[ lnTotalFiles, 2 ] = This.aDirectories[ lnDir ]
    ENDIF
  ENDFOR
ENDFOR		
HTH
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform