Solved

combine actions in a batch script

Posted on 2014-01-24
5
534 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 54

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 54

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 54

Expert Comment

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

~bp
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This Micro Tutorial will teach you the basics of configuring your computer to improve its speed. It will also teach you how to disable programs that are running in the background simultaneously. This will be demonstrated using Windows 7 operating…
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

680 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