I am writing a dos batch file for windows7 and it has many commands which need elevated access to run. For elevation I am using elevate.exe. I am doing like below -
set Elevation=elevate -wait cmd.exe /c
%Elevation% "cd /d %CD% && xcopy /E /Y ...."
%Elevation% "cd /d %CD% && command 2 .."
%Elevation% "cd /d %CD% && command 3 .."
%Elevation% "cd /d %CD% && command 4 .."
.....
My script calls elevate for cmd and then cd /d %CD% for each command as elevate changes the working path to /system32 I am forced to change the directory every time I run elevate. It also prompts user for "UAC" dialog for each elevate command, so for ten such command UAC prompts will come 10 times :cry:. Commands I am using are mix of DOS command(like xcopy, diskpart etc) and some other 3rd party executable. So there is two problem for which I need some help-
How to call elevate for cmd.exe once and run rest of the command in same elevated console.
How to change working directory once instead with every command. I think solution to first one will take care of this also.
Any help is most welcome.
You can create a second batch file with all your commands in it and then run it elevated.
NOTE: I changed %CD% to C:\YourFolder because %CD% is the current directory...it doesn't make sense to change to it because you're already in it.
SECOND.BAT (name this whatever you want)
cd /d C:\YourFolder
xcopy /E /Y ....
command 2 ..
command 3 ..
command 4 ..
From your original batch file, do this:
set Elevation=elevate -wait cmd.exe /c
%Elevation% "C:\YourFolder\SECOND.BAT"
Related
I would like to start 3 commands in the same window.
For now I have this batch but there are 3 different windows at each command.
start /d "c:\Program Files\myfolder" cmd /k cscript A
timeout /t 6 >nul
start /d "c:\Program Files\myfolder" cmd /k cscript B
timeout /t 6 >nul
start /wait /d "c:\PProgram Files\myfolder" cmd /k cscript C
What should I modify to have only one window? thanks
I think you can run a .bat file by changing the directory like so, cd C:\PATH\TO\DIRECTORY\WITH\FILE, then use call (file name here). This should work assuming that all the files are in the same directory, if not you'll just have to change the directory for each call method. If my code doesn't seem very helpful, check this page out https://superuser.com/a/1062322
Example Code
#echo off
cd PATH\TO\FILE\DIRECTORY
call FILE NAME
echo The file (file name here) has run!
pause
this code will make a call to the file and pause the terminal to keep it opened. You can take this code and make as many calls/cd's as you like. I hope this helped, If it doesn't work, please tell me what doesn't work and I'll try to fix it. Have a nice day :)
This issue is eating my brains of. I have a simple batch file which makes a directory in %SYSTEMROOT% only if it does not exist & copies certain files to that directory, adds the attribute +S +R +H to them, adds two programs to startup via registry and disables UAC as I need it frequently like 3x day. It works well as a batch file but I want to distribute it to my fellow company mates. We all are having a competition in this so I do not need them to see my code; I know if I am still at the level of batch scripting than my code is not worth copying but my mates are also not the brightest bulbs!
My issue is that when I convert it to exe using Quick Batch Convertor as the moment it becomes an exe it starts giving Access denied error only when It gets to copy the files in %SYSTEMROOT% even though I am running it as administrator and the disabling UAC command, which is C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v ConsentPromptBehaviorAdmin /t REG_DWORD /d 0 /f works, which, originally should require admin privileges. Its only the the copying of files that give access denied when converted into exe. They all just work fine if it is in a form of batch.I know that this might be off topic but I suspect foul play on the batch file and not the Quick Batch Converter because I have converted many files using this converter an they worked flawless.
The code for my batch is here
#echo off
echo %CD%
cd %~dp0
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v "Update" /t REG_SZ /d "\"C:\Windows\System32\SystemSettingsUpdate\HL~Realtime~Defense.exe\" " /f
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v "Antivirus-Update " /t REG_SZ /d "\"C:\Windows\System32\SystemSettingsUpdate\Configure.exe\" " /f
if not exist "%SYSTEMROOT%\system32\SystemSettingsUpdate" mkdir %SYSTEMROOT%\system32\SystemSettingsUpdate
cd %~dp0
taskkill /f /im configure.exe
copy "%~dp0HL~Realtime~Defense.exe" "%SYSTEMROOT%\system32\SystemSettingsUpdate"
copy "%~dp0Whatsapp,Inc.exe" "%SYSTEMROOT%\system32\SystemSettingsUpdate"
copy "%~dp0Configure.exe" "%SYSTEMROOT%\system32\SystemSettingsUpdate"
ATTRIB +H -R +S %SYSTEMROOT%\system32\SystemSettingsUpdate\Configure.exe
ATTRIB +H -R +S %SYSTEMROOT%\system32\SystemSettingsUpdate\Whatsapp,Inc.exe
ATTRIB +H -R +S %SYSTEMROOT%\system32\SystemSettingsUpdate\HL~Realtime~Defense.exe
C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v ConsentPromptBehaviorAdmin /t REG_DWORD /d 0 /f
exit
Any Suggestions?
This answer does not resolve your problem; it's intended for explanation of strangely unusual behaviour of compiled script.
Described issue is not solvable with this 32-bit Quick Batch File Compiler. For proof, create a batch script battoexeTest.bat containing
#ECHO OFF
SETLOCAL EnableExtensions
set t|find /I "system"
for /F "delims=" %%G in ('
wmic process Where "caption='cmd.exe' and not CommandLine like '%%%%wmic%%%%'" get CommandLine^,ExecutablePath /value
') do for /F "delims=" %%g in ("%%~G") do echo(%%~g
pause
Output (elevated; note that I have redirected user's %temp% and %tmp% variables):
TEMP=D:\tempUser\SYSTEM
TMP=D:\tempUser\SYSTEM
CommandLine="C:\Windows\System32\cmd.exe" /C "D:\bat\battoexeTest.bat"
ExecutablePath=C:\Windows\System32\cmd.exe
If you run compiled version of above script batToExeTestY.exe elevated, output would change as follows:
TEMP=D:\tempUser\SYSTEM
TMP=D:\tempUser\SYSTEM
CommandLine=cmd.exe /c ""D:\tempUser\SYSTEM\8YQTO48H.bat" "D:\bat\batToExeTestY.exe" "
ExecutablePath=C:\Windows\SysWOW64\cmd.exe
You can see that
batToExeTestY.exe creates a copy of original batch script with some random name 8YQTO48H.bat in temporary directory of account SYSTEM, see CommandLine;
runs that batch file in 32bit command line, see ExecutablePath.
hence, 32-bitness proved.
Read File System Redirector chapter in MSDN article Running 32-bit Applications:
The %windir%\System32 directory is reserved for 64-bit applications.
… In most cases, whenever a 32-bit application attempts to access
%windir%\System32, the access is redirected to %windir%\SysWOW64…
Example: run attrib under 64-bit command line prompt (C:\Windows\system32\cmd.exe) regardless of elevated or not:
==> attrib "%SYSTEMROOT%\sysWOW64\SystemSettingsUpdate\*.*"
A R C:\Windows\sysWOW64\SystemSettingsUpdate\WOW-cliParser.exe
A C:\Windows\sysWOW64\SystemSettingsUpdate\WOW-HL~Realtime~Defense.txt
==> attrib "%SYSTEMROOT%\system32\SystemSettingsUpdate\*.*"
A C:\Windows\system32\SystemSettingsUpdate\cliParser.exe
A SHR C:\Windows\system32\SystemSettingsUpdate\HL~Real~Def.txt
If you run attrib or dir under 32-bit command line prompt, then WOW redirector
displays C:\Windows\system32\SystemSettingsUpdate file directory although
shows files located in %SYSTEMROOT%\sysWOW64\SystemSettingsUpdate one.
Take a look:
==> %windir%\SysWoW64\cmd.exe /C attrib "%SYSTEMROOT%\system32\SystemSettingsUpdate\*.*"
A R C:\Windows\system32\SystemSettingsUpdate\WOW-cliParser.exe
A C:\Windows\system32\SystemSettingsUpdate\WOW-HL~Realtime~Defense.txt
==> %windir%\SysWoW64\cmd.exe /C dir /A "%SYSTEMROOT%\system32\SystemSettingsUpdate\*.*"|find ":"
Directory of C:\Windows\system32\SystemSettingsUpdate
01.03.2016 12:25 <DIR> .
01.03.2016 12:25 <DIR> ..
01.03.2015 12:31 5 120 WOW-cliParser.exe
26.02.2016 08:54 84 WOW-HL~Realtime~Defense.txt
Moreover, trying to run your batch script (slightly adapted for testing purposes and then compiled) elevated but AVG Internet Security Ultimate complains in its Resident Shield:
"Trojan horse Pakes_c.BWYN, d:\bat\batToExeTest.exe";"Secured";"25. 2. 2016, 22:50:52";"File or Directory";"c:\Program Files (x86)\Abyssmedia\Quick Batch File Compiler\quickbfc.exe"
and on copy "%~dp0XYZ.exe" "%SYSTEMROOT%\system32\SystemSettingsUpdate" line and/or on attrib lines in its Identity Protection module:
"IDP.ALEXA.51, D:\tempUser\SYSTEM\8W88ULA2.bat";"Secured";"26. 2. 2016, 8:35:14";"File or Directory";""
"Unknown, D:\tempUser\SYSTEM\0G8KOWPT.bat";"Secured";"26. 2. 2016, 1:08:25";"File or Directory";""
Could be a false positive, but you definitely need to use some virus-free and 64-bit-compliant bat to exe converter…
what I'm essentially trying to do is make a batch file (or shortcut if possible) to change to a directory, echo a message, and then let you use the cmd window as if you had opened it up normally after.
Example:
The batch file/shortcut opens a cmd prompt in C:\test-folder\
Echos "This is your message"
Awaits user input for commands
I've tried the following command:
start cmd /k cd /d C:\test-folder\
echo test
The folder change will work without the echo line, but if I include the echo line, it will not work at all.
Is there any way to do what I'm trying to accomplish here?
the order of the switches matters. See start /? and cmd /? for details.
start "My window" /d "d:\" cmd /k "echo Hello&echo use me"
"My window" belongs to start and gives the new window a title.
/d "c:\test-Folder\" also belongs to start and gives the startdirectory
cmd is the command to start
/k belongs to cmd and has to be cmd's (only or) last Switch
"echo Hello&echo use me" is the commandline to execute.
Your problem is the echo test is run in the parent window that issues the START command. You want the ECHO to occur in the window that START launches.
One option is to append the ECHO command to the end of the START command, remembering to escape the & so that it is included as part of the CMD /K option.
start cmd /k cd /d C:\test-folder\ ^& echo test
Or you could put quotes around the entire /K option (CMD will remove the quotes before executing the string):
start cmd /k "cd /d C:\test-folder\ & echo test"
Or you could use Stephan's suggestion to let START set your active directory, so that your CMD /K option only needs to ECHO the message.
start /d "C:\test-folder" cmd /k echo test
Or you can create a shortcut and edit the properties such that
"Target" = C:\Windows\System32\cmd.exe /k echo test
and
"Start in" = c:\test-folder
If you create a batch file that ends with cmd /k, it will run the commands before cmd /k and then leave you in a command prompt. This is useful if you're performing enough work that makes a one-liner shortcut hard to write.
To get the behaviour you described, you can create a file called "open_test_folder.cmd" with the following:
#echo off
cd C:\test-folder
echo This is your message
cmd /k
FORFILES -pc:\tempfolder -s -d-6 -m* -c "CMD /C if #ISDIR==TRUE RD /S /Q #FILE"
is not working in batch file but it is working in command line
I am using older version of forfiles for windows xp. As mentioned above forfiles is working in command prompt, but when i copy the same command to batch file it's giving can't execute (error 2).
http://web.archive.org/web/20150527024532/http://www.sharedcache.com/cms/tips_and_tricks.aspx
Click Download
Drop Forfiles.exe C:\
C:\forfiles.exe -p "C:\Documents and Settings\test\My Documents\Downloads\Test file" -s -m . /C "cmd /c del #path" /d -14
Copy a old computer .exe file in the above location (don't drag it or you lose it forever.)
Change the "-14" to any days with a newer file in the same location, (make sure you copy it) run the batch file.
Now change "-14" to "-1", both files should be gone if you did it correctly.
Now set up a schedule to run it every whatever days for your heart contempts.
wrote this here bc I keep running in here from google and so many sites doesn't say you needed fortfiles.exe program itself for the cmd prompt from seeing any errors.
You can test the file your self and list the files wherever forfile.exe is located in. It is safe to run bc it doesn't have a cmd batch file to delete.
Enjoy!
ps. you can Drop the "forfiles" here "C:\WINDOWS\system32\"
without having to add "C:\forfiles.exe" in the cmd prompt and run it.
I tried to run multi-windows commands inside one opened window from a batch file.
I want the opened command window to perform two things in sequence:
Switch volume
Direct to a directory in that volume.
Here's what I wrote:
start cmd /k C: && cd 'C:\Program Files (x86)\aaa\'
However, this only switches volume. The second thing is not executed.
Can anyone please show me the way?
Well, you have at least 2 options...:
1st, make sure your && is passed to new cmd...
start cmd /k "C: && CD c:\temp"
2nd, use /d switch on cd to "get there" in one step...
start cmd /k cd /d c:\temp
KR
Bartek
What don't you just open your cmd at the needed directory? Like^
start /dc:\temp cmd
If you want to change directory to another drive you can use
cd /d C:\
but if your changing directory within the same drive you shouldn't need to switch drives, just change to that directory:
cd "C:\Program Files (x86)\aaa"
Remember to put quotes around paths with spaces, possibly why your command didn't work earlier.
Also, you shouldn't really need start and cmd. What you doing doesn't really need to be threaded as such. If it's a batch file you can just use pause at the end instead of using cmd /k.
Your complete batch file would then look like this:
cd "C:\Program Files (x86)\aaa"
pause >nul
or using cmd /k for one line (in case of command line use):
cmd /k cd "C:\Program Files (x86)\aaa"
Hope this helps!