Jump to content

Windows 10 CMD script no longer working.


Ed_P

Recommended Posts

I have used this script on several Windows notebooks, the previous one Windows 10.  I now have a new notebook which is also Windows 10 and the script doesn't work.  The script allows me to use my old U3 USB drive on my notebook and to share it with the desktop machine upstairs and to read and copy files between them.

 


echo off&Title ShareMyU3Drv&Color 1F&MODE CON LINES=100 COLS=35
@if %1! == ! goto ShareMyU3
@goto RemoteQ

:ShareMyU3
@title Share My U3 drive&MODE CON: COLS=64 LINES=7&COLOR 1F
@echo on
@set SHR="Eds USB Drv"
::if not exist \\Dell_D400\%SHR%\*.*           goto ShareIt
::if not exist \\EDS-ACERNB\%SHR%\*.*          goto ShareIt
@if not exist  \\EDS-DELL\%SHR%\*.*            goto ShareIt
NET SHARE %SHR% /DELETE 

:ShareIt
if "%cd%" equ "C:\Windows\system32" (
     echo Being run as Admin
     cd Q:\ 
     goto ShareQ ) 
if "%cd%" neq "Q:\" ( 
     echo.
     echo.
     echo  cd="%cd%"
     echo  ** This cmd must be run from the Q: drive. ** 
     echo.
@     pause > nul
     exit )

:ShareQ
@cd ..
NET SHARE %SHR%=%cd% /USERS:1 /GRANT:Everyone,CHANGE
echo %errorlevel%
@if errorlevel 2 cls
@if errorlevel 1 cls&NET SHARE %SHR%=%cd% /USERS:1
@echo Press Enter to unshare %SHR%
@pause > nul
NET SHARE %SHR% /DELETE 
@if errorlevel 2  pause > nul
::if errorlevel 2 echo %SHR% not freed&pause > nul
@cd \documents
@exit

:RemoteQ
@title Quicken remote USB drive access&MODE CON: COLS=42 LINES=6
@echo off
color 1f
if exist \\DELL_D400\"Eds USB Drv"   subst Q: \\DELL_D400\"Eds USB Drv"
if exist \\EDS-ACERNB\"Eds USB Drv"  subst Q: \\EDS-ACERNB\"Eds USB Drv"
if exist \\EDS-DELL\"Eds USB Drv"    subst Q: \\EDS-DELL\"Eds USB Drv"
if not exist Q:\*.* pause > nul&exit
echo.
echo Q: drive assigned
echo.
echo  Press Enter to release the Q: drive.
pause > nul 
subst Q: /d
exit

 

The script has to be run as Admin and with the new notebook doing that causes the run location to change from q:\ to c:\windows\system32.  So I added a test to the script to account for that but the test fails and I get this error msg:


operable program or batch file.


 cd="C:\Windows\system32"
 ** This cmd must be run from the Q: drive. **

 

And the MODE LINES and COLS commands at the top of the script aren't working on the new system either.

 

While I would like to figure out what has changed and why my main priority is getting the script to work again.  What am I doing wrong???

 

Link to comment
Share on other sites

cd /d Q:\

Q:

   or

Q:

cd \

 

Why does the script need to be run as an Admin ?

And are you sure Q: is always already going to mapped?

 

Edited by crp
Link to comment
Share on other sites

The NET SHARE command requires Admin to run.

 

Yes Q: is the drive letter I've assigned to the USB drive so other USB drives won't take it's place.  And the script is on the Q: drive.

 

When run as Admin the %cd% value changes from Q: to \Windows\system32 so I added a test for that but the test fails!!  Why the %cd% value changes I assume is due to a new Windows change but why my test for the change fails is beyond me.

 

if "%cd%" equ "C:\Windows\system32" (
     echo Being run as Admin
     cd Q:\ 
     goto ShareQ ) 
Edited by Ed_P
Link to comment
Share on other sites

the test didn't fail. put in one of the options i showed you.

cmd shell via Admin starts in c:\windows\system32. I have no idea why, maybe Aryeh can explain it.

 

Link to comment
Share on other sites

  • 4 weeks later...

In case this thread shows up on some Windows user's Google search.  This works.

@if %1! == ! goto ShareMyU3
@goto RemoteQ

:ShareMyU3
@title Share My U3 drive&MODE CON: COLS=64 LINES=7&COLOR 1F
@echo on
@set SHR="Ed's USB Drv"
::if not exist \\Dell_D400\%SHR%\*.*           goto ShareIt
::if not exist \\EDS-ACERNB\%SHR%\*.*          goto ShareIt
@if not exist  \\EDS-DELL\%SHR%\*.*            goto ShareIt
NET SHARE %SHR% /DELETE 
:ShareIt
pushd %~dp0
@if "%cd%" neq "Q:\" (
@  echo.
@  echo.
@  echo  ** This cmd must be run from the Q: drive. ** 
@  echo.
@  pause > nul
@  exit )

@cd ..
NET SHARE %SHR%=%cd% /USERS:1 /GRANT:Everyone,CHANGE
@if errorlevel 2 cls
@if errorlevel 1 cls&NET SHARE %SHR%=%cd% /USERS:1
@echo Press Enter to unshare %SHR%
@pause > nul
NET SHARE %SHR% /DELETE 
@if errorlevel 2  pause > nul
::if errorlevel 2 echo %SHR% not freed&pause > nul
@cd \documents
@exit

:RemoteQ
@title Quicken remote USB drive access&MODE CON: COLS=42 LINES=6
@echo off
color 1f
if exist \\DELL_D400\"Eds USB Drv"   subst Q: \\DELL_D400\"Eds USB Drv"
if exist \\EDS-ACERNB\"Eds USB Drv"  subst Q: \\EDS-ACERNB\"Eds USB Drv"
if exist \\EDS-DELL\"Eds USB Drv"    subst Q: \\EDS-DELL\"Eds USB Drv"
if not exist Q:\*.* pause > nul&exit
echo.
echo Q: drive assigned
echo.
echo  Press Enter to release the Q: drive.
pause > nul 
subst Q: /d
exit

The solution is the pushd %~dp0 code.  👍

Edited by Ed_P
Code spacing
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...