DOS start command does open directory with spaces - dos

Help please..
I wanted to Open the copied file folder once the file has been copied.
I used DOS command start. It works fine as long as the directory path does not contain any space characters.
If I use quotes in the path it rather opne another dos screen:
**Here sample CMD file:
XCopy C:\1\Source\Test.txt C:\1\Target 1\ /R/Y/K
start "C:\1\Target 1\"
Pause**

You had the right idea with the quotes. The tricky bit is that start assumes the first parameter is the window title if it is quoted. If you want to quote your target then you must provide a quoted title first. It can be empty:
start "" "C:\1\Target 1\"
However, if you happen to have a batch file named Target 1.bat, then it will execute the batch script instead of opening the Target 1 folder in Windows Explorer. For that reason, it is safer to use robert oh's answer, explicitly specifying explorer as the target with the folder as a parameter.

You could use
start explorer "c:\some folder\"

Related

How to run an executable that contains a space in path from command line on Windows 10?

H:\>"H:\Program Files\R\R-3.4.0beta\bin\R.exe"
'H:\Program' is not recognized as an internal or external command,
operable program or batch file.
H:\>"H:\Progra~1\R\R-3.4.0beta\bin\R.exe"
The system cannot find the path specified.
H:\>H:\Progra~1\R\R-3.4.0beta\bin\R.exe
The system cannot find the path specified.
I tried using "..." and Progra~1 and both are not working on Windows 10.
What I'm doing wrong?
Short answer: Use & 'C:\path with spaces\app.exe'
Explanation: Just type your path into powershell and use TAB for auto completion when you choose any directory containing spaces. Powershell will automatically insert single quotes 'bla bla' and it will also put an & in front which is needed to treat the string as something that should be executed. Continue completing your path like usual.
The way to do this - and I can't believe I'm just now figuring this out - is to use Windows short names generated for files with non-8dot3 names. To get the path or program name in question, type dir /x <path to program>. It will spit out something like PROGRA~1 for Program Files folder. Of course you have to do that directory by directory, and if you have multiple files/folders with spaces in the name, it's cumbersome. If you want the full path formatted with short names, you can do:
for %I in (*) do echo %~sI
For example, if the file I want to access is C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\License Terms\License_msodbcsql_ENU.txt, I could type:
for %I in ("C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\License Terms\License_msodbcsql_ENU.txt") do echo %~sI
And what I get back is the much easier C:\PROGRA~1\MICROS~2\CLIENT~1\ODBC\170\LICENS~1\LICENS~1.TXT.
Annoying that you can't query the whole directory path without using a loop, but it is what it is. Good for aliases.
There may be an easier way with powershell, but I'm pretty sure there isn't from the cmd prompt.
you must be doing something wrong as the double quotes encapsulates the path to the executable including spaces.
To ensure you are doing it correctly, start typing the path to the command and use TAB after F:\Program until you see the correct path, in your case it will automatically do this
"F:\Program Files"
Use your arrow key to go back behind the end quotation and continue the path and use tab until you have reached.
"H:\Program Files\R\R-3.4.0beta\bin\R.exe"
You can also try and issue it with Start
start "H:\Program Files\R\R-3.4.0beta\bin\R.exe"
If Your cmd windows is open on the actual System drive where "Program Files" are located, you can run this instead of adding the drive letter as well:
".\Program Files\R\R-3.4.0beta\bin\R.exe"
Or add it to your environment variables with the path, then it should execute with just:
R.exe

trying to run a .bat that runs an .exe from a mapped drive or share and have file not found

I'm not sure what I'm doing wrong.
I have a file share called fileser01. I placed my exe which is hp.exe in \\fileser01\\software. I can map a drive whith this command:
net use f: \\fileser01\\software /user:domain\myaccount
So the drive shows as f:.
In the .bat file I have start "f:\" hp.exe but it says it can't find the file. What am I doing wrong? I even try to run a vbs and it says it can't find the file
I tried "f:" I tried putting it in another folder in software like media so "f:\media" and still same problem.
Your question says your batch files contains this:
start "f:\" hp.exe
No, that will not work. It needs to be this:
start "Window Title" "f:\hp.exe"
Or this:
start f:\hp.exe
The entire file name with no added spaces needs to be contained between the quotation marks. And in this case you could even omit the quotation marks since the file name and path contain no spaces, in which case you can also omit the window title.

Batch: Open a specific file in a specific program?

How do i tell windows to open C:\test\test.txt in WordPad, when Notepad is my default program for .txt files?
The accepted answer didn't work for me. I am not sure if it was because of the program I was trying to run, or because the path had spaces in (even though I wrapped it in quotes), or something else.
Anyway, I was able to do it be adding an empty string after the start command.
For example:
start "" "C:\My Programs\myprogram.exe" "C:\My Files\myfile.txt"
You can add the direct path to the executable like
start C:\Windows\System32\write.exe [FILE]
Try:
start wordpad c:\test\test.txt
There is no need to use start when using write.exe, so simply:
write [FILE(S)]
write.exe is located in C:\Windows\System32.

help with windows batch scripting basics - execution and calling a separate executable within the script

Newbie to windows scripting. I need help running the .bat file on the command line so I can test it.
I used Text Document as my editor to create the file (opens up also as Notepad).
I performed file "save as" (ALL FILES). If I open up cmd, I can see the file has a .txt extension (myfile.bat.txt). So if I just type in cmd myfile.bat.txt the editor opens. I am not sure how to execute this correctly.
As for the logic in my batch script, I am basically logging into a remote directory (already created the net mount) and now I want to:
run an executeable file
rename some files.
With some research, I written this so far. I have saved it as a .bat file
# echo off
echo This is a batch file to run an executable and rename some files
pause
--run executable file here, just don't know how to do it
x:
cd x:
rename fileA fileB
Any help, good tips/practice would be great. Thanks.
Type in this command in cmd window:
rename myfile.bat.txt myfile.bat
Now you can run the script by simply invoking:
myfile.bat
or
myfile
(provided there's no myfile.exe or myfile.com in the same directory).
If you need to edit the script further, you can either right click it in Explorer and choose Edit or call the editor from the command window:
notepad myfile.bat
To call a program from the script, simply add its name, if it's in the current directory:
someprogram.exe
or the name with the path, if it's somewhere else:
directory\program.exe
or
d:\directory\program.exe
If the name or the path contain spaces, be sure to enclose the entire name & path string in double quotes:
"d:\directory\program name.exe"
you can just type the full name of the program
eg
"c:\program dir\program.exe"
or you can add the program directory to your path environment variable
set PATH=%PATH%;"c:\program dir"
and just type the program name
program
you can also edit your PATH variable in windows http://support.microsoft.com/kb/310519
NOTE: When you save the file in notepad, you want to save it as filename.BAT and select All Files from the second dropdown. If you don't it still gets saved as a .TXT.
A couple of command to consider:
CSCRIPT cscript /? in CMD
START http://ss64.com/nt/start.html
If you're doing say a VBSCRIPT use CSCRIPT to start it. If you're trying to execute another BATCH script or an EXE, use START

Administrator's shortcut to batch file with double quoted parameters

Take an excruciatingly simple batch file:
echo hi
pause
Save that as test.bat. Now, make a shortcut to test.bat. The shortcut runs the batch file, which prints "hi" and then waits for a keypress as expected. Now, add some argument to the target of the shortcut. Now you have a shortcut to:
%path%\test.bat some args
The shortcut runs the batch file as before.
Now, run the shortcut as administrator. (This is on Windows 7 by the way.) You can use either right-click -> Run as Administrator, or go to the shortcut's properties and check the box in the advanced section. Tell UAC that it's okay and once again the shortcut runs the batch file as expected.
Now, change the arguments in the target of the shortcut to add double quotes:
%path%\test.bat "some args"
Now try the shortcut as administrator. It doesn't work this time! A command window pops up and and disappears too fast to see any error. I tried adding > test.log 2>&1 to the shortcut, but no log is created in this case.
Try running the same shortcut (with the double quotes) but not as Administrator. It runs the batch file fine. So, it seems the behavior is not because of the double quoted parameters, and it's not because it's run as Administrator. It's some weird combination of the two.
I also tried running the same command from an administrator's command window. This ran the batch file as expected without error. Running the shortcut from the command window spawned a new command window which flashed and went away. So apparently the issue is caused by a combination of administrator, the shortcut, and the double quotes.
I'm totally stumped, does anyone have any idea what's going on? I'd also like a workaround.
I just ran Process Monitor on this and here is what I saw:
Run as User:
cmd /c ""C:\Users\Sunbelt\Desktop\test.bat" "some args""
Run as Admin:
"C:\Windows\System32\cmd.exe" /C "C:\Users\Sunbelt\Desktop\test.bat" "some args"
For some reason, the Run as Admin case is not quoting the entire command. It seems it is trying to run the command:
C:\Users\Sunbelt\Desktop\test.bat" "some args
I would guess that since the first space is quoted it actually trying to run the following command:
"C:\Users\Sunbelt\Desktop\test.bat some" args
And in Process Monitor logs there is a file system entry of "NO SUCH FILE" for "C:\Users\Sunbelt\Desktop\test.bat some". I don't know why it is different when run as Admin, but that's what appears to be happening.
To work around this, create another bat file on a path without spaces, and with a filename without spaces, that simply does this:
call "Long path to original bat file\Original bat file.bat"
This secondary bat file can be run as admin.
You can now create a shortcut to this secondary bat file and check run as admin in the shortcut's advanced options. The shortcut can be placed on a path with spaces and it can have a filename containing spaces.
In my case I just want to pass one filename as parameter, but the path has spaces.
I found a solution that worked for this case (if that's okay to truncate the file name).
Create another bat file (input_gate.bat) to remove the spaces in the path using the syntax of CALL.exe.
Assuming that the shortcut is named test.lnk and is on the same route as the input_gate.bat:
call %~sdp0test.lnk %~sf1
This pass as a parameter to test.bat the full file name in short format, with administrator privileges.
%~sdp0 -> Is the current path (for the input_gate.bat) in short format.
%~sf1 -> Is the first parameter passed to input_gate.bat (in my case the full filename with spaces)
This worked for me in Windows 7:
ShortcutTarget: C:\Windows\System32\cmd.exe /C myscript.bat Param1 "Param Two with spaces"
StartIn: "C:\Path containing\my script"
Not tried it yet as Admin. I don't think it would work if myscript.bat contained spaces
I finally figured it out, in a way that allows the use of long filenames (short filenames weren't adequate for my use case). My solution works on Win 7, 8, and 10. I think it was inspired by Luke's mention of the missing double-quote.
Here it is:
1.) For the shortcut you create to the batch file, which you set to run as admin, use the following command line:
cmd /s /c ""path_to_batch_file"
Note that there are 2 double-quote characters at the beginning of the command, and only 1 at the end, even though there should normally be 2 at the end also. But that is the trick in getting it to work!
2.) In your batch file, add back the missing double-quote character:
set user_file=%1"
That's it! Here's an example to let you see it in action:
1.) On your desktop, create "test.bat" with the following content:
#echo off
set user_file=%1"
echo The file is: %user_file%
pause
3.) Create a shortcut to the batch file. Set it to run as admin, and give it the following command line:
cmd /s /c ""%userprofile%\desktop\test.bat"
4.) Drag a file onto the shortcut. Success! (I hope...)
Answer here worked for me: https://superuser.com/questions/931003/passing-variables-with-space-to-the-command-line-as-admin
Which is...
Adding cmd /C before the target.
I also had to make sure my script's name and path didn't have spaces, and not quote the script's path in target.

Resources