batch copy folder located by guid - batch-file

In a batch file, can I copy a folder located by a constant path like \\?\Volume{GUID}?
When copying (copy, xcopy or robocopy) a directory and its content from a local removable drive (a usb external drive for instance) to another location on the same drive, I'd like to use unique and constant absolute paths like \\?\Volume{GUID} to avoid using drive letters that can change over time.
To operate the copy, the batch file is meant to be placed on the removable device, but in case the file is moved or placed somewhere else I'd rather be sure it's operating on the good drive.
So far I've tried:
COPY can handle \\?\Volume{GUID} paths to copy a file but cannot copy
folders
XCOPY returns an "invalid drive" error
ROBOCOPY gives a "network path not found, wait 30 sec..."
for each command above : syntax variations with \\?\UNC\Volume{guid} and with the trailing "\"
Am I doing something wrong or is this just not the way to do that?
Is there another way to use invariant locations?
Ideally it should involve the least tweaking possible. By tweaking I mean: labeling the drive or giving it a fixed letter, etc.

Have a look at pushd /? and popd /?
pushd \\?\UNC\Volume{guid}
will short time map the UNC path as a network drive and change the directory to it according to the next free drive of your computer from the end of the alphabet.
For example, you got Z:\ and Y:\ already it will map the path to X:\ and change the current directory to X:\.
From that on you can go for copy filefromServer.foo C:\localfile.bar
The other commands such as XCopy can use the mapped drive as well.
To unmap the drive use popd.

None of the copy function can work with unc path.
Thus the only way is a workaround that consists in retrieving the drive letter through wmic command.
Here's the code :
for /F "usebackq tokens=1,2 delims==" %%G in (`
wmic volume where "DeviceID='%_volID:\=\\%'" get DriveLetter /value
`) do set _%%G=%%H
with _volID being the variable containing the unc path of the volume in question

Related

Copy files matching the names in a list of paths and paste them in one new folder?

I'm working in a windows 7 machine and I'm trying to take all of the files matching the names in a list of file paths (I have the list saved as a csv, rda, and can make a txt file if needed). Ie: the list looks like:
Y:/iglgrelkgjkrle/originals/jsfhdjk.xls
Y:/iglgrelkgjkrddsle/ffhej/originals/jsfhdjk.xlsx
Y:/kssrldsse/ffhej/originals/jsfhdjk.xlt
Y:/blahblah/blah/blahhh/blahhhhhh/originals/blahahaha.pdf
...
...
And basically I want all of these files in this list copied to a new folder in a different location. Thanks!
Almost any problem can be solved with a FOR statement in windows command processor. Using for /f we can search a list in a text document and for each item (This case; location) specified, can run a command to copy it to a new location.
For copying the file, xcopy will be very handy as it has many copy option switches we can use such as /i /z /y.
/I - If in doubt always assume the destination is a folder
/Z - Copy files in restartable mode. If the copy is interrupted part way through,
it will restart if possible.
/Y - Suppress prompt to confirm overwriting a file. (Use /-Y for reverse)
In the following commands bellow, C:\list.txt is used as an example. This is where you specify the location of your list file. This can support a wide range of file formats including html. It does not hurt to try your extensions.
For the place to output the copied files - C:\CopyFolder is an example of the location of the folder you wish to send them too. You can also send them to a local server via \\server\folder\.
From command Line:
for /f "delims=" %i in (C:\list.txt) do (xcopy "%i" "C:\CopyFolder" /i /z /y)
From batch file:
for /f "delims=" %%i in (C:\list.txt) do (xcopy "%%i" "C:\CopyFolder" /i /z /y)
If this has solved your issue, please don't forget to mark this response as solved. I will be happy to further explain any questions!

trouble with letters in .bat command to copy files from a dvd

I created a .bat file to replace 3 files from a DVD burned into a directory on my PC, the directory in all others the path is fixed, however it has a complication in case of my driver cd player and dvd the letter is j. But in other drivers when inserted the dvd of course the letter will change to f or g or i for example, how to solve this? How exists? because the Type the folder where it will be copied this defined in problem, but how to get and copy all files of this folder specify in or add variants and search type let's assume that the driver is with the letter n as I put in bat to find this folder with those Files and paste inside the folder that I specified? Is it possible?
I have the following .bat: copy j:\dvd folder\*.* "c:\file of destination in the pc. This .bat file recognizes the drive J as if it were in any other PC in a universal way let's say, regardless of which PC the bat is fired it will go into that DVD folder will copy and paste in the specified folder in c: windows windows folder Is not the problem, the problem is fixed the problem is this other unit of dvd in which the letters of the unit change from there will not work when clicking this bat because it is in my pc for example in another it will be i, this is the problem.
You may be able to leverage the WMI command line:
#Echo Off
Set "CDROM="
For /F "Skip=1 Delims=" %%A In (
'"WMIC CDROM Where (MediaLoaded='TRUE') Get Drive 2>NUL"'
) Do For %%B In (%%A) Do If Exist "%CDROM%\DVD Folder\" Set "CDROM=%%B"
If Not Defined CDROM GoTo :EOF
Copy "%CDROM%\DVD Folder\*.*" "%SystemDrive%\Somewhere Existing"
Timeout -1
Think from another perspective.
#echo off
copy "%~Dp0Dvd Folder\*.*" "C:\Folder\"
rem We don't need \ in between %~Dp0 and Dvd Folder because %~Dp0 comes with a \
Place this batch file in your DVD root folder.
Explanation:
%~Dp0

WMIC Batch File

I am trying to use a .bat to get system info with WMIC. I want to create a folder in C:\ with the name of the computer, then have a few .txt files made within the folder with the info on I am seeking. This is what I have so far:
start cmd
cd C:\
md C:\%computername%_Software_Baseline
wmic
/output:C:\%computername%_Software_Baseline\Installed_Software.txt product get name,version
This is where my first roadblock is. I get the message that the file name is invalid. The next few are the other .txt files I want to create in that folder as well (Once I can do the first I should be able to apply it to these as well).
/output:C:\%computername%_Software_Baseline\Computer_System_Information.txt computersystem get domain,manufacturer,model,name,totalphysicalmemory
/output:C:\%computername%_Software_Baseline\Disk_Drive_Information.txt diskdrive get model,size,manufacturer
/output:C:\%computername%_Software_Baseline\CPU_Information.txt cpu get architecture,currentclockspeed,description,extclock,l2chachesize,manufacturer,name
/output:C:\%computername%_Software_Baseline\BIOS_Information.txt BIOS get serial number,name,smbiosbiosversion,manufacturer
/output:C:\%computername%_Software_Baseline\OS_Information.txt OS get name,cdsversion,manufacturer,freephysicalmemory,buildnumber
I also want this to save everything to a folder on my network as well, but that can wait until I get the script going.
I can't understand why not to perform the task within current cmd instance omitting start cmd:
#ECHO OFF
SETLOCAL enableextensions
md "C:\%computername%_Software_Baseline"
wmic /output:"C:\%computername%_Software_Baseline\Installed_Software.txt" product get name,version
or using pushd - popd command pair:
#ECHO OFF
SETLOCAL enableextensions
md "C:\%computername%_Software_Baseline" 2>NUL
pushd "C:\%computername%_Software_Baseline"
wmic /output:"Installed_Software.txt" product get name,version
rem further wmic commands here
popd
PUSHD: Change the current directory/folder and store the previous folder/path for use by the POPD command.
POPD: Change directory back to the path/folder most recently stored by the PUSHD command.
When a UNC path is specified, PUSHD will create a temporary drive
map and will then use that new drive. The temporary drive letters are
allocated in reverse alphabetical order, so if Z: is free it will be
used first.
POPD will also remove any temporary drive maps created by PUSHD.

Getting Drive Letter using the Drives Property Name

I have a backup up batch file that I run on several computers and laptops the file is located on different drive (letters) on each machine. So the batch file needs to determine the files location and then run it from that drive, so is there a way for one get the drive letter using the drives property name?
Regards,
Paul
This will detect the last drive which has the file (add more drive letters)
#echo off
set "drv="
for %%a in (c d e f g h i j k l m n) do if exist "%%a:\" if exist "%%a:\folder\name.txt" set "drv=%%a:"
if defined drv echo the file was found on drive %drv%
Not very clear what you mean, but try this on the command line:
(if you put it in a BAT file, change %a to %%a)
for /F "delims=\" %a in ('cd') do echo %a
The result of the echo will be your current drive (frequently C:)
I am afraid there is a misunderstanding here. If you have a Batch file that is running, then there is a very easy way that "the batch file to determine the location from where it start running":
#echo off
set myDrive=%~D0
echo I am running from: %myDrive%
If the batch file is in another location, then it first needs to start running in order to determinate the location of other files and then just change the current directory to such location. In this case, foxidrive's answer provides a solution.

I am trying to overwrite sticky keys(sethc.exe) with cmd.exe but when running the script it cant find files

I am trying to overwrite two files with each other but it gives me a message that it cant find the files specified.
#echo off
set /p Drive=Enter local hard drive letter:
if "%Drive%"=="" goto :eof
xcopy Drive:\windows\system32\sethc.exe Drive:
xcopy Drive:\windows\system32\cmd.exe Drive:\windows\system32\sethc.exe
Make sure you files are in the same path as your batch file is, for example:
#echo off
start minecraft.exe
In this example, obviously it starts minecraft, but in the format it currently is, it would require minecraft to be located on your desktop if your batch file is on your desktop, if minecraft.exe is in a folder named "Folder1" then you would type:
#echo off
Start "C:\Users\<your name>\Desktop\Folder1\minecraft.exe"
In order for this to work, your batch file will also need to be located in Folder1 as well. Obviously, instead of minecraft.exe, you would use the files that you are using.
As long as your batch file is in the same path as your files then it should work. I hope the example I used helped you.
You are using %drive% where you should use drive
and using drive where you should use %drive%

Resources