0

I'm very new to coding and iI'm having a problem that is probably trivial, but is making me pull out my hair.

I'm using a batch script to automate mounting a VHD, executing a file inside and then pause until the user presses any key, which makes the VHD get unmounted and the script exits. This is the main batch file:

@echo off set fileVHD=Gord CD /D "%~dp0" powershell -command "Start-Process mount.cmd '%~dp0%fileVHD%.vhd' -Verb runas" timeout /t 1 for /f %%D in ('wmic volume get DriveLetter^, Label ^| find "%fileVHD%"') do set usb=%%D CD /D %usb% index.html echo "!!!!!!!!!!!!!!!!!!!!Press any key to fully close this program.!!!!!!!!!!!!!!!!!!!!!!!!!" pause CD /D "%~dp0" powershell -command "Start-Process unmount.cmd '%~dp0%fileVHD%.vhd' -Verb runas" exit 

This is the mount script (Not made by me):

@echo off setlocal enabledelayedexpansion if "%~1"=="" ( echo Usage: %~nx0 [vhd] [letter] exit /b 1 ) set "vhdPath=%~dpnx1" set "driveLetter=%2" if "!driveLetter!"=="" ( echo Mounting "!vhdPath!" ) else ( echo Mounting "!vhdPath!" to "!driveLetter!": ) REM REM create diskpart script REM set "diskPartScript=%~nx0.diskpart" echo select vdisk file="!vhdPath!">"!diskPartScript!" echo attach vdisk>>"!diskPartScript!" REM assign the drive letter if requested if not "!driveLetter!"=="" ( echo select partition 1 >>"!diskPartScript!" echo assign letter="!driveLetter!">>"!diskPartScript!" ) REM Show script echo. echo Running diskpart script: type "!diskPartScript!" REM REM diskpart REM diskpart /s "!diskPartScript!" del /q "!diskPartScript!" echo Done! endlocal 

When all the files are located in a system path that contains no spaces, everything works fine. But it breaks where there are spaces. That means that somewhere in the code a path is badly defined by the lack of quotes, probably in the mount script. The trouble is that i don't fully grasp the mount script when it starts using all the "%~...." variable path names. I had to mix in some powershell commands because for some reason the script wouldn't work unless executed as Administrator.

If someone could give some insight to a newbie, it would be greatly appreciated.

7
  • 1
    Why not use, ('WMIC Volume Where "Label='%fileVHD%'" Get DriveLetter 2^>Nul'), instead? BTW, there are several lines in the mount script which require double quoting modifications. Commented Sep 19, 2018 at 20:03
  • As why, i don't know, i got the command from another source, and it seemed to work, but i don't fully understand it. Where exactly do i need double quotes ? Commented Sep 19, 2018 at 20:21
  • 1
    Possibly an important one; set vhdPath=%~dpnx1 should be set "vhdPath=%~dpnx1"... Commented Sep 19, 2018 at 20:29
  • Still doesn't work with that modification. Keeps outputting a syntax error Commented Sep 19, 2018 at 21:39
  • 1
    As Compo said there are several lines, not just that one line. Another one: if {%1}=={} should be if "%~1"=="" and yet another set command: set "diskPartScript=%~nx0.diskpart", all file name variables following the redirection operators > and >> and also following type , diskpart, del commands. Although this particular condition if {!driveLetter!}=={} does not cause any problems you should to stick to the convention and use if "!driveLetter!"=="" instead. Commented Sep 20, 2018 at 7:19

2 Answers 2

1

You need end quotes around your parameters when you change directory, i.e.

CD /D "%~dp0" 

You can also see all of the %~ options by running 'help for' in a console window. In those scripts it's getting the path or filename from a variable.

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, i accidentally uploaded a version that did not have full quotes, but it doesn't work even with them
I'm not convinced you need quotes around the parameter. The CD command can work without quotes, even when there are spaces in the parameter.
@DodgyCodeException, What about ^ and & ?
@sst that's a good point. So at least an opening quote is needed. A closing quote is optional but highly recommended.
1

Discovered the root of my problem. The path from script 1 was not being passed faithfully to script 2, even using using quotes or multiquotes.

Thanks for all the input guys!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.