0

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
Jojo Coana
  • 31
  • 6
  • 1
    Maybe your code is wrong - but we can't tell as it is secret. –  Mar 13 '16 at 12:52
  • Edit your question and post your code ! – Hackoo Mar 13 '16 at 12:53
  • 1
    Please see the how to ask section and post the code or expect no help here. No offence – Mr.Helpy Mar 13 '16 at 13:05
  • 2
    When you run your Batch file as administrator its current directory is a different one, so you need to use a full path for your data file: `xcopy /s/y C:\replacer\scripts.ini ...` – Aacini Mar 13 '16 at 14:48

1 Answers1

2

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
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Oh, there seems to be another problem, i cannot overwrite the original file, because it's a system file. Even as administrator. – Jojo Coana Mar 13 '16 at 15:42
  • 2
    `/s` is not needed here for copying a single file. But using `/R` additionally to `/Y` makes it possible to overwrite a destination file with system attribute set. See [this answer](http://stackoverflow.com/a/35829012/3074564) for details about how to copy a single file with __XCOPY__. – Mofi Mar 13 '16 at 16:14