Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Moving all files to a different location

I need help changing this macro I received from Vergel Adriano. It works but
I would like it to always move all the files in F:\temp and not ask me were
the source is and I was wondering if it could ask me to browse to the
destination location so I wouldn't have to type it in the Imput box. Is this
possible??? Thanks,

Sub test()
Dim strSource As String
Dim strDest As String
Dim strFileName As String

strSource = InputBox("Enter source folder path")
strDest = InputBox("Enter destination folder path")

strFileName = Dir(strSource, vbDirectory)
If strFileName = "" Then
MsgBox "Source Folder path is invalid", vbCritical
Exit Sub
End If

strFileName = Dir(strDest, vbDirectory)
If strFileName = "" Then
MsgBox "Destination Folder path is invalid", vbCritical
Exit Sub
End If

strFileName = Dir(strSource & "\*.*")
While strFileName < ""
Name strSource & "\" & strFileName As strDest & "\" & strFileName
strFileName = Dir
Wend

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default Moving all files to a different location


Put this code in a module:

Option Explicit

Private Type BrowseInfo
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type

Const BIF_RETURNONLYFSDIRS = 1
Const MAX_PATH = 260

Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal
lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As
BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As
Long, ByVal lpBuffer As String) As Long

Public Function APIDlgFolders() As String
Dim iNull As Integer, lpIDList As Long, lResult As Long
Dim sPath As String, udtBI As BrowseInfo

With udtBI
.hWndOwner = 0
.lpszTitle = lstrcat(CurDir, "")
.ulFlags = BIF_RETURNONLYFSDIRS
End With
lpIDList = SHBrowseForFolder(udtBI)
If lpIDList Then
sPath = String$(MAX_PATH, 0)
SHGetPathFromIDList lpIDList, sPath
CoTaskMemFree lpIDList
iNull = InStr(sPath, vbNullChar)
If iNull Then
sPath = Left$(sPath, iNull - 1)
End If
End If
APIDlgFolders = sPath
End Function

Private Sub BrowseAndMove()
Dim fso, fld, fls, f
Dim strReturn As String
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder("F:\TEMP")
Set fls = fld.Files
strReturn = APIDlgFolders
For Each f In fls
fso.MoveFile f, strReturn & "\" & f.Name
Next
Set f = Nothing
Set fls = Nothing
Set fld = Nothing
Set fso = Nothing
End Sub





"Donna S" schreef in bericht
...
I need help changing this macro I received from Vergel Adriano. It works
but
I would like it to always move all the files in F:\temp and not ask me
were
the source is and I was wondering if it could ask me to browse to the
destination location so I wouldn't have to type it in the Imput box. Is
this
possible??? Thanks,

Sub test()
Dim strSource As String
Dim strDest As String
Dim strFileName As String

strSource = InputBox("Enter source folder path")
strDest = InputBox("Enter destination folder path")

strFileName = Dir(strSource, vbDirectory)
If strFileName = "" Then
MsgBox "Source Folder path is invalid", vbCritical
Exit Sub
End If

strFileName = Dir(strDest, vbDirectory)
If strFileName = "" Then
MsgBox "Destination Folder path is invalid", vbCritical
Exit Sub
End If

strFileName = Dir(strSource & "\*.*")
While strFileName < ""
Name strSource & "\" & strFileName As strDest & "\" & strFileName
strFileName = Dir
Wend

End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Moving all files to a different location

Here is a modification of code at John Walkenbach's site.

Paste this into a New Module or put the declarations part at the top of the
module.

Then run TestMe

Option Explicit
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

'32-bit API declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) _
As Long

Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long


Sub TestMe()
Dim Msg As String
Dim strFileName As String
Dim strDest As String
Dim sPath As String
sPath = "C:\Temp\"
Msg = "Please select a location to move the files to."
strDest = GetDirectory(Msg)
If strDest = "" Then Exit Sub
If Right(strDest, 1) < "\" Then strDest = _
strDest & "\"

strFileName = Dir(sPath & "*.*")
Do While strFileName < ""
Name sPath & strFileName As strDest & strFileName
strFileName = Dir
Loop



End Sub

Function GetDirectory(Optional Msg) As String
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer

' Root folder = Desktop
bInfo.pidlRoot = 0&

' Title in the dialog
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
Else
bInfo.lpszTitle = Msg
End If

' Type of directory to return
bInfo.ulFlags = &H1

' Display the dialog
x = SHBrowseForFolder(bInfo)

' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetDirectory = Left(path, pos - 1)
Else
GetDirectory = ""
End If
End Function

--
Regards,
Tom Ogilvy


"Donna S" wrote:

I need help changing this macro I received from Vergel Adriano. It works but
I would like it to always move all the files in F:\temp and not ask me were
the source is and I was wondering if it could ask me to browse to the
destination location so I wouldn't have to type it in the Imput box. Is this
possible??? Thanks,

Sub test()
Dim strSource As String
Dim strDest As String
Dim strFileName As String

strSource = InputBox("Enter source folder path")
strDest = InputBox("Enter destination folder path")

strFileName = Dir(strSource, vbDirectory)
If strFileName = "" Then
MsgBox "Source Folder path is invalid", vbCritical
Exit Sub
End If

strFileName = Dir(strDest, vbDirectory)
If strFileName = "" Then
MsgBox "Destination Folder path is invalid", vbCritical
Exit Sub
End If

strFileName = Dir(strSource & "\*.*")
While strFileName < ""
Name strSource & "\" & strFileName As strDest & "\" & strFileName
strFileName = Dir
Wend

End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Moving all files to a different location

Change C:\Temp\

to

F:\Temp\

sorry.

--
Regards,
Tom Ogilvy


"Donna S" wrote:

I need help changing this macro I received from Vergel Adriano. It works but
I would like it to always move all the files in F:\temp and not ask me were
the source is and I was wondering if it could ask me to browse to the
destination location so I wouldn't have to type it in the Imput box. Is this
possible??? Thanks,

Sub test()
Dim strSource As String
Dim strDest As String
Dim strFileName As String

strSource = InputBox("Enter source folder path")
strDest = InputBox("Enter destination folder path")

strFileName = Dir(strSource, vbDirectory)
If strFileName = "" Then
MsgBox "Source Folder path is invalid", vbCritical
Exit Sub
End If

strFileName = Dir(strDest, vbDirectory)
If strFileName = "" Then
MsgBox "Destination Folder path is invalid", vbCritical
Exit Sub
End If

strFileName = Dir(strSource & "\*.*")
While strFileName < ""
Name strSource & "\" & strFileName As strDest & "\" & strFileName
strFileName = Dir
Wend

End Sub

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Moving all files to a different location

Tom,

Yesterday I ran your macro and it worked great. Today I run it and it stops
at this line: Name sPath & strFileName As strDest & strFileName
Any advise???

Thanks,
Donna

"Tom Ogilvy" wrote:

Here is a modification of code at John Walkenbach's site.

Paste this into a New Module or put the declarations part at the top of the
module.

Then run TestMe

Option Explicit
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

'32-bit API declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) _
As Long

Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long


Sub TestMe()
Dim Msg As String
Dim strFileName As String
Dim strDest As String
Dim sPath As String
sPath = "C:\Temp\"
Msg = "Please select a location to move the files to."
strDest = GetDirectory(Msg)
If strDest = "" Then Exit Sub
If Right(strDest, 1) < "\" Then strDest = _
strDest & "\"

strFileName = Dir(sPath & "*.*")
Do While strFileName < ""
Name sPath & strFileName As strDest & strFileName
strFileName = Dir
Loop



End Sub

Function GetDirectory(Optional Msg) As String
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer

' Root folder = Desktop
bInfo.pidlRoot = 0&

' Title in the dialog
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
Else
bInfo.lpszTitle = Msg
End If

' Type of directory to return
bInfo.ulFlags = &H1

' Display the dialog
x = SHBrowseForFolder(bInfo)

' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetDirectory = Left(path, pos - 1)
Else
GetDirectory = ""
End If
End Function

--
Regards,
Tom Ogilvy


"Donna S" wrote:

I need help changing this macro I received from Vergel Adriano. It works but
I would like it to always move all the files in F:\temp and not ask me were
the source is and I was wondering if it could ask me to browse to the
destination location so I wouldn't have to type it in the Imput box. Is this
possible??? Thanks,

Sub test()
Dim strSource As String
Dim strDest As String
Dim strFileName As String

strSource = InputBox("Enter source folder path")
strDest = InputBox("Enter destination folder path")

strFileName = Dir(strSource, vbDirectory)
If strFileName = "" Then
MsgBox "Source Folder path is invalid", vbCritical
Exit Sub
End If

strFileName = Dir(strDest, vbDirectory)
If strFileName = "" Then
MsgBox "Destination Folder path is invalid", vbCritical
Exit Sub
End If

strFileName = Dir(strSource & "\*.*")
While strFileName < ""
Name strSource & "\" & strFileName As strDest & "\" & strFileName
strFileName = Dir
Wend

End Sub

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chart macro okay but sometimes ends by moving to a new location? James A Excel Programming 1 December 17th 06 07:58 PM
location for add-in files keithb Excel Programming 1 September 13th 05 08:15 AM
Moving Cursor & Sheet back to the Original Location Ricky Pang Excel Programming 2 July 8th 05 04:18 PM
moving to relative location after finding data Frank Kabel Excel Programming 1 March 3rd 04 06:57 PM
keeping track of stock moving from one location to another DL[_3_] Excel Programming 4 September 1st 03 10:09 PM


All times are GMT +1. The time now is 08:12 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"