Why do I have to import .reg file from RegEdit in Win7 and not from .bat file?

3

1

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

Posted 2011-01-04T22:04:00.637

Reputation: 891

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 – 2017-07-26T10:55:54.993

Btw, it's Windows 7 Ultimate (64-bit) in a VMWare image. – Brian T Hannan – 2011-01-04T22:12:06.130

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 – 2011-01-04T22:33:14.313

It's hard to believe that no one else is having this same problem. – Brian T Hannan – 2011-01-05T18:51:01.087

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 – 2011-01-10T16:48:34.050

@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. – user1686 – 2011-05-27T11:06:50.883

@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. – user1686 – 2011-05-27T11:27:27.910

grawity was correct, when you open a command-prompt normally, it defaults to your user directory, but when you run it as administrator, it defaults to the system directory (\Windows).

– Synetech – 2013-10-06T05:00:56.783

Answers

2

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?

JWN

Posted 2011-01-04T22:04:00.637

Reputation: 21

1

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

Greg Hewgill

Posted 2011-01-04T22:04:00.637

Reputation: 5 099

Nope, it's local and on the Desktop ... both .reg and .bat – Brian T Hannan – 2011-01-04T22:09:21.247

Network volume was the issue for me. It would be nice if Windows would tell you that. – Scott Lundberg – 2013-06-28T20:22:02.973

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

Posted 2011-01-04T22:04:00.637

Reputation: 470

I turned off UAC and still didn't work for me. – Brian T Hannan – 2011-01-04T22:28:21.613

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

Posted 2011-01-04T22:04:00.637

Reputation: 11

Part of the reason yours wasn't working is because you had spaces in the filename. – MBraedley – 2013-04-10T12:43:42.570

0

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

specked

Posted 2011-01-04T22:04:00.637

Reputation:

Nope, didn't work. – Brian T Hannan – 2011-01-04T22:27:02.703

0

Try this from an elevated CMD prompt:

reg import file.reg

paradroid

Posted 2011-01-04T22:04:00.637

Reputation: 20 970

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

user315811

Posted 2011-01-04T22:04:00.637

Reputation: 1

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

Henry Catpants

Posted 2011-01-04T22:04:00.637

Reputation: 1