none
Copying Folders RRS feed

  • Question

  • Is there anyway to Copy entire folders along with their subfolders in Small basic? I tried using that File.CopyFile..but it would take too long to enter each file name
    Saturday, October 2, 2010 10:46 PM

Answers

  • This is how it can be done using SmallBasic and a subroutine using the stack.  A stack is a useful way to recursively process data.  Be careful with the File commands and make sure you don't delete or overwrite important data.  Obviously, this example needs a directory structure and files in "C:\temp\directory1".

    copyFrom = "C:\temp\directory1"
    copyTo = "C:\temp\directory2"

    copyDirs()

    Sub copyDirs
      'Set root directories
      rootFrom = copyFrom
      rootTo = copyTo
     
      'Put starting root directory on the stack
      stack.PushValue("directories",copyFrom)
      'Process the stack directories while there are some
      While (stack.GetCount("directories") > 0)
        'Process current directory - set from and to paths
        fromDir = Stack.PopValue("directories")
        toDir = rootTo+Text.GetSubTextToEnd(fromDir,Text.GetLength(rootFrom)+1)   
        'Create directory
        File.CreateDirectory(toDir)
        'TextWindow.WriteLine("DIRECTORY CREATED "+toDir)
        'Copy files
        files = File.GetFiles(fromDir)
        For i = 1 To Array.GetItemCount(files)
          fromFile = files[i]
          toFile = rootTo+Text.GetSubTextToEnd(fromFile,Text.GetLength(rootFrom)+1)   
          File.CopyFile(fromFile,toFile)
          'TextWindow.WriteLine("FILE COPIED "+toFile)
        EndFor
        'Update Stack with subdirectories to process as the stack progresses
        subDir = File.GetDirectories(fromDir)
        For i = 1 To Array.GetItemCount(subDir)
          Stack.PushValue("directories",subDir[i])
        EndFor
      EndWhile
    EndSub

    • Marked as answer by Effrego Wednesday, October 6, 2010 11:26 PM
    Tuesday, October 5, 2010 10:24 PM
    Moderator

All replies

  • You can download Data Extension and use my CMD object to use XCopy or Robocopy.

     


    Sorry My Bad English
    Sunday, October 3, 2010 8:04 AM
  • How do you use XCopy?
    Sunday, October 3, 2010 8:26 AM
  • For syntax see  http://en.wikipedia.org/wiki/Xcopy . Just add "/c " before CMD function.

    Here is small sample:

    QuationMark = TextPlus.GetCharacter(34)' Value of QuationMark  is "
    Command = "/c xcopy "+QuationMark+"C:\Test"+QuationMark+" "+QuationMark+"C:\TestClone"+QuationMark+" /s /e /i /h"

    'Value of Command is /c xcopy "C:\Test" "C:\TestClone" /s /e /i /h
    TextWindow.WriteLine(Command)
    TextWindow.WriteLine(CMD.RunCMDcommand(Command))


    Sorry My Bad English
    Sunday, October 3, 2010 9:18 AM
  • You can also iterate through folders and copy every item that is in the folder.
    • Proposed as answer by Jacob Brown Sunday, October 3, 2010 3:47 PM
    Sunday, October 3, 2010 3:47 PM
  • I tried :

    command =  "/c xcopy " +"C:\Hi.txt" +" " +"D:\Test" + " /s /e /i /h"
     CMD.RunCMDcommand(command)

    but nothing happens, but if I add

    TextWindow.WriteLine(Command)
    TextWindow.WriteLine(CMD.RunCMDcommand(Command))

    it works? Do I have to put this in everytime? I'm using GraphicWindow, and not TextWindow in my Program, and I don't want it to show up.

    Monday, October 4, 2010 2:48 AM
  • My sample works with Folders not Files. try make C:\Hi folder and add Hi.txt in C:\Hi .

    The change your command to "/c xcopy " +"C:\Hi" +" " +"D:\Test" + " /s /e /i /h"



    Sorry My Bad English
    Monday, October 4, 2010 10:54 AM
  • Yes thanks, but why doesn't it copy when I leave these 2 lines?

    TextWindow.WriteLine(Command)
    TextWindow.WriteLine(CMD.RunCMDcommand(Command))

    It doesn't seem to work when I haven't got those 2 lines, I'm making a graphical program so I don't want TextWindow to pop up.

     

    Tuesday, October 5, 2010 9:13 PM
  • This is how it can be done using SmallBasic and a subroutine using the stack.  A stack is a useful way to recursively process data.  Be careful with the File commands and make sure you don't delete or overwrite important data.  Obviously, this example needs a directory structure and files in "C:\temp\directory1".

    copyFrom = "C:\temp\directory1"
    copyTo = "C:\temp\directory2"

    copyDirs()

    Sub copyDirs
      'Set root directories
      rootFrom = copyFrom
      rootTo = copyTo
     
      'Put starting root directory on the stack
      stack.PushValue("directories",copyFrom)
      'Process the stack directories while there are some
      While (stack.GetCount("directories") > 0)
        'Process current directory - set from and to paths
        fromDir = Stack.PopValue("directories")
        toDir = rootTo+Text.GetSubTextToEnd(fromDir,Text.GetLength(rootFrom)+1)   
        'Create directory
        File.CreateDirectory(toDir)
        'TextWindow.WriteLine("DIRECTORY CREATED "+toDir)
        'Copy files
        files = File.GetFiles(fromDir)
        For i = 1 To Array.GetItemCount(files)
          fromFile = files[i]
          toFile = rootTo+Text.GetSubTextToEnd(fromFile,Text.GetLength(rootFrom)+1)   
          File.CopyFile(fromFile,toFile)
          'TextWindow.WriteLine("FILE COPIED "+toFile)
        EndFor
        'Update Stack with subdirectories to process as the stack progresses
        subDir = File.GetDirectories(fromDir)
        For i = 1 To Array.GetItemCount(subDir)
          Stack.PushValue("directories",subDir[i])
        EndFor
      EndWhile
    EndSub

    • Marked as answer by Effrego Wednesday, October 6, 2010 11:26 PM
    Tuesday, October 5, 2010 10:24 PM
    Moderator