+ Reply to Thread
Results 1 to 2 of 2

Converting DOS Commands to VBA function

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-07-2006
    Location
    India
    MS-Off Ver
    MSOffice 365 Professional 64 bit
    Posts
    108

    Converting DOS Commands to VBA function

    I have the below batch file that remaps an existing Y: drive remote shared folder to another freely available drive letter, and then disconnects Y: drive and remaps it to my chosen remote folder or local folder.

    But i don't know how to convert this batch code into a VBA function i.e. similar to below shown function. I am mainly having problems with the double-quotes in `sCMD` string.

    Any help would be most appreciated.

    What the batch commands do is :
    See if the concerned Drive e.g. Y: is available or not.
    If in use, assign Y:'s network path to the next freely available drive.
    Disconnect (delete) Y:
    Assign Y: to concerned Directory (Local or Remote).

    So need the `sCMD` properly double-quoted inorder to make it work!

    Sub TestDriveMapping()
        MapBasePathToDrive "\\xx.xx.xx.xx\SomeFolder", "Y:", True
    End Sub
    Here is the function:
    Function MapBasePathToDrive(FullDirectory As String, strDrive As String, blnReadAttr As Boolean) As String
    
    
        Dim objShell As Object
        Dim sCmd$, sDrv$
        Dim WaitOnReturn As Boolean: WaitOnReturn = True
        Dim WindowStyle As Integer: WindowStyle = 0
        Dim i&, lngErr&
    
    
        ' remove backslash for `NET USE` dos command to work
        If Right(FullDirectory, 1) = "\" Then FullDirectory = Left(FullDirectory, Len(FullDirectory) - 1)
        
        sDrv = Left(FullDirectory, 2)
        ' check if Directory Local or Remote
        If Left(FullDirectory, 2) <> "\\" And sDrv Like "?:" Then
            FullDirectory = "\\localhost\" & Left(sDrv, 1) & "$" & Right(FullDirectory, Len(FullDirectory) - 2)
        End If
    
    
        ' prefix & suffix directory with double-quotes
        FullDirectory = Chr(34) & FullDirectory & Chr(34)
    
    
        Set objShell = CreateObject("WScript.Shell")
        sCmd = ""
        sCmd = "@Echo Off " & vbCrLf
        sCmd = sCmd & " IF EXIST " & strDrive & " ( " & vbCrLf
        sCmd = sCmd & "  FOR /F ""TOKENS=1,2,3"" %%G IN ('NET USE ^|Find /I """ & strDrive & """ ^|Find ""\\""')  DO ( NET USE * %%H >NUL 2>&1) " & vbCrLf
        sCmd = sCmd & "  NET USE " & strDrive & " /DELETE >NUL 2>&1 " & vbCrLf
        sCmd = sCmd & " )" & vbCrLf
        sCmd = sCmd & " NET USE " & strDrive & "  " & FullDirectory & " >NUL 2>&1 "
        
        lngErr = objShell.Run(sCmd, WindowStyle, WaitOnReturn)
        Debug.Print Err.Number & "_" & Err.Description
        ' remove read-only attribute from Destination folder if you plan to copy files
        If blnReadAttr Then
            sCmd = "ATTRIB " & "-R" & " " & strDrive & "\*.*" & " " & "/S /D"
            lngErr = objShell.Run(sCmd, WindowStyle, WaitOnReturn)
        End If
    
    
        ' to refresh explorer to show newly created drive
        sCmd = "%windir%\explorer.exe /n,"
        lngErr = objShell.Run(sCmd & strDrive, WindowStyle, WaitOnReturn)
    
    
        ' add backslash to drive if absent
        MapBasePathToDrive = IIf(Right(strDrive, 1) <> "\", strDrive & "\", strDrive)
    
    
    End Function
    And here is the actual Batch code i wrote which works. Please note there is no error handling code to handle if all free drives are exhausted. If someone can also help with that, that would be useful for everyone :
    @echo off 
    if exist y:\ (
        for /F "tokens=1,2,3" %%G in ('net use^|Find /I "Y:"^|Find "\\"')  do ( net use * %%H >nul 2>&1)
        net use y: /delete >nul 2>&1
    )
    net use y: \\xx.xx.xx.xx\SomeFolder >nul 2>&1

    http://www.vbaexpress.com/forum/show...o-VBA-Function
    Last edited by junoon; 03-06-2020 at 02:04 AM.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Converting DOS Commands to VBA function

    Hi junoon,

    My speculation is that your problem is not sCmd, but is probably FullDirectory when it has embedded spaces. I've been successful by putting 4 double quotes before and after the variable name. For example:
    FullDirectory = """" & FullDirectory  & """"
    See the file attached to post #4 in the following thread for a working example: http://www.excelforum.com/excel-prog...reposting.html

    Lewis

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Converting Normal Excel function to DAX function
    By Vikas_Gautam in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 07-13-2016, 06:53 PM
  2. Multiple IF function commands
    By ruthyeh in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-21-2013, 11:19 AM
  3. Macro Recording Chart Format commands missing all important commands!!!!
    By nounours in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-05-2013, 09:20 PM
  4. Refreshing Array Commands for Sampling Without Replacement Function
    By gerrardfo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-04-2013, 03:38 PM
  5. Undo function to undo Visual Basic commands
    By pierre08 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-15-2010, 04:59 AM
  6. Sleep function between commands
    By rishadjb in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-01-2009, 09:31 PM
  7. [SOLVED] Assign commands to function keys
    By Gerry in forum Excel General
    Replies: 4
    Last Post: 08-08-2005, 01:05 PM

Tags for this Thread

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