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.
('WMIC Volume Where "Label='%fileVHD%'" Get DriveLetter 2^>Nul'), instead? BTW, there are several lines in the mount script which require double quoting modifications.set vhdPath=%~dpnx1should beset "vhdPath=%~dpnx1"...if {%1}=={}should beif "%~1"==""and yet another set command:set "diskPartScript=%~nx0.diskpart", all file name variables following the redirection operators>and>>and also followingtype,diskpart,delcommands. Although this particular conditionif {!driveLetter!}=={}does not cause any problems you should to stick to the convention and useif "!driveLetter!"==""instead.