I just created simple batch script. I want to run uninstall.exe with switches like "-q" "-splash Uninstall"
Here is the code;
#echo off
echo This script will uninstall some features.
pause
SET path=C:\Program Files\Program\uninstall.exe -q -splash Uninstall
START "" "%path%"
pause
If I run this code it gives an error:
Windows cannot find 'C:\Program Files\Program\uninstall.exe -q -splash Uninstall'
Make sure you typed the name correctly, and then try again.
If I remove switches, uninstall process starts normally.
So how can I use this swtiches in a batch file?
As an aside, don't use path as an arbitrary choice of variable name. It has a special significance in Windows (and Unix-derived systems too).
Your main problem is that you are including the switches in your quoted string, which is then treated as a whole as the executable filename. Put your quotes only around the filename, and leave the switches outside:
SET command="C:\Program Files\Program\uninstall.exe" -q -splash Uninstall
START "" %command%
(The only reason for the quotes is the fact that the pathname contains spaces.)
Also, you don't really need to use a variable at all, but I've used one since you used one.
I'm not quite sure if every program you come across will have a uninstall.exe file waiting for you in the C:\Program Files(place program name here)\ directory. Even if it does, you will probably have to control it from the GUI. However, looking at another stack overflow thread here, I would like to credit the users Bali C. and PA. for coming up with a possible solution to uninstall files using a batch file by using the registry key to find an uninstall file for windows programs. I will re-paste PA.'s code below:
#echo off
for /f "tokens=*" %%a in ('reg query hklm\software\Microsoft\Windows\CurrentVersion\Uninstall\ ^| find /I "%*"') do (
for /f "tokens=1,2,*" %%b in ('reg query "%%a" /v UninstallString ^| find /I "UninstallString"') do (
if /i %%b==UninstallString (
echo %%d
)
)
)
This code will find the uninstall file for a specific program from the registry, and then it will print out the command needed to run the uninstall file. Remove the 'echo' to just run these commands when you are sure they are correct. However, even this will probably require using the program's uninstall GUI. I don't think this would be terribly inefficient. Is there any other specific reason you want to use a batch file besides efficiency?
I hope this helps!
Related
I'm attempting to replace an old executable on a system that's currently running the program with a new copy of said program. I've been able to make some headway but it seems I've twisted myself up a bit and am now getting errors. Here's what I'm trying to do:
1) Prompt technician for the IP address (this works)
2) Kill the program actively running on the target system (this works)
3) Copy the new executable from the "current directory" to the appropriate place on the target system. (Sideways)
I've attempted many iterations of this but I cannot seem to find a combination that works using either copy or xcopy (attempted the same code using both).
set /p ROOT="Enter Machine IP Address: "
taskkill /S %ROOT% /IM mobileRecorder.exe /F
taskkill /S %ROOT% /IM mobileMenu.exe /F
set TARGET=\\%ROOT%\C$\Program Files\MobileRecorder\
xcopy mobileRecorder.exe %TARGET%
The above yields "Invalid number of parameters" error (using COPY it reads, "The syntax of the command is incorrect")
I have also attempted to combine the last two lines using:
xcopy mobileRecorder.exe \\%ROOT%\C$\Program Files\MobileRecorder\
And:
xcopy .\mobileRecorder.exe \\%ROOT%\C$\Program Files\MobileRecorder\
Any assistance would be greatly appreciated!
xcopy mobileRecorder.exe "%TARGET%"
Since target contains a space, you need to group the string to provide a single token.
I have a batch script that needs to run as admin. I will be distributing to users so it would be best if they can run it from Windows Explorer.
Unfortunately, it doesn't work when run from explorer (right click -> run as admin). It does work when called from a pre-existing admin terminal.
Initially I thought the problem was with the active directory, but I added a "cd /d %~dp0" as the first command. I confirmed through echo that this places them both in the same directory, but it still fails when running from explorer.
The failure occurs when reading an external file in the same directory as the .bat. It pulls empty strings when run from explorer. Here is sample code:
rem Make sure active directory is correct (verified that this works)
cd /d %~dp0
rem Load parameters from params.txt
for /f "delims== tokens=1,2" %%G in ("params.txt") do set %%G=%%H
rem Print params (it's a loop so you can read it when running from expl.)
for /l %%a in (1 1 100000) do echo %DST%
Then you just need to make sure params.txt is in same directory as .bat and includes the line "DST=some\directory\name"
Anybody know why this doesn't work?
As has been pointed about by #nephi12 in his answer if your file name does not have spaces you can remove the quotes, otherwise it thinks the IN clause is a string you want to parse. If you need to quote your file names then you need to use the USEBACKQ option as pointed out by the comments. Once you use that option your code works just fine.
But I would like to make a point with your code. If the contents of your params.txt file is:
"DST=some\directory\name"
Then your FOR command can just be this:
for /f "usebackq tokens=1 delims=" %%G in ("params.txt") do set %%G
I am not understanding why you are echoing the %dst% variable 100,000 times?
For one thing, take away the "s from around params.txt as double-quotes means string parsing, while unquoted is a list of files.
Second, try prepending params.txt with %~pd0\ to ensure the correct path, rather than changing directory.
I am kind of new to cmd for I am a linux guy. In the code bellow I am trying to find a .mp3 or .mp4 file and save there path to a file and then move the .mp3/.mp4 into the file. I get a syntax error on the mkdir command
ECHO off
pause
mkdir "/Users/media"
pause
cd /Users/
dir *.mp3 >Users/media/output.txt/s
dir *.mp4 >>Users/media/output.txt/s
pause
for /r %%a IN (*.mp3) do (
move /y "%%a" "/Users/media"
)
pause
for /r %%a IN (*.mp4) do (
move /y "%%a" "/Users/media"
)
thanks any help would be appreciated
Windows supports forward slashes in many scenarios but prefers backslashes. So you should change the appropriate line to
mkdir \Users\media
If your path contains spaces you have to surround it with quotation marks. In case the users directory does not exist you can add a -p to the command which will have it create the complete hierarchy you specify.
Depending on how you use the batch you might want to add a drive letter to your path and check the errorlevel of the mkdir command.
Read more about mkdir here, this site lists the other available commands too.
As you are coming from Linux I want to mention that bash and others can be installed on Windows too, there even are UNIX "emulations" like Cygwin. There are alternatives to batches, for example Windows scripting host which looks more like regular programming and adds support for vbscript and JavaScript. Or you have a look at powershell.
Both alternatives create (but I am maybe biased) better, more readable and maintainable code. Batches are often a pain to those that follow you and have to understand and change.
I have always used start /w in batch files and from a Windows console to run something and pause till the application is closed. But since a few weeks ago, this does not work anymore!
I tried to simply open one by one the .pdf files in a folder and it doesn't work. So I studied for at least 3 hours what could be wrong to no avail. And nobody on the Internet seems to mention a problem like that. Today I picked a batch I used in the past to open files in sequence and it doesn't work anymore either. It would use two simple batches, the core one doing just this:
cd %1
for %%f in (*.py) do start /wait %%f
cd ..
I am pretty sure I used it successfully on the same machine I use now (Win7 Professional, 64-bit). I tried all sorts of things like call, command /b with the command, but none of them works.
From the console when I do ver I get Microsoft Windows [Version 6.1.7601] (from the 32 or the 64-bit console).
What do you think has gone wrong here?
In one of your comments, you show that the following specific command fails from the command line:
for %a in (*.pdf) do start /wait "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" %a
It doesn't work properly because the first argument is treated as a title if it is quoted. If you need to quote your executable, then you must precede the program with a quoted title. You can provide an empty title if you want:
for %a in (*.pdf) do start /wait "" "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" %a
Like the title says, I'm trying to take the output from a FIND command and save it to a variable. Specifically, I'm using:
DIR /b /s "C:\" | FIND "someexe.exe"
to locate a specific .exe file, which seems to work fine, but then I want to save the results of FIND to use later in the same script.
I've tried various different tweaks of:
for /f "usebackq" %%i in (`DIR /b /s "C:\" | FIND "someexe.exe"`) do SET foobar=%%i
but when I try to run the script the command window immediately closes (presumably due to some error, I tried putting a PAUSE command in the next line to no avail).
I assume it's some stupid minor thing that I'm doing wrong but if someone could show me what it is I'd appreciate it. Just for further reference, I don't care how many copies of "someexe.exe" exist, I just need the path for one of them.
You should be getting this error: | was unexpected at this time.. Your immediate problem is unquoted special characters like | must be escaped using ^ when they appear in a FOR /F ('command').
for /f "usebackq" %%i in (`DIR /b /s "C:\" ^| FIND "someexe.exe"`) do SET foobar=%%i
It sounds like you are running your batch file by double clicking from either your desktop or Windows Explorer. That works, but then the window immediately closes after the batch terminates. In your case it terminates before reaching PAUSE because of the syntax error.
I always run my batch files from a command window: From the Start menu you want to run cmd.exe. That will open up a command console. Then CD to the directory where your batch file resides and then run the batch file by typing its name (no extension needed). Now the window stays open after the script terminates. You can examine your variables using SET, run another script, whatever.
There is no need to use FIND in your case - DIR can find the file directly. Also, the path of your file may include spaces, in which case it will be parsed into tokens. You need to set "DELIMS=" or "TOKENS=*" so that you get the complete path.
I never understand why people use USEBACKQ when they are executing a command. I only find it useful if I am trying to use FOR /F with a file and I need to enclose the file in quotes because of spaces and/or special characters.
Also, you may run across errors due to inaccessible directories. Redirecting stderr to nul cleans up the output. Here again, the > must be escaped.
for /f "delims=" %%F in ('dir /b /s "c:\someexe.exe" 2^>nul') do set foobar=%%F