Sign in to follow this  
Followers 0
pedrosojones

Need help with FileMove

6 posts in this topic

Hi,

How can I use the FileMove to move *.pst files but from all subdirectories to a specific location?

Thank-you, Br,

João Jones

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi,

welcome to forum.

;Start Folder
$dir = "D:\"
;target Folder !! without \ !!
$target = "D:\backup"
$arfiles = _GetFilesFolder_Rekursiv($dir, "pst", 0)

For $i = 1 To UBound ($arfiles) - 1
    ;move to $target with directory structure
    If StringRight ($dir, 1) = "\" Then 
        FileMove ($arfiles [$i], StringReplace ($arfiles [$i], StringLeft ($dir, StringLen ($dir) - 1), $target),8)
    Else
        FileMove ($arfiles [$i], StringReplace ($arfiles [$i], $dir, $target))
    EndIf
Next

;==================================================================================================
; Function Name:   _GetFilesFolder_Rekursiv($sPath [, $sExt='*' [, $iDir=-1 [, $iRetType=0 ,[$sDelim='0']]]])
; Description:     recursive listing of files and/or folders
; Parameter(s):    $sPath     Basicpath of listing ('.' -current path, '..' -parent path)
;                  $sExt      Extension for file selection '*' or -1 for all (Default)
;                  $iDir      -1 Files+Folder(Default), 0 only Files, 1 only Folder
;      optional:   $iRetType  0 for Array, 1 for String as Return
;      optional:   $sDelim    Delimiter for string return
;                             0 -@CRLF (Default)  1 -@CR  2 -@LF  3 -';'  4 -'|'
; Return Value(s): Array (Default) or string with found pathes of files and/or folder
;                  Array[0] includes count of found files/folder
; Author(s):       BugFix (bugfix@autoit.de)
;==================================================================================================
Func _GetFilesFolder_Rekursiv($sPath, $sExt='*', $iDir=-1, $iRetType=0, $sDelim='0')
    Global $oFSO = ObjCreate('Scripting.FileSystemObject')
    Global $strFiles = ''
    Switch $sDelim
        Case '1'
            $sDelim = @CR
        Case '2'
            $sDelim = @LF
        Case '3'
            $sDelim = ';'
        Case '4'
            $sDelim = '|'
        Case Else
            $sDelim = @CRLF
    EndSwitch
    If ($iRetType < 0) Or ($iRetType > 1) Then $iRetType = 0
    If $sExt = -1 Then $sExt = '*'
    If ($iDir < -1) Or ($iDir > 1) Then $iDir = -1
    _ShowSubFolders($oFSO.GetFolder($sPath),$sExt,$iDir,$sDelim)
    If $iRetType = 0 Then
        Local $aOut
        $aOut = StringSplit(StringTrimRight($strFiles, StringLen($sDelim)), $sDelim, 1)
        If $aOut[1] = '' Then
            ReDim $aOut[1]
            $aOut[0] = 0
        EndIf
        Return $aOut
    Else
        Return StringTrimRight($strFiles, StringLen($sDelim))
    EndIf
EndFunc

Func _ShowSubFolders($Folder, $Ext='*', $Dir=-1, $Delim=@CRLF)
    If Not IsDeclared("strFiles") Then Global $strFiles = ''
    If ($Dir = -1) Or ($Dir = 0) Then
        For $file In $Folder.Files
            If $Ext <> '*' Then
                If StringRight($file.Name, StringLen($Ext)) = $Ext Then _
                    $strFiles &= $file.Path & $Delim
            Else
                $strFiles &= $file.Path & $Delim
            EndIf
        Next
    EndIf
    For $Subfolder In $Folder.SubFolders
        If ($Dir = -1) Or ($Dir = 1) Then $strFiles &= $Subfolder.Path & '\' & $Delim
        _ShowSubFolders($Subfolder, $Ext, $Dir, $Delim)
    Next
EndFunc

;-))

Stefan

@Edit: See post #5

Edited by 99ojo

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Or use this:

$loc = @ScriptDir & "\" & "PSTfiles"
MoveFiles(@ScriptDir, "pst", $loc) ; Scans @ScriptDir & subdirectories for .pst files

Func MoveFiles($hDir, $ext, $location)
  FileChangeDir($hDir)
  $hSearch = FileFindFirstFile("*.*")
  $hFile = FileFindNextFile($hSearch)
  While Not @error
    If @extended Then ; $hFile is a directory
      MoveFiles($hDir & "\" & $hFile, $ext, $location) ; Scan the subdirectory for .pst files
    Else ; $hFile is a file
      If StringRegExp($hFile, "(?i)\." & $ext & "\z") Then FileMove($hFile, $location & "\" & $hFile, 8) ; Move $hFile to $location (create directory if needed -- flag 8)
    EndIf
    $hFile = FileFindNextFile($hSearch)
  WEnd
  FileClose($hSearch)
EndFunc
Edited by d4ni

Share this post


Link to post
Share on other sites

Thank-you very much everybody :mellow:

Share this post


Link to post
Share on other sites

Or use this:

$loc = @ScriptDir & "\" & "PSTfiles"
MoveFiles(@ScriptDir, "pst", $loc) ; Scans @ScriptDir & subdirectories for .pst files

Func MoveFiles($hDir, $ext, $location)
  FileChangeDir($hDir)
  $hSearch = FileFindFirstFile("*.*")
  $hFile = FileFindNextFile($hSearch)
  While Not @error
    If @extended Then ; $hFile is a directory
      MoveFiles($hDir & "\" & $hFile, $ext, $location) ; Scan the subdirectory for .pst files
    Else ; $hFile is a file
      If StringRegExp($hFile, "(?i)\." & $ext & "\z") Then FileMove($hFile, $location & "\" & $hFile, 8) ; Move $hFile to $location (create directory if needed -- flag 8)
    EndIf
    $hFile = FileFindNextFile($hSearch)
  WEnd
  FileClose($hSearch)
EndFunc

Thank-you d4ni,

I have a small problem with it.

I have various PST file under the D:\ drive

Under-->

D:\Data\outlook\Private.pst

D:\Data\Private2.pst

D:\Data\Test3.pst

I modified the 2nd line of your script to: MoveFiles("D:\", "pst", $loc) ; Scans @ScriptDir & subdirectories for .pst files

And the result is:

He moved only the D:\Data\outlook\Private.pst and not the rest of the PST's

Can you help me ?

Thank-you in advance

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I will have a look at it, I was also able to reproduce it. For some reason FileMove returns 0 (failure) on the other 2 .pst files -- it does actually execute FileMove for all .pst files as expected. I'll edit this post soon.

~ edit

Ok here you go. Problem was the @WorkingDir was changed when MoveFiles() was called recursively, but in the Else statement ; $hFile is a file, I had to change it back but forgot :mellow:

$loc = @ScriptDir & "\" & "PSTfiles"
MoveFiles(@ScriptDir, "pst", $loc) ; Scans @ScriptDir & subdirectories for .pst files

Func MoveFiles($hDir, $ext, $target)
  If Not StringRegExp($target, "\\\z") Then $target &= "\"
  FileChangeDir($hDir)
  $hSearch = FileFindFirstFile("*.*")
  $hFile = FileFindNextFile($hSearch)
  While Not @error
    If @extended Then ; $hFile is a directory
      MoveFiles($hDir & "\" & $hFile, $ext, $target) ; Scan the subdirectory for .pst files
    Else ; $hFile is a file
      FileChangeDir($hDir)
      If StringRegExp($hFile, "(?i)\." & $ext & "\z") Then FileMove($hFile, $target, 8+1) ; Move $hFile to $target (create directory if needed and overwrite existing files -- flag 8+1)
    EndIf
    $hFile = FileFindNextFile($hSearch)
  WEnd
  FileClose($hSearch)
EndFunc
Edited by d4ni

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0