Solved

combine actions in a batch script

Posted on 2014-01-24
5
533 Views
Last Modified: 2014-01-27
Greetings experts!!  I have two actions that I would like a batch file to perform.  The first action is delete the value "oldserver" out of the HKCU>Printers>Settings and then delete out "oldserver" from HKCU>Printers>Connections.

I am using this script and wanted to build off of it accordingly (a product of an EE expert):
@echo off
setlocal

set Server=oldserver

for /f "tokens=*" %%A in ('reg query "HKEY_CURRENT_USER\Printers\Connections" ^| find /i ",%Server%,"') do (
  reg delete "%%~A" /f
) 

Open in new window


I tried to add this:  

for /f "tokens=*" %%A in ('reg query "HKEY_CURRENT_USER\Printers\Settings" ^| find /i ",%server%,"') do (
  reg delete "%%~A" /f

but couldn't get it to work.  Please help combine these two actions into a single batch script.  Thanks experts!
0
Comment
Question by:samiam41
  • 3
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39807668
Try this:

for /f "tokens=1" %%A in ('reg query "HKEY_CURRENT_USER\Printers\Settings" ^| find /i "\\%Server%\"') do (
  echo reg delete "HKEY_CURRENT_USER\Printers\Settings" /v "%%~A" /f
)

Open in new window

~bp
0
 
LVL 9

Author Comment

by:samiam41
ID: 39807679
BP, how do I combine the "settings" and "connections" variables?  You have "settings" listed twice.
0
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39807719
Full script would look like this.  In one case you are deleting just one value (from settings) in the other case you are deleting the whole key (connections).

@echo off
setlocal

set Server=oldserver

for /f "tokens=1" %%A in ('reg query "HKEY_CURRENT_USER\Printers\Settings" ^| find /i "\\%Server%\"') do (
  reg delete "HKEY_CURRENT_USER\Printers\Settings" /v "%%~A" /f
)

for /f "tokens=*" %%A in ('reg query "HKEY_CURRENT_USER\Printers\Connections" ^| find /i ",%Server%,"') do (
  reg delete "%%~A" /f
)

Open in new window

~bp
0
 
LVL 9

Author Closing Comment

by:samiam41
ID: 39812280
Spot on and another great solution.  Thanks BP!
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39812382
Welcome, thanks for the feedback.

~bp
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to have Windows 7 Dell automatically start in the morning after a shut down. 8 62
Logon script fails 23 45
Capture image using MDT 21 36
logon time 6 37
OfficeMate Freezes on login or does not load after login credentials are input.
On some Windows 7 (SP1) computers, Windows Update becomes super slow even the computer is reasonably fast.  There's one solution that seemed to have worked well for me (after trying a few other suggested solutions).
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question