1

Appending filename to file path input by the user not working.

I am getting a file path from the user and want to append the filename I want opened. After this I want to 'start' the file. However it is not working the way I expect it to. Can someone help?

@ echo off
if exist clientshutdown3.exe (
start clientshutdown3.exe) else (
set /p path= Enter path of pdf file:
start %path%\lor.pdf
)
pause

1 Answer 1

2

Couple of things. You need to wrap paths in double quotes to eliminate possible whitespace. Also, because we set variables inside of a loop (codeblock), it is best we use delayedexpansion.

Lastly and most importantly, NEVER set a variable name which exists as a system environment variable. Here, path will break your actual path, and commands will no longer work, so instead create a non existing name like mypath.

@echo off
setlocal enabledelayedexpansion
if exist clientshutdown3.exe (
    start clientshutdown3.exe
 ) else (
    set /p "mypath=Enter path of pdf file: "
    start "" "!mypath!\lor.pdf"
)
pause

You could also get away without delayedexpansion by moving the start section to outside of the codeblock.

@echo off
if exist clientshutdown3.exe (
    start clientshutdown3.exe
 ) else (
    set /p "mypath=Enter path of pdf file: "
)
if defined mypath start "" "%mypath%\lor.pdf"
pause
Sign up to request clarification or add additional context in comments.

3 Comments

Just one question. Will this batch file work in Windows CE. I tried it on the device, but it isn't working.
Unfortunately I do not have a CE device to test on, you would need to specify the output or error you recieve when attempting it on a Windows CE device.
I think the problem is setlocal. It isn't available for wince. Is there a workaround?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.