[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 468
  • Last Modified:

Trouble with Vb script and paths with spaces.

I am trying to copy the My Docs, Favorites and any userdata from a drive.

I am merging 2 chunks that worked.
I think my hang up is in the BrowseToFolder variable.
The original script has doule backslashes "c:\\Scripts"

I was trying to capture the Path by browsing and then inserting it into the
copy script.

---------------------------------------------------------------------------------------

Option Explicit
On Error Resume Next

Const MY_COMPUTER   = &H11&
Const WINDOW_HANDLE = 0 ' Must ALWAYS be 0
Dim numOptions, objFolder, objFolderItem, BrowseToFolder
Dim objPath, objShell, strPath, strPrompt, Count
Dim strComputer, objWMIService, colFolders, errResults

strComputer = "."
Count = 0

' Set the options for the dialog window
strPrompt = "Select a folder:"
If blnSimpleDialog = True Then
      numOptions = 0      ' Simple dialog
Else
      numOptions = &H10&  ' Additional text field to type folder path
End If      

' Create a Windows Shell object
Set objShell = CreateObject( "Shell.Application" )
' If specified, convert "My Computer" to a valid
' path for the Windows Shell's BrowseFolder method
If UCase( myStartLocation ) = "MY COMPUTER" Then
      Set objFolder = objShell.Namespace( MY_COMPUTER )
      Set objFolderItem = objFolder.Self
      strPath = objFolderItem.Path
Else
      strPath = myStartLocation
End If

Set objFolder = objShell.BrowseForFolder( WINDOW_HANDLE, strPrompt,  numOptions, strPath )
' Quit if no folder was selected
If objFolder Is Nothing Then
          BrowseFolder = ""
      WScript.Echo "Nothing"
          WScript.Quit
End If
      
' Retrieve the path of the selected folder
Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path
'WScript.Echo objPath & " " & "Path"
' Return the path of the selected folder
BrowseToFolder = objPath
WScript.Echo BrowseToFolder & " " & "Path"
'''''''''''''''''
'The Path needs to be in the User Profiles folders which have spaces.
'I.E. C:\Documents And Settings\wpeterb\My Documents
'Should the double backslash be in the path? I.E. C:\\FDR1\FDR2...
' I do not have to iterate through all the files I need to copy them out.

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery( "Select * from Win32_Directory where Name = BrowseToFolder")
 
For Each objFolder in colFolders
          errResults  = objFolder.Copy("D:\Archive")
      Count + 1
Next
WScript.Echo Count
''''''''''''''''''''
' Clean Up
Set objFolder = Nothing
Set objFolderItem = Nothing
Set objPath = Nothing
objShell = Nothing
Set objWMIService=Nothing

WScript.Echo "Copy is done"
WScript.Quit
---------------------------------------------------------------------------------------

Thanks for helping
0
wpeterb
Asked:
wpeterb
  • 4
  • 2
1 Solution
 
wpeterbAuthor Commented:
I have been able to figure out a work around but I still have trouble with navigaton issues.
This works but I can't save to a sub folder location. i.e. D:\Archive\Favorites

Option Explicit
On Error Resume Next

Const MY_COMPUTER   = &H11&
Const WINDOW_HANDLE = 0 ' Must ALWAYS be 0
Dim numOptions, objFolder, objFolderItem, BrowseToFolder
Dim objPath, objShell, strPath, strPrompt, Count, strDirectory
Dim objFSO, objFileCopy, strFilePath, strDestination, strArchive

strComputer = "."
Count = 0

' Set the options for the dialog window
strPrompt = "Select a folder:"
If blnSimpleDialog = True Then
      numOptions = 0      ' Simple dialog
Else
      numOptions = &H10&  ' Additional text field to type folder path
End If      

' Create a Windows Shell object
Set objShell = CreateObject( "Shell.Application" )
' If specified, convert "My Computer" to a valid
' path for the Windows Shell's BrowseFolder method
If UCase( myStartLocation ) = "MY COMPUTER" Then
      Set objFolder = objShell.Namespace( MY_COMPUTER )
      Set objFolderItem = objFolder.Self
      strPath = objFolderItem.Path
Else
      strPath = myStartLocation
End If

Set objFolder = objShell.BrowseForFolder( WINDOW_HANDLE, strPrompt, numOptions, strPath )
' Quit if no folder was selected
If objFolder Is Nothing Then
          BrowseFolder = ""
      WScript.Echo "Nothing"
          WScript.Quit
End If
      
' Retrieve the path of the selected folder
Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path
'WScript.Echo objPath & " " & "Path"
' Return the path of the selected folder
BrowseToFolder = objPath
WScript.Echo BrowseToFolder & " " & "Path"
'''''''''''''''''
'The Path needs to be in the User Profiles folders which have spaces.
'I.E. C:\Documents And Settings\wpeterb\My Documents
'I want to save each folder in a similar named folder. D:\Archive\Favorites D:\Archive\My Docs

strArchive = "D:\Archive"
strDirectory = (InputBox(" Location to Save Files", "Copy Location"))

' Create FileSystemObject. So we can apply .createFolder method
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder(strArchive)
WScript.Echo "Just created " & strArchive
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "Just created " & strDirectory

strFilePath = BrowseToFolder
WScript.Echo BrowseToFolder & " " & "Path"
strDestination = strDirectory
strDestination = strArchive

WSCript.Echo "Written " & strFileText & " to " & strFilePath
Set objFileCopy = objFSO.GetFolder(strFilePath)
' Copy the file to its destination
objFileCopy.Copy (strDestination)
WSCript.Echo "Copied to " & strDestination
Wscript.Quit
''''''''''''''''''''
' Clean Up
Set objFolder = Nothing
Set objFolderItem = Nothing
Set objPath = Nothing
Set objShell = Nothing
Set objFSO = Nothing

WScript.Echo "Copy is done"
WScript.Quit
--------------------------------
Thanks,

Pete
0
 
RobSampsonCommented:
Hi, add this line and see if it helps:
Dim strComputer, blnSimpleDialog, myStartLocation

Also comment out On Error Resume Next to see if any other errors occur.

Regards,

Rob.
0
 
wpeterbAuthor Commented:
Yes I see these errors + strFileText.
Thank you.

I still am unable to save to a subdirectory.
I think I need to split the strDirectory value i.e. D:\FDR1\FLDR2 to D:\FDR1 and D:\FDR1\FLDR2
And run the Set objFolder = objFSO.CreateFolder(strDirectory) twice

Last night I was having trouble figuring the best way to split the string and reassemble it.

?
Option Explicit
'On Error Resume Next

Const MY_COMPUTER   = &H11&
Const WINDOW_HANDLE = 0 ' Must ALWAYS be 0
Dim numOptions, objFolder, objFolderItem, BrowseToFolder
Dim objPath, objShell, strPath, strPrompt, Count, strDirectory
Dim objFSO, objFileCopy, strFilePath, strDestination, strArchive
Dim strComputer, blnSimpleDialog, myStartLocation, strFileText



strComputer = "."
Count = 0

' Set the options for the dialog window
strPrompt = "Select a folder:"
If blnSimpleDialog = True Then
      numOptions = 0      ' Simple dialog
Else
      numOptions = &H10&  ' Additional text field to type folder path
End If      

' Create a Windows Shell object
Set objShell = CreateObject( "Shell.Application" )
' If specified, convert "My Computer" to a valid
' path for the Windows Shell's BrowseFolder method
If UCase( myStartLocation ) = "MY COMPUTER" Then
      Set objFolder = objShell.Namespace( MY_COMPUTER )
      Set objFolderItem = objFolder.Self
      strPath = objFolderItem.Path
Else
      strPath = myStartLocation
End If

Set objFolder = objShell.BrowseForFolder( WINDOW_HANDLE, strPrompt, numOptions, strPath )
' Quit if no folder was selected
If objFolder Is Nothing Then
          BrowseFolder = ""
      WScript.Echo "Nothing"
          WScript.Quit
End If
'40      
' Retrieve the path of the selected folder
Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path
'WScript.Echo objPath & " " & "Path"
' Return the path of the selected folder
BrowseToFolder = objPath
'WScript.Echo BrowseToFolder & " " & "Path"
'''''''''''''''''
'The Path needs to be in the User Profiles folders which have spaces.
'I.E. C:\Documents And Settings\wpeterb\My Documents
'Should the double backslash be in the path? I.E. C:\\FDR1\FDR2...
' I do not have to iterate through all the files I need to copy them out.
'53
'strArchive = "D:\Archive"
strDirectory = (InputBox(" Location to Save Files", "Copy Location"))

' Create FileSystemObject. So we can apply .createFolder method
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Set objFolder = objFSO.CreateFolder(strArchive)
'WScript.Echo "Just created " & strArchive
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "Just created " & strDirectory

strFilePath = BrowseToFolder
WScript.Echo BrowseToFolder & " " & "Path"
strDestination = strDirectory
'strDestination = strArchive

WSCript.Echo "Written " & strFileText & " to " & strFilePath
Set objFileCopy = objFSO.GetFolder(strFilePath)
' Copy the file to its destination
objFileCopy.Copy (strDestination)
WSCript.Echo "Copied to " & strDestination
Wscript.Quit
''''''''''''''''''''
' Clean Up
Set objFolder = Nothing
Set objFolderItem = Nothing
Set objPath = Nothing
Set objShell = Nothing
Set objFSO = Nothing

WScript.Echo "Copy is done"
WScript.Quit


Thanks,

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
wpeterbAuthor Commented:
The last code example has some spot tests and Echos added to troubleshoot.
Ignore an early quit and some extra string variables I am trying to cycle trough a splt to create the subfolders.
0
 
wpeterbAuthor Commented:
I have completed this task.
I am sure I could spend a dozen more hours on error checking or testing but I
have what I need now.

Finished Code:
'''''''''''''''''''''''''''''''''''
Option Explicit
On Error Resume Next

Const MY_COMPUTER   = &H11&
Const WINDOW_HANDLE = 0 ' Must ALWAYS be 0
Dim numOptions, objFolder, objFolderItem, BrowseToFolder
Dim objPath, objShell, strPath, strPrompt, strDirectory
Dim objFSO, objFileCopy, strFilePath, strDestination, strArchive
Dim strComputer, blnSimpleDialog, myStartLocation, strFileText
Dim arrA, strX, strB, strC, count

strComputer = "."
count = 0

'Set the options for the dialog window
strPrompt = "Select a folder:"
If blnSimpleDialog = True Then
      numOptions = 0      ' Simple dialog
Else
      numOptions = &H10&  ' Additional text field to type folder path
End If      

' Create a Windows Shell object
Set objShell = CreateObject( "Shell.Application" )
' If specified, convert "My Computer" to a valid
' path for the Windows Shell's BrowseFolder method
If UCase( myStartLocation ) = "MY COMPUTER" Then
      Set objFolder = objShell.Namespace( MY_COMPUTER )
      Set objFolderItem = objFolder.Self
      strPath = objFolderItem.Path
Else
      strPath = myStartLocation
End If

Set objFolder = objShell.BrowseForFolder( WINDOW_HANDLE, strPrompt, numOptions, strPath )
' Quit if no folder was selected
If objFolder Is Nothing Then
          BrowseFolder = ""
      WScript.Echo "Nothing"
          WScript.Quit
End If
      
' Retrieve the path of the selected folder
Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path
'WScript.Echo objPath & " " & "Path"
' Return the path of the selected folder
BrowseToFolder = objPath
'WScript.Echo BrowseToFolder & " " & "Path"
'''''''''''''''''
' Create FileSystemObject. So we can apply .createFolder method
Set objFSO = CreateObject("Scripting.FileSystemObject")

strDirectory = (InputBox(" Location to Save Files", "Copy Location"))
'strDirectory = a split string to the first \
arrA =Split(strDirectory,"\")
for each strX in arrA
      if count < 1 then            
            strB = strX & "\"
            'WScript.Echo strB       
      else            
            strB = strB + strX & "\"
            strC = strB
            'WScript.Echo strC             
            Create_Folder(strC)            
      end if
Count = Count +1
next

' Note If..Exists. Then, Else ... End If construction
Function Create_Folder(strC)
strC = ""  & strC & ""  
If objFSO.FolderExists(strC) Then
   Set objFolder = objFSO.GetFolder(strC)
   WScript.Echo strC & " already created "      
Else
   Set objFolder = objFSO.CreateFolder(strC)
'WScript.Echo "Just created " & strC
End If

End Function

WScript.Echo strC
WScript.Echo BrowseToFolder & " " & "Path"
strDestination = strC

'WSCript.Echo "Written " & strFileText & " to " & strFilePath
'Set objFileCopy = objFSO.GetFolder(strFilePath)
Set objFileCopy = objFSO.GetFolder(BrowseToFolder)
' Copy the file to its destination
objFileCopy.Copy (strDestination)
WSCript.Echo "Copied to " & strDestination

''''''''''''''''''''
' Clean Up
Set objFolder = Nothing
Set objFolderItem = Nothing
Set objPath = Nothing
Set objShell = Nothing
Set objFSO = Nothing

WScript.Echo "Copy is done"
WScript.Quit
'''''''''''''''''''''''''''''''''''

Thanks to all of the experts at Expert Exchange.

Rob Sampson did help me with this. Thank You
Sometime a tip can point you in the right path and get you out of a jam.

Pete
0
 
RobSampsonCommented:
Pete, sorry I didn't get back to you earlier...I got busy!  Thanks for the grade anyhow. I hope it's working well.

The code looks OK although I haven't tested it.

Regards,

Rob.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now