Problems with Ampersand start as “Run as administrator” - batch-file

I wonder how do I run a .bat or .cmd file as an administrator within a folder where the name contains the same Ampersand "&"? every time I try to run the file appears an error ... at what I did when I try to run as administrator cmd understand that & the folder name is a command.
Let me illustrate, I have a folder called Projects in Cmd & vbs and a file called Project 01.bat when I try to run it as administrator until an error occurs q I remove it from the folder that contains the "&"
if the Cmd & vbs.
Folder name: Cmd & Vbs
Script name: Project 01.bat
Form of execution: Option “Run as administered” in the context menu
Version of windows: 7
Example Script:
#Echo Off
Title Remover - [WMP] Buy online music
Color 4f
reg delete "HKEY_CLASSES_ROOT\SystemFileAssociations\Directory.Audio\shellex\ContextMenuHandlers\WMPShopMusic" /f>nul 2>&1
echo msgbox"Removed!",vbinformation> %temp%\Msg.vbs
start /wait %temp%\Msg.vbs>nul 2>&1
Del %temp%\Msg.vbs>nul 2>&1
Exit

Bill is correct, for DOS to work with the & character, you need to escape it using ^ an example might help:
C:\Temp>cd my&test
The system cannot find the path specified.
'test' is not recognized as an internal or external command,
operable program or batch file.
C:\Temp>cd my^&test
C:\Temp\my&test>

I discovered that this problem is in runas configuration and the correction is below:
Browse the regedit to:
HKEY_CLASSES_ROOT\batfile\shell\runas\command
HKEY_CLASSES_ROOT\cmdfile\shell\runas\command
Changes the value:
%SystemRoot%\System32\cmd.exe /C "%1" %*
To:
%SystemRoot%\System32\cmd.exe /C ""%1"" %*

Related

Run all SQL files in multiple directories

I combined a couple of solutions I found online to try and make this happen.
https://stackoverflow.com/a/6504317/2471473
https://sqlandme.com/2013/03/25/sql-server-executing-multiple-script-files-using-sqlcmd/
I'm trying to run a single .cmd script (Script1.cmd) with folder locations of .sql files. That single script runs another script (Script2.cmd) to use sqlcmd to execute all the .sql files in that folder location. It mostly works, but it leaves a command window open that I have to exit from for each folder location.
Script1.cmd
start Script2.cmd "C:\Location1"
start Script2.cmd "C:\Location2"
Script2.cmd
#Echo Off
FOR /f %%i IN ('DIR %1\*.Sql /B') do call :RunScript %1 %%i
GOTO :END
:RunScript
Echo Executing Script: %2
cd %1
SQLCMD -S Server123 -d Database456 -E -i %2
Echo Completed Script: %2
:END
Official command line reference for Windows XP or for Windows Server 2003, Windows Vista (and above) seems to be too brief. Read this (extended) start command documentation:
Syntax: START "title" [/D path] [options] "command" [parameters]
Always include a TITLE this can be a simple string like "My Script" or
just a pair of empty quotes "". According to the Microsoft
documentation, the title is optional, but depending on the other
options chosen you can have problems if it is omitted.
If command is an internal cmd command or a batch file then the command
processor is run with the /K switch to cmd.exe. This means that the
window will remain after the command has been run.
Next Script1.cmd should work and close started command windows:
start "" cmd /C Script2.cmd "C:\Location1"
start "" cmd /C Script2.cmd "C:\Location2"

Cannot start .exe file propetly because config

So...I have "launcher"..
Its .bat file and i want it to start /ffa/server.exe
but..In ffa/ i have config file.
When i start server via launcher it starts server but it makes new config file
in directory of launcher..How can i fix this?
And "server" its: https://github.com/OgarProject/Ogar
start cmd /k %~dp0\ffa\server.exe
Please help me, its really frustrating..Thanks <3!
You can use pushd to move to the correct directory, start the server, and then pop back:
pushd %~dp0\ffa
start cmd /k server.exe
popd
I'm not familiar with the exact folder structure you're working with, and exactly how you call the script, but you definitely could use push/popd for this.
There are more possible solutions to change working directory of started command prompt window:
In calling script
pushd %~dp0\ffa
start "" cmd /k server.exe
popd
In started cmd itself: note properly escaped & character (see redirection)
start "" cmd /k pushd %~dp0\ffa^&server.exe
Using /D parameter of start command
start "" /D "%~dp0\ffa" cmd /k server.exe

Start Shortcut (.ink) as administrator in Win8.1: Folder name contains symbol (& or =) error

The folder name of my folder contains the ampersand symbol "&", and in that folder i have at batchfile, batchfile.bat.
Lets say the path is:
C:\Users\%username%\Desktop\1 & 2\batchfile.bat
When creating af shortcut to run that batchfile, when not run as administrator, it works. But as soon as i run the shortcut as administrator, it will not open batchfile.bat and gives the error:
'C:\Users\%username%\Desktop\1' is not recognized as an internal or external command,
operable program or batch file.
Is there a way to bypass the ampersand (&), and run the program as administrator, whithout changing the name of the folder?
Adapt the target field of the shortcut from
"C:\Users\%username%\Desktop\1 & 2\batchfile.bat"
to (here /K switch for debugging purposes, the cmd window should stay open)
C:\Windows\System32\cmd.exe /K call "C:\Users\%username%\Desktop\1 & 2\batchfile.bat"
and finally to
C:\Windows\System32\cmd.exe /C call "C:\Users\%username%\Desktop\1 & 2\batchfile.bat"
Tested with simplified path "D:\bat\1 & 2\batchfile.bat" as follows:
The script is as follows:
#ECHO OFF >NUL
#SETLOCAL enableextensions
set "xx=%~f0"
echo stackoverflow 28077086 %xx:&=^&%
#ENDLOCAL
goto :eof
"C:\Users\%username%\Desktop\1 & 2\batchfile.bat"
That is "quoting the string" should cure thr problem.

How do I execute cmd commands through a batch file?

I want to write a batch file that will do following things in given order:
Open cmd
Run cmd command cd c:\Program files\IIS Express
Run cmd command iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
Open Internet Explorer 8 with URL= http://localhost:8088/default.aspx
Note: The cmd window should not be closed after executing the commands.
I tried start cmd.exe /k "cd\ & cd ProgramFiles\IIS Express", but it is not solving my purpose.
So, make an actual batch file: open up notepad, type the commands you want to run, and save as a .bat file. Then double click the .bat file to run it.
Try something like this for a start:
c:\
cd c:\Program files\IIS Express
start iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
start http://localhost:8088/default.aspx
pause
I think the correct syntax is:
cmd /k "cd c:\<folder name>"
This fixes some issues with Blorgbeard's answer (but is untested):
#echo off
cd /d "c:\Program files\IIS Express"
start "" iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
timeout 10
start http://localhost:8088/default.aspx
pause
cmd /c "command" syntax works well. Also, if you want to include an executable that contains a space in the path, you will need two sets of quotes.
cmd /c ""path to executable""
and if your executable needs a file input with a space in the path a another set
cmd /c ""path to executable" -f "path to file""
#echo off
title Command Executer
color 1b
echo Command Executer by: YourNameHere
echo #################################
: execute
echo Please Type A Command Here:
set /p cmd=Command:
%cmd%
goto execute
start cmd /k "your cmd command1"
start cmd /k "your cmd command2"
It works in Windows server2012 while I use these command in one batch file.
cmd /k cd c:\
is the right answer
I was trying to run a couple of batch files parallely at startup, if a condition was true.
For this I made a parent batch file which should have checked for the condition and invoke the other child batch files if the condition was true.
I tried to achieve it via START but it gave me an empty black command prompt running in the directory of children batch files, instead of running the children batch files themselves
The thing which worked for me was by using a combination of START and CALL
As an example
condition ...
start call "C:\Users\Amd\conn\wsl_setup - conn1.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn2.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn3.bat"
I know DOS and cmd prompt DOES NOT LIKE spaces in folder names. Your code starts with
cd c:\Program files\IIS Express
and it's trying to go to c:\Program in stead of C:\"Program Files"
Change the folder name and *.exe name. Hope this helps

Start cmd.exe and change directory?

I'd like a .bat file which is stored on the desktop to perform 2 simple tasks:
1. Start cmd.exe
2. Change directory to c:\executionsdktest_10.2.2
I have:
#echo off
start cmd.exe \k
cdsdad c:\ExecutionSDKTest_10.2.2
But when I double click the .bat file, this starts cmd.exe but cd to c:\users\qestester\desktop. ANy ideas?
You can use
cmd /k "cd /d c:\ExecutionSDKTest_10.2.2"
And you wouldn't need a batch file for that. This can be put in a normal shortcut.
And if you have a normal shortcut you can just specify its working directory and run cmd directly without any arguments.
This worked for me:
start cmd.exe #cmd /k "cd /d C:\Users\Michael && node test.js"
What I needed ... was from a localhost page served in PHP, open a terminal, change directory, and start a node script. Achieved like this:
pclose(popen("start /B ". $cmd, "r"));
.. where $cmd is the first string above.
start cmd.exe /k "C: && cd \ExecutionSDKTest_10.2.2"
I wanted to:
open a command prompt
change directory (go to another directory)
run a command from there
solution that worked for me:
#echo off
d:
cd\Path\to\wherever
my command
Notes:
the d: after #echo off tells that the path is on the D drive. Write a c instead of the d and You will be on drive C.
third line starts with the mandatory cd, after which starts the path, with a preceding \.
my command can be anything you want, and can contain more that one word (or only one). I used it to run jupyter notebook.

Resources