Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Can I make a .VBS run like a DOS command  (Read 3885 times)

0 Members and 1 Guest are viewing this topic.

shreked

    Topic Starter


    Rookie

    • Experience: Familiar
    • OS: Windows 7
    Can I make a .VBS run like a DOS command
    « on: November 19, 2018, 11:03:36 AM »
    HI

    I have the following script given to me as a .VBS file but im trying to get it to run as a .BAT file as I need it to show it running and transferring files as you do on the DOS when you run .CMD file.  Can this be done?

    Code: [Select]
    Option Explicit

    ' Global variables
    Dim strBaseDir, strDestDir
    Dim objFSO, objFile
    Dim arrFiles(), i
    Dim lngFolderSize, intFolderNumber, strNextDir, intMoveFile

    ' Define paths to work with
    strBaseDir = "B:\EE\EE29124343\base"
    strDestDir = "B:\EE\EE29124343\dest"

    ' Set maximum size of new folders
    Const cMaxFolderSize = 500000000

    ' Define class that will hold file information
    Class File
        Public lngSize
        Public strPath
    End Class

    ' Create file system object
    Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

    ' Fully resolve paths
    strBaseDir = objFSO.GetAbsolutePathname(strBaseDir)
    strDestDir = objFSO.GetAbsolutePathname(strDestDir)

    ' Make sure the folders exists, exit if not
    If Not objFSO.FolderExists(strBaseDir) Then
        WScript.Echo "*ERROR* Folder does not exist: """ & strBaseDir & """."
        WScript.Quit
    End If
    If Not objFSO.FolderExists(strDestDir) Then
        WScript.Echo "*ERROR* Folder does not exist: """ & strDestDir & """."
        WScript.Quit
     End If
     
    ' Initialize array index variable
    i = -1

    ' Load info for each file into array (using File class)
    For Each objFile In objFSO.GetFolder(strBaseDir).Files
        ' Don't include any files with size greater than max allowed in a folder
        If objFile.Size > cMaxFolderSize Then
            WScript.Echo "*WARNING* Skipping file: """ & objFile.Path & """, size:""" & objFile.Size & """ exceeds maximum folder size:""" & cMaxFolderSize & """."
        Else
            ' Add another element to the array of type File class
            i = i + 1
            ReDim Preserve arrFiles(i)
            Set arrFiles(i) = New File

            ' Store the size and full path to the file
            arrFiles(i).strPath = objFile.Path
            arrFiles(i).lngSize = objFile.Size
        End If
    Next

    ' If no files found then exit
    If i = -1 Then
        WScript.Echo "*WARNING* No files found to process."
        WScript.Quit
    End If

    ' Sort the files arrary by size in descending order
    SortArray arrFiles

    ' Process all files moving to new subfolders until done
    intFolderNumber = 0
    Do
        ' Start a new destination folder and create it (MUST NOT ALREADY EXIST)
        lngFolderSize = cMaxFolderSize
        intFolderNumber = intFolderNumber + 1
        strNextDir = strDestDir & "\" & intFolderNumber & "\"
        objFSO.CreateFolder strNextDir

        ' Move files to dest folder until full
        Do
            ' Look for the largest file left that will fit in remaining space
            intMoveFile = GetFileToMove(arrFiles, lngFolderSize)

            ' If we found another file to move then move it
            If intMoveFile <> -1 Then
                Wscript.Echo "*DEBUG* Dest:[" & intFolderNumber & "], Available:[" & lngFolderSize & "], File:[" & arrFiles(intMoveFile).strPath & "], Size:[" & arrFiles(intMoveFile).lngSize & "]."
                objFSO.MoveFile arrFiles(intMoveFile).strPath, strNextDir
                lngFolderSize = lngFolderSize - arrFiles(intMoveFile).lngSize
                arrFiles(intMoveFile).lngSize = -1
            End If
        Loop Until intMoveFile = -1

    Loop Until AllFilesMoved(arrFiles)

    Function GetFileToMove(ByRef arrArray(), lngSize)
        ' Find next largest file to move that fits, -1 if none found
        Dim i
        GetFileToMove = -1
        For i = LBound(arrArray) To UBound(arrArray)
            If arrArray(i).lngSize <> -1 Then
                If arrArray(i).lngSize <= lngSize Then
                    GetFileToMove = i
                End If
                Exit Function
            End If
        Next
    End Function

    Function AllFilesMoved(ByRef arrArray())
        ' See if all files have been moved
        Dim i
        AllFilesMoved = True
        For i = LBound(arrArray) To UBound(arrArray)
            If arrArray(i).lngSize <> -1 Then
                AllFilesMoved = False
                Exit Function
            End If
        Next
    End Function

    Sub SortArray(ByRef arrArray())
        ' Sort array of files by size, descending order (simple bubble sort)
        Dim i, j, intTemp
        For i = LBound(arrArray) to UBound(arrArray)
            For j = LBound(arrArray) to UBound(arrArray) - 1
    '            If arrArray(j).lngSize < arrArray(j + 1).lngSize Then
                If LCase(arrArray(j).strPath) > LCase(arrArray(j + 1).strPath) Then
                    Set intTemp = arrArray(j + 1)
                    Set arrArray(j + 1) = arrArray(j)
                    Set arrArray(j) = intTemp
                    Set intTemp = Nothing
                End If
            Next
        Next
    End Sub

    DaveLembke



      Sage
    • Thanked: 662
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Can I make a .VBS run like a DOS command
    « Reply #1 on: November 19, 2018, 11:16:50 AM »
    Quote
    as I need it to show it running and transferring files

    Is this homework?

    the VBS can be called from the batch or even generated dynamically within the batch to write an output to a .vbs file and then that .vbs called from batch, but batch execution will still be a batched environment and the .vbs script will still be the vb script environment.

    Not sure why you cant just run the .vbs unless your trying to get around some sort of block that disallows execution of .vbs files to which what you have going on in the .vbs would have to be all handled by batch alone and batch has limitations and thats why people use other scripting languages as well as pair batch with other scripting languages to handle where batch isnt well suited.

    shreked

      Topic Starter


      Rookie

      • Experience: Familiar
      • OS: Windows 7
      Re: Can I make a .VBS run like a DOS command
      « Reply #2 on: November 19, 2018, 11:28:26 AM »
      No its not homework.

      Tell you what it is im running the VBS no problem but it dosent show anything whilst it is running and there will be numerous people using it and im frightened that they will think nothing is happening and will close it.  I just thought that a .BAT file would show it running or is there another way you can show a VBS script running.

      Salmon Trout

      • Guest
      Re: Can I make a .VBS run like a DOS command
      « Reply #3 on: November 19, 2018, 12:41:51 PM »
      There are two script engines for running Visual Basic Script code (.vbs files)

      wscript.exe (default) - no console window, scripts seem to run invisibly unless you have message boxes etc. Each Wscript.echo command shows a message box with an OK button. You have to click this before the script will move on.

      cscript.exe - opens a console window. Each Wscript.echo command shows text in the console, just like the echo command in batch scripts. After it shows the text, it moves on straight away, without waiting.

      You can Google to find out all about these, how to use them, etc. I have to tell you your script would need some changes before it would be any use under cscript.exe.

      shreked

        Topic Starter


        Rookie

        • Experience: Familiar
        • OS: Windows 7
        Re: Can I make a .VBS run like a DOS command
        « Reply #4 on: November 20, 2018, 04:13:33 AM »
        Many thanks that makes sense.

        I think that I would be going more for CSCRIPT so I will google that and have a look.

        Thanks