BATCH move files to C:\Windows - windows

I'm trying to move sleep.exe and nircmd.exe to C:\Windows using move command in my batch file. The code goes like this
move "%cd%\sleep.exe" "C:\Windows"
move "%cd%\nircmd.exe" "C:\Windows"
When I do not use administrator privileges I get error:
Access denied
But when I use administrator privileges I get error:
System cannot find the path specified
EDIT:
I also tried:
move "%cd%\sleep.exe" "C:\users\%username%\desktop"
And that worked, but as I said I want it to move to C:\Windows

When using administrator it resets the working directory, and which is why it couldn't find the files.
The solution here sets the location of the files to the folder which contains the batch file.
for %%a in ("sleep.exe" "nircmd.exe") do move "%~dp0\%%~a" "C:\Windows"

I didn't know that administrator mode changes your current director (%cd%)
but thanks to #foxidrive i know that now gooled it and found the answer
#setlocal enableextensions
#cd /d "%~dp0"

Related

Batch | Why can't a script running in admin access other scripts? [duplicate]

I have a batch file that is in the same directory as the file I want to xcopy. But for some reason the file is not being found.
I thought that current directory was always where the batch file was located.
I run batch file as administrator. This occurs on a Windows 7 64-bit desktop computer.
Batch file:
#ECHO OFF
XCOPY /y "File1.txt" "File2.txt"
PAUSE
Error:
File not found - File1.txt
0 File(s) copied
Which directory is current working directory on starting a batch file with context menu item Run as administrator depends on User Account Control (UAC) setting for the current user.
This can be demonstrated with following small batch file C:\Temp\Test.bat:
#echo Current directory is: %CD%
#pause
With having selected in User Account Control Settings
Default - Notify me only when programs try to make changes to my computer
Don't notify me when I make changes to Windows settings
and using Run as administrator, Windows uses registry key
HKEY_CLASSES_ROOT\batfile\shell\runasuser\command
This registry key does not contain a default string for executing the batch file. Instead there is the string value DelegateExecute with the CLSID {ea72d00e-4960-42fa-ba92-7792a7944c1d}.
The result is opening a dialog window with title User Account Control and text:
Do you want to allow the following program to make changes to this computer?
Program name: Windows Command Processor
Verified publisher: Microsoft Windows
After confirmation by the user, Windows opens temporarily a new user session like when using on command line RunAs.
In this new user session the current working directory is %SystemRoot%\System32 on executing now the command defined in Windows registry with default string of key
HKEY_CLASSES_ROOT\batfile\shell\runas\command
which is:
%SystemRoot%\System32\cmd.exe /C "%1" %*
Therefore a console window is opened with title C:\Windows\System32\cmd.exe and the 2 lines:
Current directory is: C:\Windows\System32
Press any key to continue . . .
After hitting any key, batch execution finishes which results in closing cmd.exe which results in closing the user session.
But with having selected in User Account Control Settings
Never notify me when
Programs try to install software or make changes to my computer
I make changes to Windows settings
the behavior is different as the user has already elevated privileges.
Now Windows uses directly the command
%SystemRoot%\System32\cmd.exe /C "%1" %*
according to default string of key
HKEY_CLASSES_ROOT\batfile\shell\runas\command
in current user session.
The result is opening a console window also with title C:\Windows\System32\cmd.exe, but displayed in window is:
Current directory is: C:\Temp
Press any key to continue . . .
The current working directory of the parent process (Windows Explorer as desktop) is used for executing of the batch file because no switch to a different user session was necessary in this case.
PA has posted already 2 possible solutions in his answer which I replicate here with a small improvement (pushd with directory in double quotes) and with adding a third one.
Change current directory to directory of batch file using pushd and popd:
pushd "%~dp0"
%SystemRoot%\System32\xcopy.exe "File1.txt" "File2.txt" /Y
popd
This works also for UNC paths. Run in a command prompt window pushd /? for an explanation why this also works for UNC paths.
Use directory of batch file in source and destination specifications:
%SystemRoot%\System32\xcopy.exe "%~dp0File1.txt" "%~dp0File2.txt" /Y
Change working directory to directory of batch file using cd:
cd /D "%~dp0"
%SystemRoot%\System32\xcopy.exe "File1.txt" "File2.txt" /Y
This does not work for UNC paths because command interpreter cmd does not support a UNC path as current directory by default, see for example CMD does not support UNC paths as current directories for details.
The error message is very self explanatory. The file file1.txt is not found.
Because the file name does not include an absolute path, the system tries to find it on the current directory. Your current directory does not contain this file.
Your misconception is that the current directory is not the directory that contains the bat file. Those are two unrelated concepts.
You can easily check by adding this two commands in your bat file
echo BAT directory is %~dp0
echo Current directory is %CD%
you can notice they are different, and that there is a subtle difference in the way the last backslash is appended or not.
So, there are esentially two ways to cope with this problem
either change the current directory to match the expected one
pushd %~dp0
XCOPY /y "File1.txt" "File2.txt"
popd
or specify the full path in the command
XCOPY /y "%~dp0File1.txt" "%~dp0File2.txt"
For the sake of completeness and obscurity, I add another workaround, confirmed as working under Windows 8.1 and expected to work elsewhere, as it relies on documented functionality:
You can change the runas command definition keys
HKEY_CLASSES_ROOT\batfile\shell\runas\command and
HKEY_CLASSES_ROOT\cmdfile\shell\runas\command into
%SystemRoot%\System32\cmd.exe /S /C "(for %%G in (%1) do cd /D "%%~dpG") & "%1"" %*
Which results in the bat or cmd file starting in its containing directory when started using the runas verb, respectively the "Run as Administrator" menu entry.
What the additions to the original command exactly do:
cmd /S strips away first and last (double) quote in command string after /C
for %%G in (%1) do enumerates its single entry, the %1 argument,
making it available for expansion as %%G in the loop body; the letter is arbitrary but some may be "reserved"
%%~dpG expands to the drive and path of %%G, the ~ tilde stripping away quotes if present, which is why we add them back explicitly
cd /D changes both the drive and directory to its argument, and finally
& runs the second command "%1" %* regardless of success of the first one.
You can use pushd which will even support UNC paths, but a stray popd would land any script in the system32 directory, not a behavior I would be fond of.
It should be possible to do this for the exefile entry as well, but frankly, I'd rather live with the inconsistency than to attempt this on my system, as any error there could break a lot.
Enjoy defeating the security mechanics of your operating system :)

Cannot run a batch code as admin privilege

I am using a batch code in removal Disk to install some software (from removal Disk).
My Problem is I need to run this batch code as administrator , but when I ran it as administrator it ran from C:\Windows\system32 but my installer path is different.
This is a sample script :
Setup1.exe /S
Setup2.exe /S
Now how can I change the directory to installer path after run as admin
As per my comment:
You can either add the following to the top of your batch script:
#CD /D "%~dp0"
…or prefix your setup file like this:
"%~dp0setup.exe" /S

How to run a .bat (that runs an .exe that is in the same directory) as administrator from a pendrive?

I'm able to run a .bat (that runs an .exe that is in the same directory) as administrator: I right-click in the bat file and select "Run as administrator".
To be able to do that, I used the following answer: Run exe from current directory in batch
Here's the code:
#echo off
:A
cls
echo This will start the program.
pause
cd %~dp0
start %1myprogram.exe
exit
However, this will only work if the .bat file and the program are in the system drive.
Because if they are, for instance, in a pendrive, and I right-click and select "Run as Administrator", I get the error:
"Windows cannot find 'myprogram.exe'. Make sure you've typed the name correctly, then try again."
Why this happens and how can I fix it?
I thought that by using cd %~dp0 it would always point to the folder in which the bat .file resides.
Thanks in advance.
Solution
Change cd %~dp0 to cd /d %~dp0
Explanation
When you run something with administrator privileges, the working directory changes to:
'C:\Windows\System32'
Although %~dp0 still points to the drive and the directory containing the batch file, cd %~dp0 does not work, because it only changes the directory, but stays on the same drive.
Using the /d parameter, you can tell the cd-command to change the drive, too.
You may need to tell cd to also change drives:
cd /d %~dp0
If the current drive is C: (e.g., the prompt says C:\>), and you do CD D:\FOO, the current directory on drive D: is set to \FOO, but you will still be "on" drive C:. Try the following:
#echo off
:A
cls
echo This will start the program.
pause
cd %~dp0
%~d0
start %1myprogram.exe
exit
(also, why %1myprogram.exe instead of just myprogram.exe, or even just myprogram? If you're right-clicking on the batch file to run it, there isn't going to be a %1.)

Batch File To Change Share Permissions of Windows 10 Folder

I recently upgraded to Windows 10 from 7 on all of my computers, I have 4. I use Cobian backup which used to work fine on windows 7 however on 10 the shares of the folders aren't set correctly and although they say they are shared they don't appear across the network so I cant back them up.
I have however found I can go into each folder and change the permissions manually and they do appear but the problem is I have around 500 folders so I wanted to know if there was a quick command or batch file that could be run to set sharing permissions to everyone for every folder in the parent folder so I don't have to do it individually?
Try toggling the inheritance on the main parent folder. The permissions should trickle down.
You can use the icacls to change the permissions, like
icacls "C:\myFolder" /grant Everyone:M
For changing permissions to all sub directories you can use a for loop and give folder name to the icacls command,
#echo off
set Dir=C:\FolderName
for /d /r "%Dir%" %%a in (*) do (
echo Setting permissions for %%~dpa Folder
icacls %%~dpa /grant Everyone:M
)
Adjust set Dir = C:\FolderName with your path (base Folder).

Why cannot i use batch XCOPY while running in admin mode?

i have run very simple script:
xcopy some.exe c:\folder\ /h/y and it works normally. but when i try to run .bat file with this code as administrator - cmd line opens for a moment but nothing happens (file not copied). Can anyone explain this issue?
i also tried to use echo xcopy instead of xcopy, but nothing had changed.
i need only admin running of .bat file, cause i want to copy file in \windows\system32 folder
when you start a batchfile as administrator, it's working directory is C:\windows\system32\. So your script doesn't find your file. Either work with absolute paths, or change the working directory.
You can change it to the directory, where your batchfile resides with:
cd /d "%~dp0"
Note: to keep the window open to read any errormessages, append a pause command.

Resources