Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 545
  • Last Modified:

combine actions in a batch script

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
samiam41
Asked:
samiam41
  • 3
  • 2
1 Solution
 
Bill PrewCommented:
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
 
samiam41Author Commented:
BP, how do I combine the "settings" and "connections" variables?  You have "settings" listed twice.
0
 
Bill PrewCommented:
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
 
samiam41Author Commented:
Spot on and another great solution.  Thanks BP!
0
 
Bill PrewCommented:
Welcome, thanks for the feedback.

~bp
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now