Call command prompt command

Discussion in 'Scripting' started by Corey, Nov 10, 2003.

  1. Corey

    Corey Guest

    What is the syntax for calling a command prompt command
    from within a script. For example, I want to use a script
    to exec "md newdir".

    Corey
     
    Corey, Nov 10, 2003
    #1
    1. Advertisements

  2. Corey

    Roland Hall Guest

    Dim newDir
    newDir = "c:\myDir"
    Set w = WScript.CreateObject("WScript.Shell")
    w.Run "cmd /c md" & newDir,1
    Set w = nothing

    Roland

    What is the syntax for calling a command prompt command
    from within a script. For example, I want to use a script
    to exec "md newdir".

    Corey
     
    Roland Hall, Nov 10, 2003
    #2
    1. Advertisements

  3. Corey

    Corey Guest

    I came across a wscript.exec("command here") some time ago
    and used it, or at least I thought I did. Was I dreaming?
     
    Corey, Nov 10, 2003
    #3
  4. Corey

    Tom Lavedas Guest

    That is WSHShell.Exec. Exec is a method of the
    Wscript.Shell object, not the Wscript object. There is a
    difference.

    For downloadable documentation for WSH 5.6 see (the single
    URL line is wrapped):

    WSH 5.6 documentation download
    http://www.microsoft.com/downloads/details.aspx?
    FamilyId=01592C48-207D-4BE1-8A76-
    1C4099D7BBB9&displaylang=en

    BTW, the FileSystemObject provides a CreateFolder method.

    Tom Lavedas
    ===========
     
    Tom Lavedas, Nov 10, 2003
    #4
  5. Corey

    Roland Hall Guest

    This doesn't test the availability and readiness of the drive, if included
    in the argument, but otherwise it will work. It returns the result if
    successful or returns an error if the folder already exists.

    Save file as cfolder.vbs
    Call it with cfolder <d:\folder | folder>
    Ex.
    cfolder myfolder
    cfolder c:\myfolder
    cfolder c:\temp\myfolder - temp folder must already exist - I do not parse
    out the argument and test for existance of each parent folder and create it
    if it does not exist. So, there is room to add functionality here but this
    will get you started.

    ' Create Folder
    ' Passes target folder as argument
    ' Written by Roland Hall
    Dim i, oResult
    Set oArgs = Wscript.Arguments

    Function createNewFolder(oDrive)
    Dim fso, f
    Set fso = CreateObject("Scripting.FileSystemObject")
    If (fso.FolderExists(oDrive)) Then
    WScript.Echo "Error: " & oDrive & " folder already exists!" & vbCrLf &
    "Create Folder Aborted!"
    WScript.quit
    Else
    Set f = fso.CreateFolder(oDrive)
    CreateNewFolder = f.Path
    End If
    End Function

    i = oArgs.Count
    If i = 0 then
    WScript.Echo "Please provide the full path and/or folder name to create
    please!"
    WScript.Quit
    Else
    oResult = createNewFolder(oArgs(0))
    WScript.Echo oResult & " created successfully!"
    End If

    Roland

    That is WSHShell.Exec. Exec is a method of the
    Wscript.Shell object, not the Wscript object. There is a
    difference.

    For downloadable documentation for WSH 5.6 see (the single
    URL line is wrapped):

    WSH 5.6 documentation download
    http://www.microsoft.com/downloads/details.aspx?
    FamilyId=01592C48-207D-4BE1-8A76-
    1C4099D7BBB9&displaylang=en

    BTW, the FileSystemObject provides a CreateFolder method.

    Tom Lavedas
    ===========
     
    Roland Hall, Nov 11, 2003
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.