Batch Drag And Drop Problems - batch-file

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.

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.

Create a batch file to run a specific exe which has a few other parameters

Here's the thing: There's an exe file to stamp on PDF through command line.
the file is from this website:
pdfmachine-command-line-tools
and this is the link to download the exe file:
pdfMachineStamp
PREVIOSULY:
I paste both the downlaoded 'pdfMachineStamp.exe' from the website, and also paste my 'prueba.pdf' (PDF file to stamp) here, manually:
C:\PDF
HOW THE EXE WORKS?:
Then I go to run MANUALLY on the System Symbol:
C:\PDF>pdfmachinestamp.exe /f prueba.pdf /t "This is the Stamp" /pos center /size 15 /rot 45 /color black /opacity 1
and it runs perfect!
My 'prueba.pdf' ends with a stamp in the center of the page rotated 45 degrees saying "This is the Stamnp".
THE PROBLEM:
NOW, I want to create a batch file to do that with a single click... but I'm new in this, and I'm stuck...
I dont' know if i have to edit the bat file with START,RUN, CD, or... ????
Then I tried
start /d "C:\PDF\pdfMachineStamp.exe" /f prueba.pdf /t "PRUEBA REAL" /pos center /SIZE 25 /rot 45 /color black /opacity 1
PAUSE
and it won't work.
Any ideas?
Thanks!
There is no need to use start or call for your purposes.
Instead you simply put the command you want there.
If you plan to keep the batch fine in the same folder as the prueba.pdf and pdfmachinestamp.exe than you can use this shorthand %~dp0 to specify the path to C:\PDF\, this is because %0 is the '0th' argument of the script,, and used to hold the full path and file name of the current script (and the current label within the script if within one.) and ~dp selects just the drive (d) and path (p); to the script from the %0 variable.
Therefore this should do the needful
#Echo off
"%~dppdfMachineStamp.exe" /f "%~dpprueba.pdf" /t "PRUEBA REAL" /pos center /SIZE 25 /rot 45 /color black /opacity 1
PAUSE
You can double click this cmd script or a shortcut to the script and it should run as expected.
However, make sure you have named it scriptname.cmd if you use notepad change the type to any instead of before saving and remove .txt from the end.
Also make sure "hide file extensions for known file types" is turned off in explorer (which is on my default) as windows will hide the ".Txt" at the end of the file if present, so you can be sure it is named correctly.
I honestly didn't have idea how to make it right, but after playing a little bit and some other research in this matter i found a solution that did exactly what i want easily.
This is the script:
start "" "C:\PDF\pdfMachineStamp.exe" /f "C:\PDF\prueba.pdf" /t ""PRUEBA_REAL"" /pos center /size 30 /rot 45 /color green /opacity 1
I decided to use start "" to begin the command line, and had to use the double quotes to highlight the names of both files (the exe and the pdf). Also, I used the full path for both. for the Stamp text, I had to use double-double quotes and avoid spaces (replaced by _). and that's it.
Thanks everybody!
I hope this is useful for somebody in the future.

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

Escape characters of a file path argument for a batch file

I was making a batch file to take dragged-and-dropped folders for program input. Everything was working fine until I passed a folder, which for the sake of this post, called foo&bar.
Checking what %1 contained inside the batch file looked like C:\path\to\foo or C:\path\to\foo\foo. If the file path were in quotes it would work, so the only working code that slightly takes this into effect is :
set arg1=%1
cd %arg1%*
set arg1="%CD%"
Which changes directory to the passed argument using wildcards. However this only works once for if there is another folder with un-escaped characters inside the parent folder, passing the child folder would result in the parent folders' value.
I tried the answer of this post, which suggests to output the argument using a remark and redirection statement during an #echo on sequence. However no progress occurred in rectifying the problem. Any suggestions?
To recap, I am looking for ways to pass folders with un-escaped characters as arguments to a batch file. The implementation should preferably be in a batch file, but answers using VBScript are welcome. However the starting program must be in batch as this is the only program of the 3 that accepts files as arguments.
To test this, create a batch file with following code:
#echo off
set "arg1=%~1"
echo "the passed path was %arg1%"
pause
Then create folders called foobar and foo&bar. Drag them onto the batch file to see their output. foo&bar will only return C:\path\to\foo.
OK, so the problem is that Explorer is passing this as the command line to cmd.exe:
C:\Windows\system32\cmd.exe /c ""C:\path\test.bat" C:\path\foo&bar"
The outermost quotes get stripped, and the command becomes
"C:\working\so46635563\test.bat" C:\path\foo&bar
which cmd.exe interprets similarly to
("C:\working\so46635563\test.bat" C:\path\foo) & bar
i.e., bar is considered to be a separate command, to be run after the batch file.
The best solution would be to drag-and-drop not directly onto the batch file but onto, say, a vbscript or a Powershell script or a plain old executable. That script could then run the batch file, either quoting the argument appropriately or putting the directory path into an environment variable rather than on the command line.
Alternatively, you can retrieve the original command string from %CMDCMDLINE% like this:
setlocal EnableDelayedExpansion
set "dirname=!CMDCMDLINE!"
set "dirname=%dirname:&=?%"
set "dirname=%dirname:" =*%"
set "dirname=%dirname:"=*%"
set "dirname=%dirname: =/%"
for /F "tokens=3 delims=*" %%i in ("%dirname%") do set dirname=%%i
set "dirname=%dirname:/= %"
set "dirname=%dirname:?=&%"
set dirname
pause
exit
Note the exit at the end; that is necessary so that cmd.exe doesn't try to run bar when it reaches the end of the script. Otherwise, if the part of the directory name after the & happens to be a valid command, it could cause trouble.
NB: I'm not sure how robust this script is.
I've tested it with the most obvious combinations, but YMMV. [It might be more sensible to use delayed expansion exclusively, I'm not sure. It doesn't seem to be necessary except in the first set command. Jeb's answer here might be a better choice if you're going this route.]
For the curious, the script works like this:
Load the original command line into dirname [necessary for the reason pointed out by jeb]
Replace all the & characters with ?
Replace all the quote marks with *
If a quote mark is followed by a space, suppress the space.
NB: it is necessary to suppress the space to deal with both the case where the path contains a space (in which case Explorer adds quote marks around it) and the case where it doesn't.
Replace all remaining spaces with /
NB: ? * and / are illegal in file names, so these replacements are safe.
At this point the string looks like this:
C:\Windows\system32\cmd.exe//c/**C:\path\test.bat**C:\path\foo?bar**
So we just need to pull out the third asterisk-delimited element, turn any forward slashes back into spaces and any question marks back into ampersands, and we're done. Phew!

Open shortcuts in batch files with spaces

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"

Resources