Search code examples
batch-filevirtualboxquotation-marks

Batch script to import Virtual Box appliance adding quotation marks to end of shared folder path


I have created a batch script that imports a virtual box appliance and configures it for that machine. Part of the script deletes the share (between the guest and host) and creates it again with a new host path like this:

"%vbPath%VBoxManage" sharedfolder remove "%devBoxName%" --name wwwshare
"%vbPath%VBoxManage" sharedfolder add "%devBoxName%" --name wwwshare --hostpath "%shareName%"

When I put these lines of code into a separate file and set shareName directly above, it works fine (eg. shareName=c:/test). However, when I run it as part of the script, the share that is created would have c:/test" as the share path, which of course means it fails. This is all the code that is used for this variable:

setlocal enabledelayedexpansion
:: Check they want the default share folder name
SET shareName=C:\ld-sd%devNum%-rh7-www\
:getShareName
SET /P shareNameOK="Your shared folder will be %shareName%. Do you want to keep this value (enter y to keep and n to change): "
cls

if /I "%shareNameOK%"=="n" (
    SET /P shareName="Please enter the path and name you would like for your shared folder: "
    GOTO :getShareName
) else (
    if /I not "%shareNameOK%"=="y" (
        echo WARNING: You did not enter y or n
        GOTO :getShareName
    )
)

:getShareCheck
:: Check if this folder exists and if they plan to keep or overwrite it if it does
if /I exist "%shareName%" (
    echo WARNING: Selecting yes will result in all website files being erased!
    SET /P shareNameOverwrite="This folder already exists, would you like to overwrite the files in it with a clean copy (yes/no)? "
    if /I not "!shareNameOverwrite!"=="no" (
        if /I "!shareNameOverwrite!"=="yes" (
            @RD /S /Q "!shareName!"
            timeout /t 1 /nobreak > NUL
            mkdir "!shareName!"
        ) else (
            echo WARNING: You did not enter yes or no - NOTE full name
            GOTO :getShareCheck
        )
    )
) else (
    SET shareNameOverwrite=yes
    mkdir "!shareName!"
)

Whenever I echo the variable (anywhere in the file) it shows no quotations in it. This is the only point it causes an issue. I have even echoed the entire line in question:

echo "%vbPath%VBoxManage" sharedfolder add "%devBoxName%" --name wwwshare --hostpath "%shareName%"

and it shows fine. Where are the quotation marks coming from?


Solution

  • The problem ended up being the line:

    SET shareName=C:\ld-sd%devNum%-rh7-www\
    

    should have been:

    SET shareName=C:\ld-sd%devNum%-rh7-www
    

    I had missed that there was a \ on the end.

    The result of this is that with a \" it will take the " as part of the string and not the end of the string.

    So if ever you have a rouge " at the end of a variable, make sure your variable does not end in a backslash!