Reconnect mapped network drives in Windows OS using Command Prompt script

This is an update of my blog entry https://www.metalvortex.com/blog/2016/07/24/1903.html where I published a Windows 10 Pro Command Prompt script to reconnect mapped network drives at log on. This was saved as a .CMD batch file and then executed twice using Task Scheduler; one in non-admin and the other in admin mode. This allows mapped drives to be visible to those applications running in non-admin and also to those applications running in admin modes.

I’ve made some minor tweaks to the code so it should be a little more robust against errors.

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET me=%~n0
SET parent=%~dp0

TITLE Mapping network drives
@ECHO Please wait whilst we reconnect your network drives.

SET neterror=0

SET counter=1
SET loopvalue=11
SET /A trueloop=%loopvalue%-1
:Start
ECHO _____________________________________
ECHO:
IF "%counter%" EQU "%loopvalue%" (
ECHO Connection to D:\ drive timed-out
SET neterror=1
GOTO End
)
ECHO Attempt %counter% of %trueloop% to D:\ drive
TIMEOUT /t 5 /NOBREAK >NUL
IF EXIST D:\NUL (
ECHO Attempt %counter% successful.
GOTO End
)
NET USE D: \\192.168.49.69\Data /PERSISTENT:YES
IF "%ERRORLEVEL%" NEQ "0" IF "%ERRORLEVEL%" NEQ "85" (
SET /A counter=%counter%+1
GOTO Start
)
:End

IF "%neterror%" EQU "0" GOTO Endofscript

:userconfirm
ECHO _____________________________________
ECHO:
SET /P userinput="Errors were found. Do you wish to try again [Y/n] "
IF /I "%userinput%" EQU "y" GOTO Start
IF /I "%userinput%" EQU "" GOTO Start
IF /I "%userinput%" EQU "n" GOTO Endofscript
GOTO userconfirm

:Endofscript
ECHO _____________________________________
ECHO:
@ECHO Please wait, script is closing.
TIMEOUT /t 5 /NOBREAK >NUL
ENDLOCAL
@ECHO OFF
@EXIT /B 0

Reconnect mapped network drives in Windows OS using Command Prompt commands

Command

[Update at https://www.metalvortex.com/blog/2017/07/15/2202.html]

Sometimes when booting into Windows, the OS does not reconnect mapped network drives even though the mapping was configured with the “Reconnect at sign-in” option.

Although there are free Third Party software available that can automatically reconnect your mapped network drives, I would rather be able to do this using built-in tools than add more unnecessary software to my system. I came across a response someone had written in a blog about using Third Party and showed how you could use a few Command Prompt commands using the “net use” command. I’ve lost the link to that blog article, but I did copy that code and built my own batch script around it. The key change I wanted to make was to put in a limited number of attempts just in case the NAS drive was down.

This is the code I use which I’ve put in a .CMD batch file which is executed by Task Scheduler at user log on.; this is better than executing it via a shortcut from within the Windows “StartUp” directory as I have flexibility in adding delays without needing to change the code. Talking of the code, I recognise that it is a bit ugly and some of you scripting experts can make it far more elegant. But it works for me and I didn’t want to spend too much time on this when I could be doing other more fun things! Note that this has only been tested in Windows 10 Pro 64-bit.

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SET me=%~n0
SET parent=%~dp0
TITLE Mapping network drives
@ECHO Please wait whilst we reconnect your network drives.

SET neterror=0

SET counter=1
SET loopvalue=11
SET /A trueloop=%loopvalue%-1
:Start
ECHO _____________________________________
ECHO(
IF %counter% EQU %loopvalue% (
 ECHO Connection to D:\ drive timed-out
 SET neterror=1
 GOTO End
)
ECHO Attempt %counter% of %trueloop% to D:\ drive
TIMEOUT /t 5 /NOBREAK >NUL
IF EXIST D:\NUL (
 ECHO Attempt %counter% successful.
 GOTO End
)
NET USE D: \\192.168.49.69\Data /PERSISTENT:YES
IF %ERRORLEVEL% NEQ 0 (
 SET /A counter=%counter%+1
 GOTO Start
)
:End

SET counter=1
SET loopvalue=8
SET /A trueloop=%loopvalue%-1

IF %neterror% EQU 0 GOTO Endofscript

:userconfirm
ECHO _____________________________________
ECHO(
SET /P userinput="Errors were found. Do you wish to try again [Y/n] " 
IF /I "%userinput%" EQU "y" GOTO Start
IF /I "%userinput%" EQU "" GOTO Start
IF /I "%userinput%" EQU "n" GOTO Endofscript
GOTO userconfirm

:Endofscript
ECHO _____________________________________
ECHO(
@ECHO Please wait, script is closing.
TIMEOUT /t 5 /NOBREAK >NUL
ENDLOCAL
@ECHO OFF
@EXIT /B 0

Logitech MX Revolution left mouse button double-click solution

The left mouse button on my Logitech MX Revolution had started to generate spurious double-click events for a single-click action. One way to attempt a fix is to open the unit and clean the micro-switch with a switch-cleaner.

I tried to avoid doing that as removing the gliding pads seemed too much. I looked for an alternative solution and implemented the following AutoHotKey code.

*LButton::
If (A_PriorHotkey=A_ThisHotkey && A_TimeSincePriorHotkey < 150) ;hyperclick
Return
sendinput {Blind}{LButton down}
KeyWait, LButton
sendinput {Blind}{LButton up}
Return

This initially seemed to work but I soon discovered that not all windows in Windows 8 Pro 64-bit would recognise a single-click when switching between application windows. In such situations I needed to right-click on the application window to switch to it. I tried various modifications of the code to get a fuller solution but so far I have not found one.

So, I bit the bullet and went for using a solvent to clean the micro-switch.

I opened the case as per the video and, after blowing out any dust, sprayed both the left and the right mouse button micros-switches with switch-cleaner. I think I actually over-sprayed the switches by accident but there does no damage seems to have occurred…perhaps it even helped? I then let the solvent dry out by waiting for about an hour (just to be sure!).

I re-assembled the mouse and tested at http://unixpapa.com/js/testmouse.html. Everything was working as expected. That was over two weeks ago and the un-commanded double-switching has not re-occurred. Excellent!

Logitech don’t make the MX Revolution anymore, and its direct replacement (the MX Performance) is, in some ways, an inferior device. So I’m glad I can hold onto my MX Revolution a little bit longer.

Article by Kulvinder Singh Matharu – 2013

Suspicion on NSA-endorsed encryption standards

Wired has good article with views from various cryptographic experts on the weaknesses of certain NSA-endorsed encryption standards and the suspicions that the NSA have backdoors to these standards or have trivial attack methods to decrypt messages encrypted with these standards. These come hot on the heals from statements issued by The Times and their review of documents provided by Edward Snowden.

The Wired article does highlight that there currently isn’t a consensus in the tech world if the standards are a result of incompetence or of conspiracy. Further investigation is required. The article is here:

Article by Kulvinder Singh Matharu – 2013

Logitech MX Revolution left button double-click; or AutoHotKey to the rescue, again!

[Update 22 Sept 2013 – I’ve stopped using this code as I’ve found that not all windows in Windows 8 Pro 64-bit will recognise when I single-click on them when switching between application windows. In such situations I need to right-click on the application window to switch to it. I’ve tried various modifications of the code but so far I have not found a solution.]

I use a Logitech MX Revolution mouse; I bought it several years ago and love the scrolling features. However, in the last few weeks, the left mouse button has begun to generate spurious double-click events for a single-click action. The micro-switch contacts are either dirty or worn out. I don’t know which. They only way to find out for sure is to open the beast up and spray the offending micro-switch with some contact cleaner much like this chap:

This is a very helpful video but I’m not ready to open up my MX Revolution until I’ve exhausted other less intrusive options. I thought that there may be a way to write a script that could recognise these spurious double-clicks and ignore them. It would need to involve timing the interval between each click and ignore the event if it was below a certain time duration. I’m thinking of AutoHotkey scripts as I’ve previously used AutoHotKey to automate scripts and troubleshoot software on my PC, and have shared some tips to this blog.

I checked to see if someone had already written a script for the double-click problem and I soon found one from earlier this year on a AutoHotKey forum:

This is the code I used:

*LButton::
If (A_PriorHotkey=A_ThisHotkey && A_TimeSincePriorHotkey < 150) ;hyperclick
Return
sendinput {Blind}{LButton down}
KeyWait, LButton
sendinput {Blind}{LButton up}
Return

I tested this at http://unixpapa.com/js/testmouse.html and it has significantly reduced the number of spurious events. Brilliant!

Do note that the original code used a time of <100, but I changed it to <150 as it seems to give a better result. I will play around with this value over the next few days or weeks. So far so good.

Systems setup: The PC is a desktop running Windows 8 Pro 64-bit.

[Update 22 Sept 2013 – I’ve stopped using this code as I’ve found that not all windows in Windows 8 Pro 64-bit will recognise when I single-click on them when switching between application windows. In such situations I need to right-click on the application window to switch to it. I’ve tried various modifications of the code but so far I have not found a solution.]

Article by Kulvinder Singh Matharu – 2013

Microsoft Office 2013 revised license – hooray!

I’d previously blogged about my disappointment with the Microsoft Office 2013 license restrictions:

Well, the good news today is that Microsoft have revised the licensing terms so that users can now, at last, move Office 2013 to a different machine:

Article by Kulvinder Singh Matharu – 2013

Microsoft licenses: Office 2013 is rubbish, Office 365 is better

Damn!

If Microsoft had said about a year ago that Office 2013 could only ever be installed on one computer then that would have been fair warning and placated a lot of people especially with the Office 365 offer. But by making the draconian Office 2013 licensing terms public only recently, Microsoft have angered a lot of people. Count me as one of those angry people as I purchased Office 2013 just a few days ago. Luckily I didn’t get around to installing it on my machine especially as I was thinking of getting a new machine in a few months time. I’m going to have to sell my copy of Office 2013; it’s pretty much useless to me.

Article by Kulvinder Singh Matharu – 2013