In Windows 7 identify ip of a mapped drive and reconnect it - parsing

I know that I can show mapped drives in windows 7 using
c:\>net use
and it shows
Status Local Remote Network
-------------------------------------------------------------------------------
OK X: \\192.168.168.100\c\Output
Microsoft Windows Network
OK Y: \\192.168.168.100\c\Input
Microsoft Windows Network
Is it possible to discover if X (or Y) are connected to ip 192.168.168.100 ?
And in this case disconnect them using
net use X: /delete
net use Y: /delete
and reconnect them to same path but different ip using (from 192.168.168.100 to 192.168.168.101)
net use X: \\192.168.168.101\c\Output /user:user01
net use Y: \\192.168.168.101\c\Input /user:user01
and viceversa, if they are connected to 192.168.168.101, disconnect and reconnect
to 192.168.168.100
Can someone help me to parse the net use output ?

Not pretty but it works in my limited testing:
#echo OFF
setlocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
goto start
:checkone
setlocal
echo INFO: %3 is mapped to drive %2
set "server="&set "new="
for /F "usebackq tokens=1,* delims=\" %%A in ('%3') do #set server=%%A&set share=%%B
if "%server%"=="192.168.168.100" set new=192.168.168.101
if "%server%"=="192.168.168.101" set new=192.168.168.100
if not ".%new%"=="." (
call net use %2 /delete
call net use %2 \\%new%\%share% /user:user01
)
endlocal
#goto :EOF
:start
for /F "tokens=*" %%A in ('net use^|find ":"^|find "\\"') do #for /F "usebackq tokens=1,* delims=:" %%B in ('.%%A') do call :checkone %%B: %%C
If the remote path has spaces there will probably be trouble...

Related

How to make "URL Protocol" to launch application from its own directory instead of launching from c:\windows\system32?

I have registered a URL protocol in my system using below script to launch a batch file "showPath.bat".
#echo off
reg add HKEY_CLASSES_ROOT\ProtoTest /t REG_SZ /d "My Description" /f
reg add HKEY_CLASSES_ROOT\ProtoTest /v "URL Protocol" /t REG_SZ /d "" /f
reg add HKEY_CLASSES_ROOT\ProtoTest\shell /f
reg add HKEY_CLASSES_ROOT\ProtoTest\shell\open /f
reg add HKEY_CLASSES_ROOT\ProtoTest\shell\open\command /t REG_SZ /d "C:\TestFolder\showPath.bat" /f
pause
Content of "showPath.bat" is just to display the current working directory. ie.,
#echo off
SET var=%cd%
ECHO %var%
pause
If I run the batch file directly by double clicking it, I can see its path correctly. However if I launch the batch file using the URL protocol registered above. ie, from Chrome, browsing "ProtoTest://", the batch file runs, however display's the path "C:\Windows\system32" instead of the batch file's directory. So, I believe applications launched using URL protocol runs with system32 as working directory. Now How can I get the batch file run from its own directory when launched from browser using URL protocol - without modifying the batch file itself. Only URL protocol possible to be changed from my end.
Below code worked for me. I had my batch file in a folder that had spaces in it, so added "" along with escape char. However it gets added to registry as below in image without escape characters. Answer credit to #aschipfl
#echo off
reg add HKEY_CLASSES_ROOT\ProtoTest2 /t REG_SZ /d "My Description" /f
reg add HKEY_CLASSES_ROOT\ProtoTest2 /v "URL Protocol" /t REG_SZ /d "" /f
reg add HKEY_CLASSES_ROOT\ProtoTest2\shell /f
reg add HKEY_CLASSES_ROOT\ProtoTest2\shell\open /f
reg add HKEY_CLASSES_ROOT\ProtoTest2\shell\open\command /t REG_EXPAND_SZ /d "%ComSpec% /C \"cd /D \"C:\Source\For Ref\URL Protocol\BatchTest\" ^& showPath.bat\"" /f
pause

how to escape empty lines from end of command result

i use the following command to create a variable from my CPU Name:
#echo off
Rem create variable from cpu name
for /f "useback tokens=* skip=1" %%g in (`wmic cpu get name ^|findstr /i "."`) do (
set CPU_NAME=%%g
echo %CPU_NAME%
)
but the result is nothing, because there is some empty lines at end of "wmic cpu get name" command result and remove created variable
how can i solve it?
thanks a lot
Please search SO for delayed expansion.
call echo %%CPU_NAME%%
should show you the required data. This is one of several well-documented solutions.
There are some empty lines at the end of wmic cpu get name
Use findstr as follows to strip blank lines from the wmic output. You also need to use delayed expansion
Corrected batch file (test.cmd):
#echo off
setlocal enabledelayedexpansion
Rem create variable from cpu name
for /f "useback tokens=* skip=1" %%g in (`wmic cpu get name ^| findstr /r /v "^$"`) do (
set CPU_NAME=%%g
echo !CPU_NAME!
)
endlocal
Example usage:
> test
Intel(R) Core(TM) i5-2410M CPU # 2.30GHz
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
enabledelayedexpansion - Delayed Expansion will cause variables to be expanded at execution time rather than at parse time.
findstr - Search for strings in files.
wmic - Windows Management Instrumentation Command.
There is no need to echo the result within the for loop in your case because you are only setting a single name. Just echo it later.
#Echo Off
For /F "Skip=1 Delims=" %%A In ('WMIC CPU Get Name'
) Do For /F "Delims=" %%B In ("%%A") Do Set "CPU_NAME=%%B"
Echo=%CPU_NAME%
The second For loop is intended to remove the unwanted 'empty lines' you reported.

How to print multiple PDF files in different folders?

An example would be:
 Folder 1:
  a.pdf
  b.pdf
   Folder11
   c.pdf
  Folder 2:
  a.pdf
  b.pdf
   Folder21:
   c.pdf
printing all files between folders
And the cmd would have a way to find the file only putting part of the words?
Example
TEXT : ABC*.PDF
PRINT ABCDF.PDF
1. To loop over multiple files recursively:
FOR /f "tokens=*" %%F in ('dir /s /b *.pdf') DO echo "%%F"
dir /s /b *.pfd finds all pdfs (*.pdf), in all subdirectories (/s), in bare format - ie just the path name (/b)
DO echo "%%F" just echo's the result to the console.
"tokens=*" adds the whole line into %%F regardless of white spaces / other tokens
/F makes it run the ('dir ...') command
2. To print from command line use: From this question
AcroRd32.exe /t "C:\Folder\File.pdf" "Brother MFC-7820N USB Printer" "Brother MFC-7820N USB Printer" "IP_192.168.10.110"
Note: Path to AcroRd32.exe must be in your path environment variable
3. Putting it all together -- edit -- 'I've added taskkill to close acrord32 after printing
FOR /f "tokens=*" %%F in ('dir /s /b *.pdf') DO AcroRd32.exe /t "%%~F" "Brother MFC-7820N USB Printer" "Brother MFC-7820N USB Printer" "IP_192.168.10.110" & taskkill /IM AcroRd32.exe

CMD - get MAC address of iPhone

Im trying to get the MAC address of my iPhone from my windows machine.
GETMAC /s 10.30.114.14
And it returns:
ERROR: The RPC server is unavailable
But if I try to get a MAC address of any other device (laptops and what not), I get a good address within milliseconds.
So Im asking: Is there a way to get a MAC address of an iDevice from CMD?
**I know I can look at the device settings, but that is realy not what I'm after.
ping it and then grab the MAC from the arp cache:
#echo off
set IP=10.30.114.14
ping -n 1 %IP% >nul
for /f "tokens=2" %%a in ('arp -a ^| find "%IP%"') do set MAC=%%a
echo MAC: %MAC%
pause
Suppose that you a have the inputfile with computers or ip address list, you can give a try with batch file :
#echo off
Title Get IP and MAC address for remote PCs over the network using batch
Set "Copyright=by Hackoo 2021"
Title %~nx0 %Copyright%
Mode con cols=90 lines=12
cls & color 0A & echo.
echo ********************************************************************************
echo Get IP and MAC address for remote PCs over the network by %Copyright%
echo ********************************************************************************
echo(
if _%1_==_Main_ goto :Main
:getadmin
echo %~nx0 : self elevating
set vbs=%temp%\getadmin.vbs
(
echo Set UAC = CreateObject^("Shell.Application"^)
echo UAC.ShellExecute "%~s0", "Main %~sdp0 %*", "", "runas", 1
)> "%vbs%"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
goto :eof
::-------------------------------------------------------------------------------------
:Main
set "InputFile=%~dp0Hosts.txt"
set "OutPutFile=%~dp0IP-MAC.txt"
If Exist "%OutPutFile%" Del "%OutPutFile%"
If Not Exist "%InputFile%" (
color 0C & echo "%InputFile%" does not exist. Please check it first !
Timeout /T 8 /NoBreak>nul & Exit
)
Netsh interface ip delete arpcache >nul 2>&1
#for /f "tokens=* delims=" %%H in ('Type "%InputFile%"') do (
Ping -n 1 %%H>nul
#for /f "tokens=2" %%M in ('arp -a %%H ^| find "%%H"') do (
echo %%H : %%M
echo %%H : %%M>>"%OutPutFile%"
)
)
If Exist "%OutPutFile%" Start "" "%OutPutFile%" & Timeout /T 1 /NoBreak>nul & Exit

How to check the existence of a Windows service in a batch file

How can I check the existence of a Windows service in a batch file?
The user is inputting a service name, and I want to check that the service does exist before continuing with my script.
Try this:
>NET START | FIND "Workstation"
where "Workstation" is the name of the service
#echo off
color 1F
SET KEYS=HKLM\SYSTEM\CurrentControlSet\services\ACPI
for /f "tokens=3" %%i in ('REG QUERY "%KEYS%" ^| find "Start"') do set START=%%i
IF "%START%" == "%START%" ECHO %START% | find /I "%START%" && IF "%START%" NEQ "0x3" REG ADD %KEYS% /v "Start" /t REG_DWORD /d 3 /f >> %COMPUTERNAME%_MODIFIER.TXT
IF ERRORLEVEL 1 ECHO %KEYS% >> %COMPUTERNAME%_SERVICE_MISSING.TXT
OR
#echo off
color 1F
#sc query >%COMPUTERNAME%_START.TXT
ECHO REPORT MISSING INSTALL SERVICES >%COMPUTERNAME%.TXT
find /I "AcPrfMgrSvc" %COMPUTERNAME%_START.TXT >nul
IF ERRORLEVEL 1 NET START "AcPrfMgrSvc"
IF ERRORLEVEL 1 ECHO AcPrfMgrSvc >>%COMPUTERNAME%.TXT

Resources