Write to registry

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jvuz
Posts: 38
Joined: 25 Feb 2011 03:37

Write to registry

#1 Post by jvuz » 28 Sep 2012 05:01

Hello,

I'm having a csv file (for instance)

Code: Select all

192.168.3.132,LC03-132,User A,11-04-2011
192.168.3.133,LC03-133,User B,02-05-2011
192.168.4.109,LC04-109,User C,26-10-2011
192.168.5.143,LC05-143,User D,02-05-2010

This file contains IP-address, PC name, User and start date of the warranty. The goal is to create a batch file (being launched from my pc) thakes the first line, tries to ping that machine, if it responds, write the date into the registry and then goes to pinging the following and so on.
If there isn't any response to the ping, it should continue to the following line. Is there a way to doe this by a batch file?

Jvuz

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Write to registry

#2 Post by abc0502 » 28 Sep 2012 06:42

where you want the date to be written in the registry?

jvuz
Posts: 38
Joined: 25 Feb 2011 03:37

Re: Write to registry

#3 Post by jvuz » 28 Sep 2012 06:47

I want it to be written to HKLM\SYSTEM\CurrentControlSet\Policies StartWarrantyDate (reg_sz). this key doesn't exist yet.

I know how to write to the registry, I don't know how to start, how to check the first line (1st and 4the field).

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Write to registry

#4 Post by abc0502 » 28 Sep 2012 06:52

To read a file and check for it's content, in your case it's a csv and that make it easy.
The comma is seprating IP,username,pc name and date
so

Code: Select all

For /f "tokens=1,2,3,4 delims=," %%A in ('Type "C:\File.csv"') Do (
         :: All Your Commands here
)

the %%A will hold the IP, and %%B will hold the PC names and %%C will have the user names and %%D will have the date

jvuz
Posts: 38
Joined: 25 Feb 2011 03:37

Re: Write to registry

#5 Post by jvuz » 28 Sep 2012 06:56

OK, thanks for the info. I knew it would be easier with a csv file, I just didn't know how ;)
Now I can play with it. I'll keep you posted.

Thanks again,
have a nice weekend,
jvuz

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Write to registry

#6 Post by abc0502 » 28 Sep 2012 06:57

Try this batch , but it's not tested

Code: Select all

@Echo Off & Cls & Color 0E
Setlocal EnableDelayedExpansion

:: Reading Input From CSV File
For /F "tokens=1,2,3,4 delims=," %%A in ('Type "C:\File.csv"') Do (
   set "IP=%%A" & set "PC=%%B" & set "User=%%C" & set "SDate=%%D"
   ping !IP! -n 4 >nul
   IF !errorlevel! == 0 (
      Reg Add "\\!PC!\HKLM\SYSTEM\CurrentControlSet\Policies" /v "StartWarrantyDate" /t "REG_SZ" /d "!SDate!" /f
   )
)
Last edited by abc0502 on 01 Oct 2012 10:44, edited 1 time in total.

jvuz
Posts: 38
Joined: 25 Feb 2011 03:37

Re: Write to registry

#7 Post by jvuz » 01 Oct 2012 02:12

Thanks,

but what if I have more lines. It's meant for our whole computer park (between 300 and 400 pc's). Is there a way to use something like EOF (end of file)?

Jvuz

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Write to registry

#8 Post by abc0502 » 01 Oct 2012 03:07

jvuz wrote:Thanks,

but what if I have more lines. It's meant for our whole computer park (between 300 and 400 pc's). Is there a way to use something like EOF (end of file)?

Jvuz

@Jvuz,
The code i posted will apply to all your 300 or 400 PC's, Just place all your PC's in the same format you did in your first post and then run that batch and it will process all PC's.

This For Loop is limited Only to your IPs in your CSV file whether there was only one line:

Code: Select all

192.168.3.132,LC03-132,User A,11-04-2011

or 1000 line like that line above it will do the same job to all

jvuz
Posts: 38
Joined: 25 Feb 2011 03:37

Re: Write to registry

#9 Post by jvuz » 05 Oct 2012 01:31

Thanks guys, it seems to be working, but is there a way to show which line has been done and which couldn't (because there weren't up and running)? Is it possible maybe to write it to another txt file?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Write to registry

#10 Post by foxidrive » 05 Oct 2012 02:35

Try this:

Code: Select all

@Echo Off & Cls & Color 0E
Setlocal EnableDelayedExpansion

:: Reading Input From CSV File
For /F "tokens=1,2,3,4 delims=," %%A in ('Type "C:\File.csv"') Do (
   set "IP=%%A" & set "PC=%%B" & set "User=%%C" & set "SDate=%%D"
   ping !IP! -n 4 >nul
   IF !errorlevel! == 0 (
      Reg Add "\\!PC!\HKLM\SYSTEM\CurrentControlSet\Policies" /v "StartWarrantyDate" /t "REG_SZ" /d "!SDate!" /f
   ) else (
       >>file.log echo IP %%A was not responding - "PC=%%B"  "User=%%C"  "SDate=%%D"
   )
)

jvuz
Posts: 38
Joined: 25 Feb 2011 03:37

Re: Write to registry

#11 Post by jvuz » 05 Oct 2012 02:47

With this I have all:

Code: Select all

@Echo Off & Cls & Color 0E
Setlocal EnableDelayedExpansion

:: Reading Input From CSV File
For /F "tokens=1,2,3,4 delims=," %%A in ('Type "C:\Users\USERNAME\Documents\Support\PC-Dates.csv"') Do (
set "IP=%%A" & set "PC=%%B" & set "User=%%C" & set "SDate=%%D"
ping !IP! -n 4 >nul
IF !errorlevel! == 0 (
Reg Add "\\!PC!\HKLM\SYSTEM\CurrentControlSet\Policies" /v "StartWarrantyDate" /t "REG_SZ" /d "!SDate!" /f
>> c:\StartWarrantyDate.log echo IP %%A OK: PC: %%B - User=%%C - SDate=%%D
   ) else (
       >>c:\StartWarrantyDate.log echo IP %%A was not responding - PC: %%B - User=%%C - SDate=%%D
   )
)


Thanks a lot!

Post Reply