W10: Copy a file to a usb device, based only on its label, not the drive letter, using CMD - batch-file

We have the following problem:
12 computers without internet access, from which we have to retrieve a file at a specific time (let's say once per hour). We are using an usb drive in order to take the files.
I tried to use CMD and target the USB as per the name, and not the drive letter, as it follows:
for /f %D in ('wmic volume get DriveLetter^, Label ^|, Label ^| find "USB2"') do set myD=%D
in order to use move hours.csv %myD%\ but unfortunately it returns Volume label (32 characters, ENTER for none)? without assigning the variable.
Is there any workaround to use the same syntax for USB drives? (it works for the current installed ssd drives)

Related

Read current drive of batch file and use drive name in command

I have the following problem: We send customers a USB-stick with content that they need to import onto a Linux-computer via a import-function (no influence on the Linux-part, only on the USB-stick part). The Linux import-function cannot read files with spaces, i.e. the file "system volume information" which is created on every drive of a Windows computer disrupts the import function.
With a google research I found out, how I can delete this file on the drive of the USB-Stick. If I do that before disconnecting the USB-stick from my Windows computer, I can import the content on the Linux computer. I do this with a batch-file containing the following command:
rd /s /q D:\"system volume information"
However, those USB-Sticks are sent out to customers that will want to view the content on a Windows-computer as well, before importing it, i.e. the file "system volume information" is created anew. I would like to make it as simple as possible for the customer and put a batch-file on the stick where they simply have to double-click on it in order to delete the file. This is where my problem lies: I cannot be sure, that it will be drive D: where the stick will lie. This is why I am looking for a function or command that reads the drive of the batch-file (USB-Stick) and does the command of delete "system volume information". I read some things about %~d0 but I could not find out how I specifically have to write the command to make it work.
Would you be able to help me? I have very limited experience with batches, so I apologize if this question seems very easy for you.
Thanks
Milak
I have not tested the code:
#echo off
set P=%~d0
set "P=%P%\system volume information"
rd /s /q "%P%"

for /f reg query finding the subkey which contain my value

I want to find the parameters of the wifi cards remotely on many notebooks.
The Key is HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318} but the subkey is not the same for every notebooks, depending the driver and the others networkcards.
Sometimes the subkey is 0012, 0013 or 0007. I need to find the right subkey by filtering the *mediatype (0x10) and after having collected the subkey display the driverdesc. and some other values.
Is there a way to find the subkey which contain my value?
I suggest you use an appropriate tool or API for this. The command
wmic /node:host1 nic list brief
will list the NIC parameters of the (remote) host "host1". You only have to filter the results for WiFi NICs and to get the information you want.
You can easily embed calls to WMIC in Powershell (which is available in Windows XP and up). Even for parsing the output I would not recommend DOS batch anymore.

Making Bootable USB From DVD using Command line

Is there any way to create a boot-able USB from a windows DVD via command line?
It is accepted using third party application which accepts command line arguments.
I searched internet but did not found any way.
You can do this using the program diskpart which is included in windows.
You need the following steps:
SELECT DISK <DRIVE NUMBER>
CLEAN
CREATE PARTITION PRIMARY
SELECT PARTITION 1
ACTIVE
FORMAT FS=NTFS QUICK
ASSIGN
EXIT
save them in a txt file. Find out the drive number using "LIST DISK"
You can then call diskpart with the /s parameter and give the saved textfile as a script.
Afterwards you can copy the content of the dvd over.
Now as to really do this programmatically:
The main difficulty would be selecting the correct drive number in case it may vary.
The main problem however is that this is a pretty dangerous operation. If you select the wrong disk (as stated those drives are selected by number not even by their drive letter) instead of your usb drive you might format a partition of your harddrive. I would personally not really fully automate the task but rather do it by hand.

Mapping Drive to an unknown share name?

I'm trying to create a batch file that can look through contents of network shares to find a specific named directory and subdirectory and then map a drive to the share that contains these. Problem being that, whilst the contents of the share have these folders in them to identify that this is the correct share, the share name itself may be different in different setups, so I can't just use the share name in the script.
In other words
\\server\share\sims\setups
the sims and setups directories will ALWAYS exist in the share, but the share name is the thing that can be different. The server name will already exist in the batch file as it is set by using the set /p command to prompt the user for the servers ip.
i have looked at the find and the forfiles commands but these seem to only work to search locally and not on network paths.
Thanks
As indicated by foxidrive you can use the net view command to list shares of a server.
The for command is able to iterate over output from commands. Combining these in the following rough commandline does map a drive for me if a certain folder/file is present.
for /F %a in ('net view \\srv') do if exist "\\srv\%a\sims\setup" net use z: \\srv\%a
(don't forget to change %a to %%a if you use this line in a command-script)
The net view does output some noise but that doesn't screw the result. You could add some extra handling to only handle shares after the '-----' line is received.
How does this work?
net view \\server outputs the following:
Shared resources at \\server
Awesome server
Share name Type Used as Comment
-----------------------------------------------------------------------------
Configuration Disk System Configuration
Download Disk Download Share (Full Access)
Public Disk Public Share (Full Access)
Blah Disk Blah share (Full Access)
The command completed successfully.
The for /f command parses each line up to the next space. From the FOR /?
FOR /F ["options"] %variable IN (`command`) DO command [command-parameters]
...you can use the FOR /F command to parse the output of a
command. You do this by making the file-set between the
parenthesis a back quoted string. It will be treated as a command
line, which is passed to a child CMD.EXE and the output is captured
into memory and parsed as if it was a file.
Using that technique would echo the following result if run on net view \\server:
Shared
Network
Share
-----------------------------------------------------------------------------
Configuration
Download
Public
Blah
The
The EXIST operator checks if a file/directory exists. By concatenating the servername, share and the expected folder you can find out if that folder does excist on that share.
As I said the first 4 lines and the last line are noise and should be ignored but they don't hurt too much in this scenario.

Number optical disc drives in C like drutil does for OS X command line

Optical disc device names (examle: /dev/disk4) are a bit weird on Mac OS X. Unlike on other OSs, they belong to the mounted media, not the drive so only exist when a disc is inserted. Additionally they are in no term stable. They can change already when anoter .dmg file is mounted before the next disc is inserted into the drive. On Windows drive letters are stable and linux has device names like /dev/cdrom or /dev/sr0 which are quite stable. Changes in hard discs, usb drives and packages have no effect on optical device names there.
I develop a C library working with audio CDs and would like to use the drive numbering exposed by drutil from C.
drutil list lists all drives connected to the machine (with or without disc and gives them numbers.
With drutil details -drive 1 I can then get the details for the first drive, including the (BSD) device name when a disc is in the drive.
This would be perfect to set a prefered drive in the configuration or similar.
In C I wasn't able to list or index the drives without discs. So when I get the first inserted disc I don't know which drive this disc belongs to.
I can get device names for inserted discs with the IOCDMediaClass.
I do get a one drive listed with the IOCDBlockStorageDeviceClass (with or without a disc inserted), but I can't access the device name even when a disc is in the drive.
I prepared some example code that tries to list members of both classes and the device name if available.
The output without a disc in the drive:
detected media:
detected CD block devices:
drive 1:
class name: IODVDServices
The output with a disc in the drive:
detected media:
drive 1:
device name: disk4
class name: IOCDMedia
detected CD block devices:
drive 1:
class name: IODVDServices
PS:
I know I can call drutil from C, but that would be a last resort.
I found the information attached to IODVDServices using ioreg -r -c IODVDServices (ioreg).
The corresponding registry entry has (grand)children of which one is of type IODVDMedia when no CD is in the drive and of type IOCDMedia when an audio disc is in the drive (including the device name when a disc is inserted).
These children can be accessed with IORegistryEntryCreateIterator() and tested for being a certain class with IOObjectConformsTo():
static io_object_t get_media(io_object_t storageDevice)
{
io_iterator_t childrenIterator;
io_object_t nextChild;
io_object_t mediaObject = 0;
IORegistryEntryCreateIterator(storageDevice,
kIOServicePlane, kIORegistryIterateRecursively,
&childrenIterator);
while ((nextChild = IOIteratorNext(childrenIterator))) {
if (IOObjectConformsTo(nextChild, kIOCDMediaClass))
mediaObject = nextChild;
else
IOObjectRelease(nextChild);
}
IOObjectRelease(childrenIterator);
return mediaObject;
}
I pushed a complete solution to gist.
This tries to find a mounted media attached to a specific drive of IOCDBlockStorageDeviceClass. Finding the device name of that media works the same as specified in the question and the CDROMSample provided by Apple.
EDIT:
You can also just take any BSD name in one of the (recursive) children with IORegistryEntrySearchCFProperty. There shouldn't be other children with device names and this saves you the additional iteration.
deviceFilePathAsCFString = IORegistryEntrySearchCFProperty(storageDevice,
kIOServicePlane, CFSTR(kIOBSDNameKey),
kCFAllocatorDefault, kIORegistryIterateRecursively);
This is used in the implementation for libdiscid.

Resources