Open shortcuts in batch files with spaces - batch-file

I am trying to open a short cut on a desktop that has spaces in it?
every time I put the space in the batch file it doesn't recognize it.
sorry I am pretty new to all of this so...
#echo off
start BLS UK.lnk
pause
Shortcut location is: C:\Users\Jonathan.Gorbutt\Desktop
if needed
Really do appreciate all the help

#echo off
start "" "C:\Users\Jonathan.Gorbutt\Desktop\BLS_UK.lnk"
pause
Note that the first quoted item in a start command is taken to be the window title.
This worked on my machine:
#ECHO OFF
SETLOCAL
start "" "C:\106x\with spaces\nasm manual.lnk"
GOTO :EOF
Where nasm manual.lnk had been copied from my desktop to C:\106x\with spaces\
Sadly No luck anything else? doesn't provide us with much of a clue about what happened. It all becomes a guessing game...
You are aware that the command (copied from your question) doesn't contain a space? Jonathan.Gorbutt is not the same as Jonathan Gorbutt

Place double quotes around the shortcut like "C:\Users\Jonathan.Gorbutt\Desktop"

Related

Error "Invalid path 0 File(s) copied" using xcopy in batch file

I want to start with a little disclaimer:
I read this thread on a similar issue to mine, but it seems like the solution doesn't work. It might just be me not understanding it completely but here I am asking for clarification.
My goal is to copy a shortcut to the start menu programs folder conserving all of its attributes, icon and start in value. I thought making a copy would be simple but it seems like my brain can't understand anything today.
So here's the actual xcopy argument:
#echo off
xcopy "%~dp0\file.lnk" "%userprofile%\Start Menu\Programs\file.lnk\" /p /v /f
pause
I have tried every combination of adding/removing the file name, with/without the \ at the end and any combination of both... I also tried running the batch file as administrator just in case.
The #echo off is just a habit and the pause is to allow me to read any error messages that could pop up. I also put the extra arguments into the xcopy line to try to get more information. It doesn't seem to help me a lot though.
I'm starting to think the issue is completely isolated from the other thread.
As suggested by SomethingDark, changing the path from %userprofile%\Start Menu\Programs\ to %AppData%\Microsoft\Windows\Start Menu\Programs\ fixed my issue.

When using Batch file to start a program, it says "Cannot find 'example.exe' "

I am 100% sure that I have the exe application. The exe application is DriverBooster.exe. It will not start the program with the command `
#echo off
start DriverBooster.exe
`. I searched my computer and found the exact file in C:\Program Files\IObit\Driver Booster, with the exe so then I wrote
#echo off
start C:\Program Files\IObit\Driver Booster\DriverBooster.exe
`
But it still shows that Windows cannot find the program "DriverBooster.exe", try fixing spelling mistakes. I also checked the spelling and its 100% correct. I am using a batch file and editing it with "notepad" (Yes a regular notepad). Please someone help.I would greatly appricate anyone helping, thank you!
It might be due to the space in the file location, try this:
#echo off
start "" "C:\Program Files\IObit\Driver Booster\DriverBooster.exe"
EDIT: Added extra set of quotes as suggested by Squashman - it has been a while since I've done bash :)

Opening project in VSCode using batch file

Disclaimer: I read this and this before, but it doesn't work as I want.
Description: I decided to create set of batch files for convenient way to run different projects in VSCode from desktop in one click(double-click). I want close cmd terminal after running a batch file, but terminal remains. I tried:
start code "C:\Users\MyUserName\path\to\my\project\directory"
and
cmd /c start code "C:\Users\MyUserName\path\to\my\project\directory"
It quickly runs command, runs code and opens my project, then, it seems to me, closes terminal and runs a new one in desktop directory.
I found solution with help of DavidPostill. This works fine for me:
start "" cmd /b /c code "C:\Users\MyUserName\path\to\my\project\directory" && exit 0
UPDATE:
There is a more simple way to run VSCode using command line interface:
cd path/to/my/project
code .
If anyone else comes across this in 2022, I found a solution that works great for me.
code "" "C:\path\to\folder\with\project" | exit
Also, below is my batch I made for a quick workspace that:
asks for a folder name, this will also be used as the project name
makes the vscode project
makes 2 text files, one for things I had to look up, and another for answers to my question
makes a png file called work.png that opens with paint for diagrams I might need for thinking through things
Lastly (the part I love the most) it CLOSES the command window once everything is opened!
Hope this helps someone like me who doesn't know everything about batch files!
#echo off &setlocal
set /p "folder=Enter the folder name to be created: "
md "%folder%" ||(pause &goto :eof)
cd %folder%
echo. > Things_I_had_to_look_up.txt
echo. > Answers.txt
dotnet new console
xcopy /s "C:\xPaintFileTemplate" "C:\Users\TBD\Documents\Program Work\%folder%"
start mspaint.exe Work.png
code "" "C:\Users\TBD\Documents\Program Work\%folder%" | exit
Rem not needed but to be safe
exit
The C:\xPaintFileTemplate is a folder in my C drive that contains only a png file named Work.png. The png file is blank, and sized to what I want mspaint to open with. If there are more files in that folder, it will copy all of them, so be careful with xcopy!
The Rem is a comment, saying the exit command doesn't seem to be required but I added it in anyways as I believe it is good practice.
Try using: start cmd /C code . :0 It should be able to close the cmd terminal. At least that worked for me on my Windows 10.
Another version:
start cmd /C code "C:\Users\MyUserName\path\to\my\project\directory" :0
Based on Blake Daugherty's answer, I found the first pair of double quotes seems unnecessary:
code "D:\proj\directory-1" | exit
code "D:\proj\directory-2" | exit
exit
None of the above worked for me; at worst one of the left-over CMD's needed its close button clicking three times before it would go away.
I tried the URL method and this worked just as I wanted: VSCode opened, and the cmd window went away; my batch file ("VSCode on project.bat") contains just one line:
start vscode://file/C:/path/to/project
or:
start "" "vscode://file/C:/path/to/project"
One line code:
code C:\Users\MyUserName\path\to\my\project\directory pause

Batch Drag And Drop Problems

Recently I've been getting in to batch. I wanted to make a file that opens the file that you dragged on to it. But I can only seem to open a blank CMD window and or nothing at all. Here's my code.
#echo off
cd %dp1
start %1
exit
just a missing tilde: cd %~dp1
Thanks, SomethinDark, I overlooked that. A bit expained:
a "dragged" file with spaces (in filename or path) will already be qouted. Which is good (no need to care about quotes).
Start takes the first quoted parameter as title, which is bad. I assume, your "blank CMD window"'s title is exactly your parameter?
Give start a dummy title to avoid that:
start "" %1
Best practice: **always* use a title (blank or not) with start.

Gracefully trap error on start cmd

On a cmd prompt or bat file, I issue the following:
start textpad myfile.txt and it works fine.
If the program textpad does not exist on the computer, then an error sound and a popup occurs which the OK button must be pushed.
I desire to trap this error so that I could do something like
start textpad myfile.txt || start notepad myfile.txt
where the || implies that if the start of textpad is not successful, then the start of notepad should occur. HOWEVER, I still get the error sound and requirement of hitting OK.
My intent is to avoid the sound and the requirement of any user intervention.
I have also tried the following bat approach below, to no avail.
start textpad
if not %ERRORLEVEL% == 0 GOTO END
start notepad
:END
Any help would be great.
thanks
ted
You can use the following little snippet to find out whether the program you intend to launch exists:
for %%x in (textpad.exe) do set temp=%%~$PATH:x
if [%temp%]==[] echo Didn't exist.
But may I suggest that you simply use
start foo.txt
instead of forcing a specific editor onto the user? File type associations are there for a reason.
I do not believe you will find a way to make that work. Perhaps look for the existence of textpad.exe on the machine? If you can predict what directory it would be loaded from, this should be pretty easy using IF EXIST.
There are some techniques to detect the presence of a specific tool, but this only works for command line tool, or with GUI applications also supporting command line options.
For these tricks, take a look at this page.
"/wait" parameter would do the trick for you..
START /wait NOTEPAD.EXE SOME.TXT
echo %ERRORLEVEL%
# This gives zero as output.
START /wait TEXTPAD.EXE SOME.TXT
echo %ERRORLEVEL%
# This gives non-zero output.
You probably already have an answer, but my over-sized ego has forced me to post my answer.
So, this should work.
start textpad 2> nul||start notepad
This will start notepad if the command start texpad fails, while also redirecting any error message you may get from the first command.

Resources