3

I have a .bat file that makes a call to a .reg file (something like: regedit mytest.reg). I run the .bat file as administrator but I get an error: "Cannot import mytest.reg: Error opening the file. There may be a disk or file system error."

However, if I open RegEdit (as administrator) first then File >> Import >> mytest.reg ... it successfully runs.

Any ideas?

Brian T Hannan
  • 1,071
  • 4
  • 15
  • 26
  • Btw, it's Windows 7 Ultimate (64-bit) in a VMWare image. – Brian T Hannan Jan 04 '11 at 22:12
  • It also works if I open cmd.exe as administrator then kick off the .bat file. For some reason it seems like the Run as administrator for a .bat file doesn't work. – Brian T Hannan Jan 04 '11 at 22:33
  • It's hard to believe that no one else is having this same problem. – Brian T Hannan Jan 05 '11 at 18:51
  • It turns out that if you have a .bat file and relative paths in it then it doesn't know how to use them properly. But if you only put in absolute paths in the .bat file then it works ... bug in Windows 7 batch files? – Brian T Hannan Jan 10 '11 at 16:48
  • @Brian: Paths are relative to the "current directory", which may simply be different when you use "Run as administrator". Add a `cd` to see what the current directory is. – u1686_grawity May 27 '11 at 11:06
  • @Brian: (Use `cd /d "%~dp0"` to force to the batch file's location.) Besides, while `cmd` *does* have many quirks, it doesn't touch filenames you give to other commands - it's entirely up to `reg` how to treat the file named in, for example, `reg import foo\bar\baz.reg`. – u1686_grawity May 27 '11 at 11:27
  • grawity was correct, when you open a command-prompt normally, it [defaults to your user directory](http://superuser.com/questions/399978/start-elevated-command-prompt-in-userprofile-directory-by-default), but when you run it as administrator, it defaults to the system directory (`\Windows`). – Synetech Oct 06 '13 at 05:00
  • **I got the exact same error but in both RegEdit and the batch file**. It turns out that either regedit or the UAC account elevation can't handle local folders that are mounted to a drive letter with `subst D: "C:\FolderName"`. Running `D:\test.reg` fails while the full path `C:\FolderName\test.reg` works fine. – Lilienthal Jul 26 '17 at 10:55

8 Answers8

3

Had the same problem. Once you accept running under elevated permissions, the "root" of the elevated session does not have the same relative location for the actual command.

If you specify an absolute address for the file it should work.

Now the $.42 question... what is the current directory for the elevated session in which the command is running?

sblair
  • 12,617
  • 6
  • 48
  • 77
JWN
  • 31
  • 2
1

Is the .reg file on a network volume? Local administrators cannot normally read files from across a network.

Greg Hewgill
  • 5,499
  • 2
  • 29
  • 30
1

It works for me in a simple test, but I have to answer UAC-related prompts when the batchfile runs.

Have you customized your UAC settings? Maybe when you run the batch file things are configured to not elevate (or even ask to elevate) so it fails.

But regedit elevates when it loads (regardless of UAC settings, I think).

Michael Burr
  • 532
  • 3
  • 11
1

I had the exact same problem and error message.. I could not get my REG file to from my batch file on my Win7 Pro 64 bit machine. Finally got it to work by puttin quatation marks around the REG file.. (EXAMPLE)

c:\windows\regedit.exe /s "C:\Windows\Enable DTS Login Script.reg"

Hope this is helpful

D8ripper
  • 11
  • 1
0

try using the /C switch "regedit /C myfile.reg"

0

Try this from an elevated CMD prompt:

reg import file.reg
paradroid
  • 22,761
  • 10
  • 76
  • 114
0

It doesn't seem to work with the relative path as others suggested.

This is what worked for me:

Simply add %~dp0 in front of the file name and it will use fill in the direct path to the batch file. So as long as the .reg file is in the same folder as the batch file you are good to go.

For example regedit.exe /s %~dp0registryfile.reg

ᔕᖺᘎᕊ
  • 6,173
  • 4
  • 33
  • 45
user315811
  • 11
  • 2
0

It doesn't seem to work with the relative path as others suggested.

This is what worked for me:

Simply add %~dp0 in front of the file name and it will fill in the direct path to the batch file. So as long as the .reg file is in the same folder as the batch file you are good to go.

For example:

regedit.exe /s %~dp0registryfile.reg
Jan Doggen
  • 4,108
  • 10
  • 36
  • 51