none
Copy Directories RRS feed

  • Question

  • Is there an way to copy a directory, with all it's sub-directories and all files from one location to another in a small basic program? Or do I have to create each directory and sub-directory and use file.copy
    Wednesday, January 19, 2011 5:30 PM

Answers

  • You do have to process every file and directory, but you can write code to do it using the stack to recursively process subdirectories. Be VERY careful not to delete any directories or files - work with a test directory and take backups first.

    Here is a simple example using the stack to find all jpg images and put their paths in a text file.

    rootDir = Program.Directory ' Tested with this dir with plently of jpgs "C:\Users\Public\Pictures"
    outputFile = rootDir+"\piclist.txt"

    'Add root directory to the Stack as the first to check
    Stack.PushValue("dirs",rootDir)

    'Delete the output file first (if it exists)
    File.DeleteFile(outputFile)

    'Recursively find all jpg in current and all sub directories
    While (Stack.GetCount("dirs") > 0)
      'Current directly to be searched
      dirName = Stack.PopValue("dirs")
     
      'Find all files ending in jpg and add then to the output file
      files = File.GetFiles(dirName)
      For i = 1 To Array.GetItemCount(files)
        fileName = files[i]
        If (Text.EndsWith(Text.ConvertToLowerCase(fileName),".jpg")) Then
          File.AppendContents(outputFile, fileName)
        EndIf
      EndFor
     
      'Add all sub directories to the Stack to be handled as the stack unwinds
      subDir = File.GetDirectories(dirName)
      For i = 1 To Array.GetItemCount(subDir)
        Stack.PushValue("dirs",subDir[i])
      EndFor 
    EndWhile

    • Marked as answer by SFC-P33A Wednesday, January 19, 2011 7:56 PM
    Wednesday, January 19, 2011 7:09 PM
    Moderator

All replies

  • You do have to process every file and directory, but you can write code to do it using the stack to recursively process subdirectories. Be VERY careful not to delete any directories or files - work with a test directory and take backups first.

    Here is a simple example using the stack to find all jpg images and put their paths in a text file.

    rootDir = Program.Directory ' Tested with this dir with plently of jpgs "C:\Users\Public\Pictures"
    outputFile = rootDir+"\piclist.txt"

    'Add root directory to the Stack as the first to check
    Stack.PushValue("dirs",rootDir)

    'Delete the output file first (if it exists)
    File.DeleteFile(outputFile)

    'Recursively find all jpg in current and all sub directories
    While (Stack.GetCount("dirs") > 0)
      'Current directly to be searched
      dirName = Stack.PopValue("dirs")
     
      'Find all files ending in jpg and add then to the output file
      files = File.GetFiles(dirName)
      For i = 1 To Array.GetItemCount(files)
        fileName = files[i]
        If (Text.EndsWith(Text.ConvertToLowerCase(fileName),".jpg")) Then
          File.AppendContents(outputFile, fileName)
        EndIf
      EndFor
     
      'Add all sub directories to the Stack to be handled as the stack unwinds
      subDir = File.GetDirectories(dirName)
      For i = 1 To Array.GetItemCount(subDir)
        Stack.PushValue("dirs",subDir[i])
      EndFor 
    EndWhile

    • Marked as answer by SFC-P33A Wednesday, January 19, 2011 7:56 PM
    Wednesday, January 19, 2011 7:09 PM
    Moderator
  • Thanks Litdev,

    I'll give it a go

    Wednesday, January 19, 2011 7:17 PM
  • You do have to process every file and directory, but you can write code to do it using the stack to recursively process subdirectories. Be VERY careful not to delete any directories or files - work with a test directory and take backups first.

    Here is a simple example using the stack to find all jpg images and put their paths in a text file.

    rootDir = Program.Directory ' Tested with this dir with plently of jpgs "C:\Users\Public\Pictures"
    outputFile = rootDir+"\piclist.txt"

    'Add root directory to the Stack as the first to check
    Stack.PushValue("dirs",rootDir)

    'Delete the output file first (if it exists)
    File.DeleteFile(outputFile)

    'Recursively find all jpg in current and all sub directories
    While (Stack.GetCount("dirs") > 0)
      'Current directly to be searched
      dirName = Stack.PopValue("dirs")
     
      'Find all files ending in jpg and add then to the output file
      files = File.GetFiles(dirName)
      For i = 1 To Array.GetItemCount(files)
        fileName = files[i]
        If (Text.EndsWith(Text.ConvertToLowerCase(fileName),".jpg")) Then
          File.AppendContents(outputFile, fileName)
        EndIf
      EndFor
     
      'Add all sub directories to the Stack to be handled as the stack unwinds
      subDir = File.GetDirectories(dirName)
      For i = 1 To Array.GetItemCount(subDir)
        Stack.PushValue("dirs",subDir[i])
      EndFor 
    EndWhile

    Make the app save a list in the XML format.

    Joke!!!

    Thursday, January 20, 2011 7:46 PM