Newbie - How to move files from subdirs 
Author Message
 Newbie - How to move files from subdirs

Hi,

I want to move all the files in all the subdirectories (recursiv) in the
starting directory. After that, all subdirs should be deleted.

For example:

D:\Start
D:\Start\Sub1\file1.txt
D:\Start\Sub2\file2.txt
D:\Start\Sub2\SubSub2\file3.txt

After running the script, there should be just the D:\Start directory,
containing all the txt-files.

Any suggestions?

Thanx
Michael



Sat, 24 Dec 2005 04:32:13 GMT  
 Newbie - How to move files from subdirs
I wrote the following class to do similar stuff. The enum method can be used
to recursively get the files in a folder. Once this is done you can go
through the generated dictionary and use the movefile method of the
filesystem object to move the files. After the files are moved you can use
the deletefolder method to trash the folder.

'
'
'  Name:
'
'
'
'    FilesSystem.cls
'
'
'
'  Description:
'
'
'
'    Builds on the scripting file system object to provide additional
features               '
'
'
'  Properties:
'
'
'
'    none (yet)
'
'
'
'  Methods:
'
'
'
'    EnumFiles (folder,flist,recurse)
'
'
'
'      folder      a string containing the name of a folder to scan
'
'      flist       a dictionary object to contain the resulting file list
'
'      recurse     true to do all sub-folders, false to do just the named
folder             '
'
'
'    OpenFile (filename,iomode,create,timeout,tso)
'
'
'
'      filename    the name of the file to open
'
'      iomode      1=read 2=write 8=append (see Scripting.FileSystem ref)
'
'      create      true=create if not found (see Scripting.FileSystem ref)
'
'      timeout     number of seconds to wait id file not available
'
'      tso         returned text stream object (see Scripting.FileSystem
ref)                '
'
'
'      returns true if file was successfully opened, false otherwise
'
'
'
'  Notes:
'
'
'
'    This code does not clear the dictionary object so you can invoke the
FileList multiple  '
'    times on different folders and keep all of the file names in one place.
The dictionary  '
'    key is the fully qualified file name. The dictionary entry is the file
size in bytes.   '
'
'
'  Audit:
'
'
'
'    2003-01-16  jdeg  added Drive, Path, File, Extension functions
'
'    2003-01-07  jdeg  added OpenFile method
'
'    2003-01-06  jdeg  original code
'
'
'

Class clsFileSystem

   private m_fso

   Private Sub Class_Initialize()
      set m_fso = CreateObject("Scripting.FileSystemObject")
   End Sub

   Private Sub Class_Terminate()
      set m_fso = Nothing
   End Sub

   'add file names and sizes to a dictionary

   Public Function EnumFiles (folder , dic , recurse)

      dim file,subfolder

      if TypeName(dic) <> "Dictionary" then set dic =
CreateObject("Scripting.Dictionary")

      on error resume next

      for each file in m_fso.GetFolder(folder).Files
         dic(file.Path) = file.Size
      next

      if recurse then
         for each subfolder in m_fso.GetFolder(folder).SubFolders
            EnumFiles subfolder.Path,dic,recurse
         next
      end if

      on error goto 0

      EnumFiles = dic.Count

   End Function

   Public Function Drive ( filename )
      Drive = ucase(m_fso.GetDriveName(filename))
   End Function

   Public Function Path ( filename )
      Path = m_fso.GetParentFolderName(filename)
   End Function

   Public Function File ( filename )
      File = m_fso.GetFileName(filename)
   End Function

   Public Function Extension ( filename )
      Extension = m_fso.GetExtensionName(filename)
   End Function

   '
'
   'wait for a file to become available then open it
'
   '
'

   Public Function OpenFile ( filename , mode , create , byVal timeout ,
tso )

      'if the file doesn't exist and we aren't supposed to create it then
return false

      OpenFile = false
      if not m_fso.FileExists(filename) then
         if mode = 1 or not create then exit function
      end if

      'let's be optimistic and assume we'll eventually get it

      OpenFile = true
      on error resume next

      'keep going until we run out of time

      do while timer >= 0

         'try to open the file for append (will fail if another process is
currently writing)

         set tso = m_fso.OpenTextFile(filename,8,create)

         if err.Number = 0 then

            'we got the file - if we really want to append then just return

            if mode = 8 then Exit Function

            'otherwise close it and re-open with the desired mode

            tso.Close
            set tso = m_fso.OpenTextFile(filename,mode,create)
            if err.Number = 0 then Exit Function

         end if

         err.Clear

         'decrement the timer, wait one second then try again

         timeout = timeout - 1
         wscript.Sleep 1000

      loop

      'couldn't do it - return false

      OpenFile = false

   End Function

End Class



Sun, 25 Dec 2005 08:43:27 GMT  
 Newbie - How to move files from subdirs
Great! Thank you!!!

Michael



Quote:
> I wrote the following class to do similar stuff. The enum method can be
used
> to recursively get the files in a folder. Once this is done you can go
> through the generated dictionary and use the movefile method of the
> filesystem object to move the files. After the files are moved you can use
> the deletefolder method to trash the folder.

> '
> '
> '  Name:
> '
> '
> '
> '    FilesSystem.cls
> '
> '
> '
> '  Description:
> '
> '
> '
> '    Builds on the scripting file system object to provide additional
> features               '
> '
> '
> '  Properties:
> '
> '
> '
> '    none (yet)
> '
> '
> '
> '  Methods:
> '
> '
> '
> '    EnumFiles (folder,flist,recurse)
> '
> '
> '
> '      folder      a string containing the name of a folder to scan
> '
> '      flist       a dictionary object to contain the resulting file list
> '
> '      recurse     true to do all sub-folders, false to do just the named
> folder             '
> '
> '
> '    OpenFile (filename,iomode,create,timeout,tso)
> '
> '
> '
> '      filename    the name of the file to open
> '
> '      iomode      1=read 2=write 8=append (see Scripting.FileSystem ref)
> '
> '      create      true=create if not found (see Scripting.FileSystem ref)
> '
> '      timeout     number of seconds to wait id file not available
> '
> '      tso         returned text stream object (see Scripting.FileSystem
> ref)                '
> '
> '
> '      returns true if file was successfully opened, false otherwise
> '
> '
> '
> '  Notes:
> '
> '
> '
> '    This code does not clear the dictionary object so you can invoke the
> FileList multiple  '
> '    times on different folders and keep all of the file names in one
place.
> The dictionary  '
> '    key is the fully qualified file name. The dictionary entry is the
file
> size in bytes.   '
> '
> '
> '  Audit:
> '
> '
> '
> '    2003-01-16  jdeg  added Drive, Path, File, Extension functions
> '
> '    2003-01-07  jdeg  added OpenFile method
> '
> '    2003-01-06  jdeg  original code
> '
> '
> '

> Class clsFileSystem

>    private m_fso

>    Private Sub Class_Initialize()
>       set m_fso = CreateObject("Scripting.FileSystemObject")
>    End Sub

>    Private Sub Class_Terminate()
>       set m_fso = Nothing
>    End Sub

>    'add file names and sizes to a dictionary

>    Public Function EnumFiles (folder , dic , recurse)

>       dim file,subfolder

>       if TypeName(dic) <> "Dictionary" then set dic =
> CreateObject("Scripting.Dictionary")

>       on error resume next

>       for each file in m_fso.GetFolder(folder).Files
>          dic(file.Path) = file.Size
>       next

>       if recurse then
>          for each subfolder in m_fso.GetFolder(folder).SubFolders
>             EnumFiles subfolder.Path,dic,recurse
>          next
>       end if

>       on error goto 0

>       EnumFiles = dic.Count

>    End Function

>    Public Function Drive ( filename )
>       Drive = ucase(m_fso.GetDriveName(filename))
>    End Function

>    Public Function Path ( filename )
>       Path = m_fso.GetParentFolderName(filename)
>    End Function

>    Public Function File ( filename )
>       File = m_fso.GetFileName(filename)
>    End Function

>    Public Function Extension ( filename )
>       Extension = m_fso.GetExtensionName(filename)
>    End Function

>    '
> '
>    'wait for a file to become available then open it
> '
>    '
> '

>    Public Function OpenFile ( filename , mode , create , byVal timeout ,
> tso )

>       'if the file doesn't exist and we aren't supposed to create it then
> return false

>       OpenFile = false
>       if not m_fso.FileExists(filename) then
>          if mode = 1 or not create then exit function
>       end if

>       'let's be optimistic and assume we'll eventually get it

>       OpenFile = true
>       on error resume next

>       'keep going until we run out of time

>       do while timer >= 0

>          'try to open the file for append (will fail if another process is
> currently writing)

>          set tso = m_fso.OpenTextFile(filename,8,create)

>          if err.Number = 0 then

>             'we got the file - if we really want to append then just
return

>             if mode = 8 then Exit Function

>             'otherwise close it and re-open with the desired mode

>             tso.Close
>             set tso = m_fso.OpenTextFile(filename,mode,create)
>             if err.Number = 0 then Exit Function

>          end if

>          err.Clear

>          'decrement the timer, wait one second then try again

>          timeout = timeout - 1
>          wscript.Sleep 1000

>       loop

>       'couldn't do it - return false

>       OpenFile = false

>    End Function

> End Class



Mon, 26 Dec 2005 00:49:03 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Newbie - Move Files Across the network

2. Moving between objects???HELP NEWBIE

3. File security when move/copy files

4. if any files exist then move files using vbscript

5. Web folders and moving files to

6. move file to a folder...

7. Move PS file in GSView

8. How do I move data file from Server to Client

9. Moving files on the server via ASP

10. Using the FSO to move a file

11. I want to move files with ASP without giving the anonymous user write access

12. Move files to server

 

 
Powered by phpBB® Forum Software