Run a script file as administrator - windows-7

I have this .vbs script that I am trying to run on windows 7.
It has to run with full permissions and it has to do it automatically.
To be clear When the user double clicks on the file it will get the prompt that asks "to allow to run the file as an administrator", and then run with full permissions.
To do this I created a batch file (run.bat) that calls the script file
cscript "V02.vbs"
pause
then I created a shortcut for the batch file which I can choose to run as admin.
The problem I encounter now is that when I run the batch file as admin the folder changes to c\windows\system32. The script, batch file and shortcut are all in the same folder. is there a way to get the folder location?

I may have misunderstood...
The easiest solution would be to "hardcode" that path into the patch file with a -
cd \path\to\my\script
cscript myscript.vbs
pause

Use "modifiers with batch parameters". From the linked documentation:
%~dp1 Expands %1 to a drive letter and path.
Since %0 is your script, %~dp0 is what you want:
cscript "%~dp0V02.vbs"
pause
Note that %~dp0 includes the trailing backslash, so your v02.vbs file name above becomes quite hard to read.

Related

Batch file replacing issue

I'm trying to change my group policies by replacing the scripts.ini file in C:\Windows\System32\GroupPolicy\Machine\Scripts by using a batch file. The batch file is on my desktop in a folder called replacer, the custom scripts.ini is in the same folder. When i right click the batch file and "Run as administrator" it suddenly can't find the scripts.ini file that's in the same folder. When i don't run as administrator it finds it, but can't replace the scripts.ini file in group policies.
Edit:
Here's the code(1 line):
xcopy /s/y scripts.ini C:\Windows\System32\GroupPolicy\Machine\Scripts
When you run a batch script by double clicking it, the current directory will be the folder where the script resides.
But when you run the script as Administrator by right mouse clicking, then the current directory is something else, typically C:\wINDOWS\system32.
Your script can use %~dp0 to get the full path of where the script is installed, so you can simply prefix your source file with that path:
xcopy /s/y "%~dp0scripts.ini" C:\Windows\System32\GroupPolicy\Machine\Scripts
If you have additional commands that depend on the current directory, then I suggest you use PUSHD to change your current directory instead
pushd "%~dp0"
xcopy /s/y scripts.ini C:\Windows\System32\GroupPolicy\Machine\Scripts

VBScript is running batch file from the file path of VBScript, not path of batch

I am having a problem with a VBScript which was working just fine yesterday, but for some reason does not work today. I am attempting to run a batch file off the sever, from a VBScript in an adjacent server folder. The VBScript reads like this:
Dim Shell
Set Shell = CreateObject ("WScript.Shell")
Shell.run "cmd /K ..\Analyses\GeniE_SP.bat"
The VBScript is in a file in the folder ..\Local Design Calculations, whereas the batch file is in ..\Analyses. The script is calling up the batch file just fine, but when the batch file runs it is running with the file path of the VBScript. This is what is shown in the command window:
I:\Shared\Projects\105874\Local Design Calculations>REM #echo off
If I simply run the batch file from its actual location, there are no problems as the first line in the command window will read like this (as it should):
I:\Shared\Projects\105874\Analyses>REM #echo off
Any ideas?
Shell.run "cmd /K cd ..\Analyses & GeniE_SP.bat"
If the current active directory for the batch file is a requirement, first change it.
You would specify the full path to what you want. There in no such thing as per drive default directories in WinNT. Graphical programs (ie normal) set their own default directory based on the folder the exe is in (and that's not a per drive default but a program default).
For console programs (ie non graphical) cmd.exe simulates per drive default directories.
SPECIFY FULL PATHS.

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.

Run exe from current directory in batch

I'm trying to run 2 *.exe files from whatever directory the batch file is located in. Commands:
#echo off
:A
cls
Echo programs
start %1myprogram.exe
start %1myprogram1.exe
exit
This works perfect when I open my batch file simply by double clicking it, but it doesn't work when I run the batch file as administrator. I need to do this as the two exe's have to have administrator privileges. I suspect this error occurs because it runs the batch file as if it were in the SYSTEM32 folder. Is this correct?
Thanks for your help!
Erik
Although the answer is in the comments, it should be as an actual answer so that this question is removed from the "unanswered" list.
#echo off
:A
cls
echo programs
cd %~dp0
start %1myprogram.exe
start %1myprogram1.exe
exit

A batch file that copies another into Start Up folder?

I am making a batch file that needs to copy another batch file into the Start Menu Start Up folder (the one used when a program launches on login/start up). Since the path uses the user's computer name eg. C:\Documents and Settings\User Name I need the batch file to get the user's correct name instead of the "User Name" or * (wildcard). Wildcards doesn't work as the batch file comes up with "the filename directory name or volume label syntax is incorrect".
I hope this is clear enough.
You can also try this:
cd %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup
It works in Windows 10. The %appdata% variable gives you your required username by default
Open a new command prompt window by executing cmd.exe or using the shortcut in Accessories in Windows start menu. Enter set and look on the list of environment variables predefined by Windows. You are mostly interested in USERPROFILE.
The following command can be used to copy a batch file with name AaRM.bat from a folder available for all users like the all users desktop folder to startup folder of the currently logged in user.
copy "%ALLUSERSPROFILE%\Desktop\AaRM.bat" "%USERPROFILE%\Start Menu\Programs\Startup"
The double quotes are important as the name of the batch file with path and the path to the startup folder both contain spaces.
Copying the batch file from your desktop folder to the startup folder of the other user is most likely not possible as the other user might have no permission to access anything in your user profile directory and below.
You can copy the batch file to distribute also to a different folder accessible for all users like "%ProgramFiles%" or %SystemRoot% as the batch file in all users desktop folder is visible for all user accounts on desktop.
Best would be to put the batch file into Windows directory (%SystemRoot% or %windir%) and create / copy a shortcut file (*.lnk) in / to startup folder of the other user accounts. The Windows start menu folders should contain only *.lnk files and not batch files and applications.
And last it would be also possible to create a shortcut in "%ALLUSERSPROFILE%\Start Menu\Programs\Startup" to the batch file in %windir% to execute this batch file for any user who logs in on this computer. Your batch file could contain at top something like if "%USERNAME%"=="your account name" goto :EOF 1 or more times with various user account names to prevent doing anything for 1 or more specific users.
try the following command if u r using win 7. never tried on win 8 though.
cd C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
works perfectly for me.
Stumped about nobody answering yet..
Why are we so lost?
echo %userprofile%
To know the name of current user
For copying
Copy /y %~f0 "%USERPROFILE%\%AppData%\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
Alternative
copy "path of file you want to copy" "path of the directory where you want it copied"
copy path of batch file path of startup folder
Copy /y "File-Address-to-copy-here" "where-to-copy-to-here"
(as above)
then save file as .bat
and you have a batch file, run as admin if in system directories.
copy "C:\Documents and Settings\%username%\Desktop\batch.bat" "C:\Documents and Settings\%username%\Start Menu\Programs\Startup\"
This should work.
This works:
#echo off
copy "C:\Users\%username%\Desktop\somefolder\example.bat" "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"
Try this:
copy "CopyPath" "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup"
Also, if you want to skip the username for the copy path, then use %username% instead of the actual username.
#echo off
copy "%~n0%~x0" "%USERPROFILE%\Start Menu\Programs\Startup"
*Use this code!
i know the answer,
type this in your batch file:
copy "copy file path" "paste file path"
if the file path has a username than type %ALLUSERSPROFILE% instead of the username (it will fill up automatically the username for every windows pc)
example: copy "C:\Users%username%\Desktop\New folder\hi.txt" "C:\Users%username%\Desktop"
that did this:
Example to what it did when opened
final answer:
copy "Path of file" "C:\Users%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
that would work
This should work:
copy "anything.bat" "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup"
this copies .txt, .exe, .bat, more into startup just change the .bat to anything else
I made this in Windows 10

Resources