+ Reply to Thread
Results 1 to 2 of 2

Macro to Move files from one directory to other directory

  1. #1
    Registered User
    Join Date
    08-16-2012
    Location
    Banglore
    MS-Off Ver
    Excel 2007
    Posts
    23

    Macro to Move files from one directory to other directory

    Hi,

    i want to clean up my desktop and move all the files in my desktop to defined folder. can i get a macro code which will perform this action.

    i have a code which will copy the files and move it to defined destination, but i don't want to keep the folder in older directory.


    Option Explicit

    Private Declare Function SHFileOperation Lib "shell32.dll" _
    Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

    Private Const FO_MOVE As Long = &H1
    Private Const FOF_SIMPLEPROGRESS As Long = &H100

    Private Type SHFILEOPSTRUCT
    hWnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAnyOperationsAborted As Long
    hNameMappings As Long
    lpszProgressTitle As Long
    End Type

    Sub Sample()
    Dim fileToOpen As Variant
    Dim outputfolder As String
    Dim i As Long

    outputfolder = "C:\Users\A467599\Desktop\To folder"

    fileToOpen = Application.GetOpenFilename(MultiSelect:=True)



    If IsArray(fileToOpen) Then
    'If Dir(outputfolder) = "" Then MkDir outputfolder

    For i = LBound(fileToOpen) To UBound(fileToOpen)
    Call VBCopyFolder(fileToOpen(i), outputfolder)
    Next i
    Else
    MsgBox "No files were selected."
    End If
    End Sub

    Private Sub VBCopyFolder(ByRef strSource, ByRef strTarget As String)
    Dim op As SHFILEOPSTRUCT
    With op
    .wFunc = FO_MOVE
    .pTo = strTarget
    .pFrom = strSource
    .fFlags = FOF_SIMPLEPROGRESS
    End With
    '~~> Perform operation
    SHFileOperation op
    End Sub

    Thanks,
    Uday

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro to Move files from one directory to other directory

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code in [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1