How to check if current drive is on a local disk (cmd)? - windows

To get the drive the current batch resides in is easy using
set batchdrive=%~d0
But how is it possible to check if %batchdrive% is on a local drive and not on a (mapped) network share?
Checking for %SYSTEMDRIVE% or a fixed list "C:" "D:" ... is not reliable.

To check whether a drive (%~d0) is a local one, you could use a wmic query:
wmic LogicalDisk where(DeviceID="%~d0" AND DriveType=3) get Description,DeviceID,DriveType
Given that %~d0 expands to the local drive C:, the output looks like:
Description DeviceID DriveType
Local Fixed Disk C: 3
In case %~d0 is a network drive Z:, the error output is:
No Instance(s) Available.
Unfortunately, wmic does not set the ErrorLevel in case of no matches, but the above message is returned at the STDERR stream rather than the STDOUT stream, so we can apply redirection to discard STDOUT (in case the drive matches; so the get query is omitted as it is not used anyway) and redirect STDERR to STDOUT instead (so the error message is returned at STDOUT in case):
2>&1 > nul wmic LogicalDisk where (DeviceID="%~d0" AND DriveType=3)
Hence the command line returns nothing in case %~d0 is a local drive, but something otherwise. Now let us capture the (redirected) STDOUT by a for /F loop:
for /F "delims=" %%L in ('
2^>^&1 ^> nul wmic LogicalDisk where ^(DeviceID^="%~d0" AND DriveType^=3^)
') do echo Drive "%~d0" is not local!
So if %~d0 points to a local drive, the body of for /F is not executed, but otherwise it is.
According to this resource, WMI and therefore the wmic command line tool is available since Windows XP (Prof.) onward; it was not available on Windows XP Home though. wmic does not require administrative privileges. The Win32_LogicalDisk class is available since availability of WMI. Reference the following resources for more information about WMI/wmic: Windows Management Instrumentation: Frequently Asked Questions and WMIC - Take Command-line Control over WMI.

The first thing to check is if batchdrive is an unmapped network share (this happens if you start the batch file outside of cmd.exe, for example via double click or via a system call):
if "%batchdrive%" == "\\" set nshare=1
The second thing is to check if batchdrive is in the list of network shares. These are shown with net use, which output is similar to
status local remote network
-------------------------------------------------------------------
OK D: \\computer1\share1 Microsoft Windows Network
OK E: \\computer1\share2 Microsoft Windows Network
disconnected F: \\computer2\share Microsoft Windows Network
Command executed successfully.
Therefore we filter the output for all lines that looks like a disk drive with findstr /r /c:" [A-Z]: " and took the second part of the output via for /f "tokens=2".
Complete snipped (working on WinXP and above):
if "%~d0" == "\\" (
set nshare=1
) else (
set nshare=0
for /f "tokens=2" %%a in ('net use ^| findstr /r /c:" [A-Z]: "') do (
if "%%a" == "%~d0" set nshare=1
)
)
Comments for possible issues requested :-)

Related

How to find where windows is installed using batch file

Is there a way to create a simpel batchfile that finds the drive where windows is installed?
Use %SystemDrive% from the command prompt or in a batch file.
echo %SystemDrive%
or
d:>%SystemDrive%
c:>
Ken White has the "normal" way to get the system drive via %SystemDrive%. But that variable can easily be corrupted by setting your own value.
An alternative that should "always" work for any Win version later than XP is to use:
for %%A in ("%__APPDIR__%") do echo %%~dA
Of course you can set your own InstallDrive variable to the value of %%~dA.
The %__APPDIR__% variable is one of two special dynamic "variables" that always report the correct value, even if a user tries to override the value by explicitly defining their own variable of that name. However, the value can be overridden on XP. See Why can't I access a variable named __CD__ on Windows 7? for more info about dynamic variables %__CD__% and %__APPDIR__%.
Just in case 'finds the drive' doesn't just mean the drive letter:
#Echo Off
For /F "Tokens=2,5,6 Delims=\|" %%I In ('WMIC OS Get Name') Do Echo=%%I %%J %%K
Timeout -1
…and for no real reason:
#Echo Off
For /F "Tokens=2,5,6 Delims=\|" %%I In ('WMIC OS Get Name') Do (
Set _di=%%I %%K %%J )
Set/A _dn=%_di:~-1%+1
For /F "UseBackQ Tokens=2 Delims==" %%L In (`WMIC DiskDrive Where^
"DeviceID Like '%%PHYSICALDRIVE%_dn%'" Get Model /Value`) Do Echo=%_di% %%L
Timeout -1
Type cd %windir% in a dos command prompt, and then press ENTER.
Note the current folder. This is the folder in which Windows is installed.
You actually do not need a batch file for that. Just hold the windows key and press R to open up a small window in which you type %windir% and hit Enter.
A Windows Explorer window will pop up showing the directory of the Windows installation. You can the click onto the bar, where the directory is shown (like the URL-bar of the browser) to get the direct path including any parent folders and the drive letter.

dot net command: net use - How to only get Alphabet and pathname?

When I do a "net use" on my command prompt, it will display the following:
New connections will be remembered.
Status Local Remote Network
-------------------------------------------------------------------------------
OK W: \\hfs2\ATS\Novell Profile Backup\wk\one\two\three\four\five\six\seven\eight\nine\ten\eleven\twelve\thirteen
Microsoft Windows Network
OK X: \\hfs2\ATS\Novell Profile Backup\wk\one\two\three\four\five\six\seven\eight\nine\ten
Microsoft Windows Network
OK Y: \\hfs2\ATS\Novell Profile Backup
Microsoft Windows Network
Unavailable Z: \\hfs2\ATS Microsoft Windows Network
The command completed successfully.
How do I extract ONLY get the drive alphabet and pathname?
W:
\\hfs2\ATS\Novell Profile Backup\wk\one\two\three\four\five\six\seven\eight\nine\ten\eleven\twelve\thirteen
X:
\\hfs2\ATS\Novell Profile Backup\wk\one\two\three\four\five\six\seven\eight\nine\ten
Y:
\\hfs2\ATS\Novell Profile Backup
Z:
\\hfs2\ATS
EDIT
WMIC does not require administrator rights
It does require rights for what you are trying to do. You can't use it to do admin things if not an admin.
It also requires an administrator to run it once on a system to set it up.
From Help
User Account Control
Under UAC, accounts in the local Administrators group have two access tokens, one with standard user privileges and one with administrator privileges. Because of UAC access token filtering, a script is normally run under the standard user token, unless it is run "as an Administrator" in elevated privilege mode. Not all scripts required administrative privileges.
Scripts cannot determine programmatically whether they are running under a standard user security token or an Administrator token. The script may fail with an access denied error. If the script requires administrator privileges, then it must be run in the elevated mode. Access to WMI namespaces differs depending on whether the script is run in elevated mode. Some WMI operations, such as getting data or executing most methods, do not require that the account run as an administrator. For more information about default access permissions, see Access to WMI Namespaces and Executing Privileged Operations.
Wmic
The first time you run Wmic after system installation, it must be run from an elevated command prompt. The elevated mode may not be required for subsequent executions of Wmic unless the WMI operations require administrator privilege.
Use WMIC
wmic netuse get /format:list
gives you what's available.
Use something like
wmic netuse get remotepath, localname /format:list
To put the output in a file or on the clipboard.
WMIC specific switch
/output or /append
eg
wmic /node:"#%userprofile%\desktop\ComputerName.txt" /output:"%userprofile%\desktop\EventLog.html" /failfast:on PATH Win32_NTLogEvent where (EventIDentifier=42 or eventidentifier=1003) get /format:hform
(/node is a list of IP addresses and/or computer names of computers to run the command against, one IP address or computer name per line)
General Command Prompt File Redirection
Appending >filename.ext (or >>filename.ext to append to a file)to a command writes the output to the file rather than the screen.
wmic baseboard get product,Manufacturer,model,partnumber>MotherboardPartNum.txt
General Command Prompt Piping
Appending |command sends the output to a command rather than the screen. The usefull commands that output is sent to are
find or findstr (finds and filters text)
sort (sorts the output)
more (displays output to screen one page at a time)
clip (puts output onto the clipboard)
null (makes the data disappear for good - used for unwanted error messages)
wmic baseboard get product,Manufacturer,model,partnumber|clip
Combining Piping and Redirection
So we can combine them. To send the list to a file on the desktop in reversed sort order (z to a) with blank lines removed.
wmic service get name,displayname /format:list|findstr .|sort /r>"%userprofile%\desktop\services_reversed.txt"
WMIC Output Options
The output options are
/Format:list (a list - use notepad to view)
/format:table (a table - use notepad to view)
/format:hform (an html list - name the file's extension .html so IE will show)
/format:htable (an html table - name the file's extension .html so IE will show)
/format:csv (comma seperated variable - used for importing data into other programs such as excel)
also value, mof, rawxml, and xml.
So,
sort /?
find /?
findstr /?
more /?
clip /?
There are some problems in the output of net use
The Status field can hold information or be empty.
The Network field can have multiple values depending of network mapping. In my case i have "Microsoft Windows Network" and "Netware Services". So, there is no direct substitution.
The Network field can be in the same line that the Remote field or can be on the next line, and as the Remote field may include spaces, checking the character at the column limit position is not reliable. It is necessary to delay the check until the next line is readed to determine if it contains remote data.
So, not a one liner to handle it
#echo off
setlocal enableextensions disabledelayedexpansion
set "drive="
for /f "skip=6 tokens=1,* delims=\" %%a in ('net use') do (
if defined drive (
setlocal enabledelayedexpansion
if "%%b"=="" (
echo !drive! !networkPath!
) else (
echo !drive! !networkPath:~0,26!
)
endlocal
set "drive="
)
if not "%%b"=="" for /f "tokens=2" %%c in ("x%%a") do (
set "drive=%%c"
set "networkPath=\\%%b"
)
)
%%a loop will read the lines from net use and split them using a backslash as delimiter. This will allow us to determine if the line contains or not a network path (if there is no second token, the line did not contain a backslash).
As we are delaying the output of the information in one line until the next is readed (to determine if the remote path continues in the Network column), the first operation inside the for loop is to determine if we have data pending from previous loop. If there is data, depending on the content of the current line we select what to output.
Once the data is echoed, if the current line contains network information, it is saved for later output.
This is the faster solution, but there are two alternatives that require less code:
multiple net use commands
#echo off
setlocal enableextensions disabledelayedexpansion
rem For each line in the output of the net use that includes a drive letter
for /f "delims=" %%a in ('net use^|find ":"') do (
rem Retrieve the drive letter from the line
for /f "tokens=2" %%b in ("x%%a") do (
rem Run a net use with the drive letter and output the drive and the path
for /f "tokens=1,* delims=\" %%c in ('net use %%b') do if not "%%d"=="" echo(%%b \\%%d
)
)
Less code, but as multiple net use commands are executed, it is slower
Use WMIC
#echo off
setlocal enableextensions disabledelayedexpansion
for /f "tokens=2,3 delims=," %%a in (
'wmic netuse get LocalName^, RemoteName^, Status /format:csv ^| find ":"'
) do echo(%%a %%b
Less code, but in this case, adminitrator righs are required to run the command
I might be a little late but this helped me
WMIC NETUSE GET LocalName, RemotePath /FORMAT:TABLE | FIND /i ":"

How to read net use generated text file and use the net use command to remap the network drive

How to read net use generated text file lets called it network_drive.txt that consist of the current machine mapped network drive as image below:
New connections will be remembered.
Status --- Local --- Remote ------------------ Network
-------------------------------------------------------------------------------
OK ---------- H: ---- \\server\users\john -----
Microsoft Windows Network
OK ---------- Y: ---- \\server\e$\ --------------
Microsoft Windows Network
The command completed successfully.
How to read the file above to map the network drive again with the same Letter path and path only if the status is ok and ignore the unavailable one?
update!
#echo off
set drive=\\server\users\john\
net use > %drive%\%USERNAME%_temp_Networkdrive.txt <-- generating net use file
for /f "skip=6 delims=*" %%a in (%drive%\%USERNAME%_temp_Networkdrive.txt) do (
echo %%a >>%drive%\%USERNAME%_del_Networkdrive.txt ) <-- deleting the first 6 lines
xcopy %drive%\%USERNAME%_del_Networkdrive.txt %drive%\%USERNAME%_Networkdrive.txt /y <-- make a new copy of the network drive file after deleting the lines
findstr /v "The command completed successfully." %drive%\%USERNAME%_del_Networkdrive.txt > %drive%\%USERNAME%_Networkdrive.txt <-- find the string and delete them and make a new copy of file with just the network drives
del %drive%\%USERNAME%_del_Networkdrive.txt /f /q
del %drive%\%USERNAME%_temp_Networkdrive.txt /f /q
for /f "tokens=2,3,4" %%a in (%drive%\%USERNAME%_Networkdrive.txt) do ( net use %%a %%b ) **<-- find the letter path and the drive path and map accordingly.**
however..
in some cases, sometimes the "Microsoft Windows Network" is on the same line as the letter and Drive path and hence deleting the record/line.
can someone help pls?
Update.
I removed Microsoft Windows Network from the findstr line because the tokens in the for loop would only pick up the second and third strings for the net use command.
I have tested it and it works.
Also, it would be a good idea to use if exist command on the second line just to see if the file is exist before running the other commands.
#ECHO OFF
SETLOCAL
FOR /f "tokens=2*delims=: " %%a IN ('type q20294868.txt^|find "\"^|findstr /b "OK"') DO ECHO NET use %%a: %%b
ECHO(======================
FOR /f "tokens=2*delims=: " %%a IN ('type q20294868.txt^|find "\"^|findstr /b "OK"'') DO FOR /f %%c IN ("%%b") DO ECHO NET use %%a: %%c
GOTO :eof
This should do what you appear to want.
You should replace type q20294868.txt with net use for your situation. q20294868.txt is simply a file I used to save your test data.
There are two separate methods here. The text is filtered first for lines containing \ and then for those /b beginning "OK"
I'm unsure whether the network name may potentially be included on a data line rather than on a line by itself, consequently I've devised the second method. The first is simpler, the second more robust.
Note that your original findstr would have eliminated any lines containing ANY of the individual words contained in the quotes - see findstr /? from the prompt for more information.
And of course, the resultant NET USE command is merely ECHOed to the screen, not executed.

Batch script, call cmd on servers

I am trying to call x.cmd from many servers in a list and basically x.cmd will return a log file so I will copy the output log files into a new folder.
the structure in that list is:
server name&folder path of the x.cmd on thet server&output folder name that I will create
when I run the script below, it doesn't return any error and return value is 0. but x.cmd just doesn't do anything when I check on the actual server. x.cmd working fine once I run it manually.
Please if possible point me out the mistake of my script. my other concern is the folder path being too long(but it is within the limits of Microsoft if I am right). :) Thanks for your time.
#echo on
cd /d %~dp0
setlocal EnableDelayedExpansion
set serverList=List.txt
for /f "usebackq tokens=1,2,3* delims=&" %%A in ("%serverList%") DO (
set remoteCmd=x.cmd
wmic /node:%%A process call create "%%B\remoteCmd"
pause
robocopy %%B\logs Output\%%C /e
)
endlocal
Pause
JS
The problem seems to be that you're not actually calling x.cmd on the remote servers, since the variable isn't substituted in the wmic call. Move the definition of remoteCmd out of the loop and enclose remoteCmd in the wmic call in percent signs:
set serverList=List.txt
set remoteCmd=x.cmd
for /f "usebackq tokens=1,2,3* delims=&" %%A in ("%serverList%") DO (
wmic /node:%%A process call create "%%B\%remoteCmd%"
pause
robocopy %%B\logs Output\%%C /e
)
Update 1: Since you're getting a response on the shell with a processID and Error level=0, it's fairly safe to say that x.cmd was in fact started on the remote server. The fact that the logfile does not turn up in the same directory where x.cmd resides in, is very probably because the logfile is not given an absolute path in x.cmd. That means it will be created in the directory on the remote machine where the cmd.exe is located, which is executing x.cmd. In most systems this will be %WINDIR%\system32, i.e. c:\windows\system32.
Update 2: If modifying x.cmd is no option, then you need to change the path to it's directory before calling it. You can do it like this:
wmic /node:%%A process call create "cmd /c cd /d %%B & x.cmd"
Note: The cd /d allows you to change directories even across different drives.
Update 3: Since there are spaces in the directory names you'll need to enclose them with quotes - which will have to be escaped, since there already outer quotes enclosing the whole remote command. So that means:
wmic /node:%%A process call create "cmd /c \"cd /d %%B\" & x.cmd"
Note: A possible alternative to the inner quotes would be to use 8.3-filenames of the involved directories. You can view those with dir /x on the command line.

Windows: Obtaining and Storing a Machine Name in a Variable

I have a script that is run over a network, with VPN being the same as a LAN environment.
The script previously worked fine, as we had variables that stored the username and password for the administrator. However, due to a recent change, when we map a drive over the network and whatnot, the machine name is now needed in front of the administator username, E.g. machinename2343\administrator.
What I would like to do is take an existing command - perhaps such as nbtstat - and after entering the ip address, have the program pull the machine name and insert it into a variable.
I have found that Nbtstat can give me the machine name, but provides large amounts of unnecessary information for my task. Is there a way to filter out just the machine name in a reliable and consistent manner, or is there perhaps another network related command that perform in the same capacity?
`#echo off
FOR /f "tokens=1* delims= skip=23 " %%a IN ('nbtstat -a IPADDRESS) DO (
SET VARIABLE=%%a
GOTO Done
)
:Done
echo Computer name: %VARIABLE%`
You could do ping /a. The computer name is resolved. And this computer name is the second token. I haven't taken care of Error checking. I believe you could implement that yourself.
Try this:
#ECHO OFF
FOR /f "tokens=2* delims= " %%a IN ('PING -a -n 1 IPADDRESS') DO (
SET Variable=%%a
GOTO Done
)
:Done
echo Computer name: %Variable%
Put this in your batch file where it would fit.
You could just use the %computername% environment variable.
When I first read your post I thought you were running the batch file remotely on each machine. If that were the case having %computername% in the batch file would work, because when the batch file is executed remotely %computername% would be expanded based on the remote machine's environment variable not the local machine.
Looking back on it, it's still not very clear, but based on your comment I assume the batch file is running locally and then connecting to a set of machines to perform some operation(s).
You could use tool the WMI command-line tool to get the computer name. The solution would look similar to #Thrustmaster's, but I think it's a little cleaner since the output of wmic, in this case, does "filter out just the machine name in a reliable consistent manner." Of course you'd replace the 127.0.0.1 with the ip you want to query.
#ECHO OFF
FOR /F "tokens=*" %%A IN ('wmic /node:127.0.0.1 ComputerSystem Get Name /Value ^| FIND "="') DO (
SET COMP.%%A
)
ECHO %COMP.NAME%

Resources