# Solved: Batch file problems



## Resarfski (Mar 2, 2011)

Hi All

I have a problem enhancing an existing .bat file that I use to remotely edit networked computers. The existing file runs fine - see copy of contents below ....

*BATFILE01*
_set a="InsertComputerName"_
_sc __\\%a%__ START "RemoteRegistry"_
_reg delete __\\%a%\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing__ /f_
_reg add __\\%a%\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing__ /f
reg add __\\%a%\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\HardwareID__ /f
reg add __\\%a%\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store__ /f
reg add __\\%a%\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store\LICENSE000__ /f_
_c:\FraserData\BATS\Misc\subinacl.exe /keyreg __\\%a%\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing__ /grant=Users=F
c:\FraserData\BATS\Misc\subinacl.exe /keyreg __\\%a%\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\HardwareID__ /grant=Users=F
c:\FraserData\BATS\Misc\subinacl.exe /keyreg __\\%a%\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store__ /grant=Users=F
c:\FraserData\BATS\Misc\subinacl.exe /keyreg __\\%a%\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store\LICENSE000__ /grant=Users=F
pause_

This file is used on an "ad-hoc" basis and is manually edited with a computer name, each time it is run.
I am trying to enhance the process by using the FOR /f command, reading a .txt file with a list of computer names.

The contents of the amended file are ...

*BATCH02*
_FOR /F %%a IN (C:\FraserDATA\BATs\Misc\WorkBenchLicenses.txt) do reg delete __\\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing__ /f
reg add __\\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing__ /f
reg add __\\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\HardwareID__ /f
reg add __\\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store__ /f
reg add __\\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store\LICENSE000__ /f
c:\FraserData\BATS\Misc\subinacl.exe /keyreg __\\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing__ /grant=Users=F
c:\FraserData\BATS\Misc\subinacl.exe /keyreg __\\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\HardwareID__ /grant=Users=F
c:\FraserData\BATS\Misc\subinacl.exe /keyreg __\\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store__ /grant=Users=F
c:\FraserData\BATS\Misc\subinacl.exe /keyreg __\\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store\LICENSE000__ /grant=Users=F
pause_

However, when the file is run, an error is returned when the second line is processed (reg add) indicating "the network path was not found".

Can anybody help please.


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!

The %%a variable is only define while on the same line as the For loop. So all the following lines have to be on the same line as the For command. Use the ( as a line continuation character, everything aget is consideered to be on the same line until the matching closing parenthesis:

```
FOR /F %%a IN (C:\FraserDATA\BATs\Misc\WorkBenchLicenses.txt) do [COLOR=Red][B]([/B][/COLOR]
  reg delete \\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing /f
  reg add \\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing /f
  reg add \\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\HardwareID /f
  reg add \\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store /f
  reg add \\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store\LICENSE000 /f
  c:\FraserData\BATS\Misc\subinacl.exe /keyreg \\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing /grant=Users=F
  c:\FraserData\BATS\Misc\subinacl.exe /keyreg \\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\HardwareID /grant=Users=F
  c:\FraserData\BATS\Misc\subinacl.exe /keyreg \\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store /grant=Users=F
  c:\FraserData\BATS\Misc\subinacl.exe /keyreg \\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store\LICENSE000 /grant=Users=F
[COLOR=Red][B])[/B][/COLOR]
pause
```
As far as the command processor is concerned, the above is only two lines.


----------



## Resarfski (Mar 2, 2011)

Thank you for posting this reply, I've learned a little more.

However, I still seem to have a problem. When I run the .bat (edited as per your example), the text is echo'd to the screen in one block, but does not appear to process.

I was expecting each of the commands to process against each computer contained within the .txt file.

Your further help would be much appreciated.


----------



## TheOutcaste (Aug 8, 2007)

It should run all 9 commands for each computer name in the C:\FraserDATA\BATs\Misc\WorkBenchLicenses.txt file.
Each PC name should be on it's own line, and they can't have any spaces and should not include the leading \\
For a quick test to verify it's reading the file names, run this, it should list each PCname:

```
FOR /F %%a IN (C:\FraserDATA\BATs\Misc\WorkBenchLicenses.txt) do (
Echo %%a
)
pause
```
If you haven't turned off Echo, you'll see the complete *For* loop line displayed, then the block of 9 commands will be displayed for each PC name found, then each command will be executed, and you should see the result for each command. The *reg* commands should display *The operation completed successfully*.after each command, *subinacl* should display the changes made.

Open a Command Prompt, run the file, then copy and paste the entire screen here. Be sure to paste it in between code tags:
[codeblock_7590a61ebb3015b481c58088e33961ba]

To copy from the Command Prompt:
Right click the title bar
Point to *Edit*
Click *Select All*. This highlights everything
Press *Enter*. This copies to the clipboard.
Type the code tags in the reply window, click in between them, press *CTRL+V*


----------



## Resarfski (Mar 2, 2011)

For simplicity, I have only placed 2 computer names in the WorkBenchLicenses.txt file. Firstly, I ran the Batch01 against each computer, to confirm they were alive and receptive to the batch file commands. Both ran OK and I've posted the result from one here...
(NB - this bat was run from w064)


```
C:\FraserDATA\BATs\Misc>set a=w064
C:\FraserDATA\BATs\Misc>sc [URL="file://\\w064"]\\w064[/URL] START "RemoteRegistry"
[SC] StartService FAILED 1056:
An instance of the service is already running.

C:\FraserDATA\BATs\Misc>reg delete [URL="file://\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\"]\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\[/URL]
MSLicensing /f
The operation completed successfully
C:\FraserDATA\BATs\Misc>reg add [URL="file://\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSL"]\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSL[/URL]
icensing /f
The operation completed successfully
C:\FraserDATA\BATs\Misc>reg add [URL="file://\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSL"]\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSL[/URL]
icensing\HardwareID /f
The operation completed successfully
C:\FraserDATA\BATs\Misc>reg add [URL="file://\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSL"]\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSL[/URL]
icensing\Store /f
The operation completed successfully
C:\FraserDATA\BATs\Misc>reg add [URL="file://\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSL"]\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSL[/URL]
icensing\Store\LICENSE000 /f
The operation completed successfully
C:\FraserDATA\BATs\Misc>c:\FraserData\BATS\Misc\subinacl.exe /keyreg [URL="file://\\w064\HKEY"]\\w064\HKEY[/URL]
_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing /grant=Users=F
SOFTWARE\Microsoft\MSLicensing : delete Perm. ACE 1 builtin\users
SOFTWARE\Microsoft\MSLicensing : delete Perm. ACE 0 builtin\users
SOFTWARE\Microsoft\MSLicensing : new ace for builtin\users
[URL="file://\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing"]\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing[/URL] : 3 change(s)

Elapsed Time: 00 00:00:00
Done: 1, Modified 1, Failed 0, Syntax errors 0
Last Done : [URL="file://\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing"]\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing[/URL]
C:\FraserDATA\BATs\Misc>c:\FraserData\BATS\Misc\subinacl.exe /keyreg [URL="file://\\w064\HKEY"]\\w064\HKEY[/URL]
_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\HardwareID /grant=Users=F
SOFTWARE\Microsoft\MSLicensing\HardwareID : delete Perm. ACE 1 builtin\users
SOFTWARE\Microsoft\MSLicensing\HardwareID : delete Perm. ACE 0 builtin\users
SOFTWARE\Microsoft\MSLicensing\HardwareID : new ace for builtin\users
[URL="file://\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\HardwareID"]\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\HardwareID[/URL] : 3 change(s
)

Elapsed Time: 00 00:00:00
Done: 1, Modified 1, Failed 0, Syntax errors 0
Last Done : [URL="file://\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\HardwareID"]\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\HardwareID[/URL]

C:\FraserDATA\BATs\Misc>c:\FraserData\BATS\Misc\subinacl.exe /keyreg [URL="file://\\w064\HKEY"]\\w064\HKEY[/URL]
_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store /grant=Users=F
SOFTWARE\Microsoft\MSLicensing\Store : delete Perm. ACE 1 builtin\users
SOFTWARE\Microsoft\MSLicensing\Store : delete Perm. ACE 0 builtin\users
SOFTWARE\Microsoft\MSLicensing\Store : new ace for builtin\users
[URL="file://\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store"]\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store[/URL] : 3 change(s)

Elapsed Time: 00 00:00:00
Done: 1, Modified 1, Failed 0, Syntax errors 0
Last Done : [URL="file://\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store"]\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store[/URL]
C:\FraserDATA\BATs\Misc>c:\FraserData\BATS\Misc\subinacl.exe /keyreg [URL="file://\\w064\HKEY"]\\w064\HKEY[/URL]
_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store\LICENSE000 /grant=Users=F
SOFTWARE\Microsoft\MSLicensing\Store\LICENSE000 : delete Perm. ACE 1 builtin\use
rs
SOFTWARE\Microsoft\MSLicensing\Store\LICENSE000 : delete Perm. ACE 0 builtin\use
rs
SOFTWARE\Microsoft\MSLicensing\Store\LICENSE000 : new ace for builtin\users
[URL="file://\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store\LICENSE000"]\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store\LICENSE000[/URL] : 3 ch
ange(s)

Elapsed Time: 00 00:00:00
Done: 1, Modified 1, Failed 0, Syntax errors 0
Last Done : [URL="file://\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store\LICE"]\\w064\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store\LICE[/URL]
NSE000
C:\FraserDATA\BATs\Misc>pause
Press any key to continue . . . 
<\code>

So far so good.

I then ran the code to echo the list of computers within the WorkBenchLicenses.txt and got the following ...

[CODE] 
C:\FraserDATA\BATs\Misc>FOR /F %a IN (c:\FraserDATA\BATs\Misc\WorkBenchLicenses.
txt) do (Echo %a )
C:\FraserDATA\BATs\Misc>pause
Press any key to continue . . . 
<\code>

No errors were generated, but there is no output. I then ran a simple .bat to TYPE the contents of the file to the screen ...

[CODE] 
C:\FraserDATA\BATs\Misc>type c:\FraserDATA\BATs\Misc\WorkBenchLicenses.txt
w064
w535
C:\FraserDATA\BATs\Misc>pause
Press any key to continue . . . 
<\code>

Similarly, I ran the BATCH02 file (edited in accordance with your previous post), which returns the following ...

[CODE] 
C:\FraserDATA\BATs\Misc>FOR /F %a IN (C:\FraserDATA\BATs\Misc\WorkBenchLicenses.
txt) do (
reg delete [URL="file://\\%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing"]\\%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing[/URL] /f
reg add [URL="file://\\%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing"]\\%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing[/URL] /f
reg add [URL="file://\\%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\HardwareID"]\\%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\HardwareID[/URL] /f
reg add [URL="file://\\%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store"]\\%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store[/URL] /f
reg add [URL="file://\\%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store\LICENSE000"]\\%a\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing\Store\LICENSE000[/URL]
/f
c:\FraserData\BATS\Misc\subinacl.exe /keyreg [URL="file://\\%a\HKEY_LOCAL_MACHINE\SOFTWARE\M"]\\%a\HKEY_LOCAL_MACHINE\SOFTWARE\M[/URL]
icrosoft\MSLicensing /grant=Users=F
c:\FraserData\BATS\Misc\subinacl.exe /keyreg [URL="file://\\%a\HKEY_LOCAL_MACHINE\SOFTWARE\M"]\\%a\HKEY_LOCAL_MACHINE\SOFTWARE\M[/URL]
icrosoft\MSLicensing\HardwareID /grant=Users=F
c:\FraserData\BATS\Misc\subinacl.exe /keyreg [URL="file://\\%a\HKEY_LOCAL_MACHINE\SOFTWARE\M"]\\%a\HKEY_LOCAL_MACHINE\SOFTWARE\M[/URL]
icrosoft\MSLicensing\Store /grant=Users=F
c:\FraserData\BATS\Misc\subinacl.exe /keyreg [URL="file://\\%a\HKEY_LOCAL_MACHINE\SOFTWARE\M"]\\%a\HKEY_LOCAL_MACHINE\SOFTWARE\M[/URL]
icrosoft\MSLicensing\Store\LICENSE000 /grant=Users=F
)
C:\FraserDATA\BATs\Misc>pause
Press any key to continue . . . 
<\code>

Again, no errors returned and the pause command appers to have been processed.

I'm sure the solution is bound to be simple, but I can't see the wood for the trees.

Appreciate your help and time spent on this.
```


----------



## Resarfski (Mar 2, 2011)

bump

Also had a problem with the "code" tags when I posted the last reply. They merely appeared as text.


----------



## Squashman (Apr 4, 2003)

You need to use Square Brackets for the Code tags


----------



## Resarfski (Mar 2, 2011)

Thank you Squashman.

I think my problem is a little more fundamental.

I have used the simple code below, but I'm still not getting any result. The .bat file appears to run (no error), but doesn't actually do anything. I have re-created the folder/files on alternative computers - but with the same result.


```
FOR /F %%g IN (C:\Resources\BATs\Test\List.txt) DO xcopy Test.txt [URL="file://\\%%g\c"]\\%%g\c[/URL]:
```
I believe this .bat should copy the file Test.txt to the route directory of the computers listed in the List.txt file (only two computers listed - 2 lines containing the computer names, no \\'s etc.)


----------



## Squashman (Apr 4, 2003)

Going to assume you mean ROOT directory. Not ROUTE directory.

If you are trying to copy to the root of the C: drive then I believe you need to use the administrative share to copy to it.


```
FOR /F %%g IN (C:\Resources\BATs\Test\List.txt) DO xcopy Test.txt \\%%g\C$
```


----------



## Resarfski (Mar 2, 2011)

You are correct Squashman, root not route... it's been a long day!

I have edited the code as per your post, but the result is still the same ...

Inserting a "pause" after the line of code, the following is displayed:-

_C:\Resources\BATs\Test>FOR /F %g IN (C:\Resources\BATs\Test\List.txt) DO xcopy T
est.txt __\\%g\c$_
_C:\Resources\BATs\Test>pause
Press any key to continue . . ._

There's no other error message.


----------



## Squashman (Apr 4, 2003)

Do you have any spaces in the computer names?
Can you ping the computer names and get a response?
Does the same administrative username and password exist on each computer?
Are the administrative shares enabled?


----------



## Resarfski (Mar 2, 2011)

Hi Squashman

Thank you for your help. I checked each of the points you noted and confirmed each is configured correctly.

However...

I subsequently re-created the .txt file containing the list of computer names, using NotePad (as I had done previously) - but it still didn't work. I noted that by default, NotePad saved the .txt file encoded as "Unicode", so I then resaved the .txt file encoded as "ANSI" .... and the .bat worked !!!

I didn't realize that the encoding would make a difference. I thought a plain text file was a plain text file (obviously not).

Anyway, thank you *Squashman* for putting me on the right track and thank you *TheOutcaste* for your previous posts and help.


----------



## Squashman (Apr 4, 2003)

Unicode files are double byte. So when you use that file as input to a batch file it will see this:
C O M P U T E R N A M E

Instead of this.
COMPUTERNAME


----------



## Resarfski (Mar 2, 2011)

Thanks Squashman, a little more learned today.


----------

