1.

Solve : Port reset script?

Answer»

Hey guys!

So I'm working on this script even as you're reading this, but there's a simple thing I want it to do, no doubt it can be put in as little as 10 lines of code. I need a script that will reset my LPT1 connection.

Sounds too simple right? Right. I have several different computers many of which use different networked printers over the LPT1 connection. Every now and then (and honestly I haven't figured out, or really tried to figure out, why this happens) the LPT1 port will read "Disconnected" when check using NET USE. Since there are several different computer/printer combinations, I want the script the grab the existing LPT1 name before deleting the connection so that it can be written on the reconnect step.

Most of this is quite easy, but there are a few things I need help with... Something like this is what I'm thinking:

Code: [Select]@echo off
echo.
NET USE LPT2 > NUL && set port=LPT2 && if '%port%'=='' set port=LPT1
echo Resetting %port%
echo.
echo Please Wait...
REM I WOULD NEED THE CURRENT PRINTER SHARE INFO GRABBED HERE
set share=\\Hostname\ShareName
net use %port% /delete > nul
echo Connection %port% deleted...
echo Reconnecting %port% now...
net use %port% %share% && echo Connection Repaired... && pause

anyway... IDK.. I think that's prettier than it needs to be. lolIs it possible to get the current share info for the printer if it is disconnected?

Quote from: foxidrive on October 24, 2013, 04:37:37 AM

Is it possible to get the current share info for the printer if it is disconnected?

Yes. But I think it only stays since it's manually set. I can see that if it were a PnP printer that the data wouldn't be available when it's disconnected. For me, the printer doesn't even need to be connected to the Host computer!! Just shared...

If the LPT1 printer stops working, I usually manually use the NET USE command to see if it's actualled correctly, and it will read out something like:

Code: [Select]X: \\network_file_location\file_folder ...
Y: \\network_file_location\file_folder ...
DISCONNECTED \\Host_computer_name\Printer_share_name

From there I manually run
Code: [Select] NET USE LPT1 /DELETE
NET USE LPT1 \\Host_computer_name\Printer_share_name

And am ALMOST always greeted happily with COMMAND COMPLETED SUCCESSFULLY! On RARE ocassions get a credentials prompt, and even more rare, I'll get a failure or ERROR (usually because of mistyping something).

Anyway, yeah! Pointers on extracting that info ( \\Host_computer_name\Printer_share_name ) would be great!!Have you tried something like this?

Code: [Select]@echo off
for /f "tokens=1*" %%a in ('net use ^|find "DISCONNECTED" ') do (
NET USE LPT1 /DELETE
NET USE LPT1 %%b
)
pause
Quote from: foxidrive on October 25, 2013, 04:58:22 AM
Have you tried something like this?

Code: [Select]@echo off
for /f "tokens=1*" %%a in ('net use ^|find "DISCONNECTED" ') do (
NET USE LPT1 /DELETE
NET USE LPT1 %%b
)
pause

haven't really tried anything yet.

I'll give that a shot and let you know foxi. thanks!I had a similar issue many years ago with LPT1 disappearing etc and I was able to solve it by performing a reset of the spooler SERVICE on the system that was connected to the printer which was on LPT1

NET STOP SPOOLER

NET START SPOOLER

I added to a batch file and gave the users in the office the ability to do this over the network to the system that was acting as a print server/workstation. I used the SC.exe remote service manager tool to call SC \\system_name NET STOP SPOOLER and then SC \\system_name NET START SPOOLER

and this seemed to be the fix. Never found out why the HP 4000 laserjet on this HP Compaq Evo PENTIUM 4 system was having this issue, but this was the bugfix to our problem where the printer was not found because it appeared that LPT1 lost connection with the printer under Windows XP Pro systems and a few older Windows 2000 Pro SP4 systems running on Pentium II and Pentium III CPUs, that also were tied to the printer share on the XP print server / workstation.

This might not be related to your issue, however it sounds like the issue I dealt with 6 years ago. And this was the quick fix to allowing the food store to be able to print their signs and other stuff while manning the service desk waiting for customer questions etc.Quote from: DaveLembke on October 25, 2013, 03:48:44 PM
I had a similar issue many years ago with LPT1 disappearing etc and I was able to solve it by performing a reset of the spooler service on the system that was connected to the printer which was on LPT1

NET STOP SPOOLER

NET START SPOOLER

I added to a batch file and gave the users in the office the ability to do this over the network to the system that was acting as a print server/workstation. I used the SC.exe remote service manager tool to call SC \\system_name NET STOP SPOOLER and then SC \\system_name NET START SPOOLER

and this seemed to be the fix. Never found out why the HP 4000 laserjet on this HP Compaq Evo Pentium 4 system was having this issue, but this was the bugfix to our problem where the printer was not found because it appeared that LPT1 lost connection with the printer under Windows XP Pro systems and a few older Windows 2000 Pro SP4 systems running on Pentium II and Pentium III CPUs, that also were tied to the printer share on the XP print server / workstation.

This might not be related to your issue, however it sounds like the issue I dealt with 6 years ago. And this was the quick fix to allowing the food store to be able to print their signs and other stuff while manning the service desk waiting for customer questions etc.

Thanks Dave I'll try that out too. It's happened to pretty much every computer that has the LPT1 setup on it, and I'm not quite sure why. It's usually not a common problem, but it does seem that some systems have more trouble than others. All the systems are XP Pro SP3 or earlier.


Foxi, I tried this:
Code: [Select]for /f "tokens=1*" %a in ('net use ^| find "LPT1" ') do (
echo %b > test.txt(modified for manual entry rather than batch)

made a text file with:
Quote
LPT1 \\Host_Machine\Share_name

I can't remember off the top of my head how to use the trimming and substitute switches... but I'll prolly get that pretty quick.So... here's the new script:

Code: [Select]

@echo on
Title LPT Printer Reset Utility - KUtils
REM Set base variables
set ComD=net use
set port=LPT1
echo Fetching Printer Share Data...
REM check for LPT2
%ComD% LPT2 && set port=LPT2

for /f "tokens=1*" %%a in ('net use ^| find "%port%" ') do ( set share=%%b)
set share=%share:~10%
set ComR=%ComD% %port% %share%
echo Deleting Connection Data...
%ComD% %port% /DELETE
echo Resetting Connection...
%ComR% && goto SUCCESS
goto FAILURE

:SUCCESS
echo Connection Reset Successfully!
echo.
echo Press any key to exit...
pause > nul
exit

:FAILURE
title ERROR!
echo.
echo There was an error in processing
echo your network printer settings.
echo.
echo If this is the first error of
echo this type, try running the
echo utility again.
echo.
echo Press any key to exit...
pause > nul
exit

pastebin.com/xhbPysNq

Here's the new error I hit. It completed fine with the printer that I was testing on, then I decided to network another printer and test it on a different printer to be sure, you know? Then this occurred... This
Code: [Select] for /f "tokens=1*" %%a in ('net use ^| find "%port%" ') do ( set share=%%b)gave me a value of:
Code: [Select]LPT1 \\Host_Machine\Share_name Microsoft Windows Network

Now of course this messes up my script, because it's not TRYING to run the command
Code: [Select]net use LPT1 \\Host_Machine\Share_name Microsoft Windows Networkwhich is syntactically wrong!

Anyway, I got trimming the front 10 chars down... now how do I trim the last X characters? Or how can I tell it to only grab up to the space? :/

I'm gonna keep fiddling around... but if you feel the need to chime in, don't hesitate.Quote from: kyle_engineer on October 26, 2013, 12:40:12 PM

Code: [Select] for /f "tokens=1*" %%a in ('net use ^| find "%port%" ') do ( set share=%%b)gave me a value of:
Code: [Select]LPT1 \\Host_Machine\Share_name Microsoft Windows Network

Change this Code: [Select]tokens=1,2 and it will split it at the spaces. If the network address will never have a space in it then it will work fine.

Another method is to use this, which will remove that exact text.

Code: [Select]set "share=%share: Microsoft Windows Network=%"
@Foxi, It didn't like either of those... :/ maybe because I'm testing on a Win 7 Ult IDE, and not on XP... but I'm not sure. lol!!

I'll keep fiddling around... but if you have any more ideas, let me know!

Thanks If you are changing the variable in a loop then you need to use delayed expansion and the !variable! syntax.

To get help, you'd need to describe the error messages and tell us what it does, and how it fails.Quote from: foxidrive on October 28, 2013, 03:44:44 PM
If you are changing the variable in a loop then you need to use delayed expansion and the !variable! syntax.

To get help, you'd need to describe the error messages and tell us what it does, and how it fails.

duh. sorry.

Code: [Select]tokens=1,2seems to just break the command. when entered manually it returns no result or error at all. (Obviously I also changed the %% to % for manual use, so that's not the problem either...) Running this in a batch results in
Code: [Select]share=Microsoft Windows Networkmeaning it's clipping off the part that I want and keeping the part I don't want.

using this:
Code: [Select]set share=\\host_machine\share_name Microsoft Windows Network
set share=%share: Microsoft Windows Network%results in %share% now having the value "%share: Microsoft Windows Network%" which is completely off from what I want. I also did this with and without quotes and it didn't make any difference.

Anyway, I did a little looking, but isn't there a way to count from the back forward? I could be mixing that up with string manipulation in VB (or VBS or VBA) but I could of sworn I did that before... :/

So yeah, if you want to toy around with it, the exact string I'm stuck on (with spaces and everything) is:
Quote
\\xxxxxx-xxxxx\xxxxxxx Microsoft Windows Network
some of our network naming may differ... so I'd like it to just use the first space as a delim... Wait! I'll see if I can delim at the space and assign the rest to scrap or something!Here's some code - it uses the suggestion I gave you and proves that it works.

Code: [Select]@echo off
set "string=LPT1 \\Host_Machine\Share_name Microsoft Windows Network"


@echo off
Title LPT Printer Reset Utility - KUtils
REM Set base variables
set ComD=net use
set port=LPT1
echo Fetching Printer Share Data...
REM check for LPT2
rem %ComD% LPT2 && set port=LPT2

rem for /f "tokens=1,2" %%a in ('net use ^| find /i "%port%" ') do (set share=%%b)

for /f "tokens=1,2" %%a in ("%string%") do set "share=%%b"
echo set ComR=%ComD% %port% %share%
echo Deleting Connection Data...
echo %ComD% %port% /DELETE


pauseAlright Foxi, I was able to tweak your code a little to work. Not sure what it was, but I decided that the "tokens=1,2" worked when it was being passed a string rather than a command to process. Not sure if that's actually the case, but hey - it worked. LOL!

Anyway, here's what I got!!

Code: [Select]

@echo off

::Lpt Printer Reconnect Utility
Title Lpt Printer Reset Utility - KUtils
::set base variables
set ComD=net use

:: Capture used port
Echo Checking LPT Port Assignment...
%ComD% lpt2 > nul
if %errorlevel% neq 0 (
set port=LPT1
) else (
set port=LPT2)

echo --- Port is set as %port%
echo.

:: Capture current share name
echo Fetching Share Data...
for /f "tokens=1*" %%a in ('net use ^|find "%port%" ') do (
set oShare=%%b)
for /f "tokens=1,2" %%A in ("%oShare%") do set "Share=%%B"
echo --- Network share name captured is - %Share%
echo.

:: Delete Current Port
echo Removing Existing Port Settings...
%ComD% %port% /delete > nul
if %errorlevel% neq 0 (
echo --- Error Deleting Port! && pause > nul && exit
) else (
echo --- Port Deleted Successfully!)
echo.

:: Recreate Port Connection
echo Rebuilding Port Connection...
%ComD% %port% %share%
if %errorlevel% neq 0 (
echo --- Error Rebuilding Port && pause > nul && exit
) else (
echo --- Port Rebuilt Successfully!)
echo.

:: Report Final Settings
for /f "tokens=1*" %%a in ('net use ^|find "%port%" ') do (
set cShare=%%b)
echo.
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo +
echo + CURRENT PORT SETTINGS:
echo + --- %cShare%
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo.
echo Press any key to exit...
pause > nul
exit

http://pastebin.com/xhbPysNq#

It works perfectly for me. (If you test it and it doesn't seem to work, I did manually retype it, so something could have gone wrong.) If you'd like to make any recommendations to clean anything up a little, just let me know.

And THANKS for all the help!!!
- kyle_engineer


Discussion

No Comment Found