Getting Drive Letter using the Drives Property Name - batch-file

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.

Related

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

batch copy folder located by guid

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

How do I change the directory that a batch file will output text to?

I am trying to write a log that a user will create in a batch file, as a small little game, but I can't seem to get the correct directory of the file down. I want to write to this directory:
BatchfileandFolder\subfolder1\subfolder2\ThedataisHere.txt
This is the code I have (I really don't know what I am doing)
Echo Write a piece of text here
set /p UserData=
>>\subfolder1\subfolder2\"ThedataisHere.txt" echo %UserData%
It is essential trying to dig deeper into the directory, but I don't know the exact command, and the help menu for CD, and PATH on the cmd.exe promt don't really help me that much.
Thank you for real human contact, speaking real English
#ECHO OFF
SETLOCAL
SET "logfile=u:\sub folder1\sub folder2\ThedataisHere.txt"
FOR /f "delims=" %%a IN ("%logfile%") DO MD "%%~dpa"
Echo Write a piece of text here
set /p UserData=
>>"%logfile%" echo %UserData%
GOTO :EOF
This method uses a variable logfile so that you aren't forever typing out the name (and avoid the pain if you want to change the names or directories, and the method allows you yo use multiple logfiles easily if you want)
I've deliberately used spaces in directory names to prove the method. The directory gets created immediately after the logfile name is set
append 2>nul to the md instruction line to suppress the directory already exists message
From there, simply use >>"%logfile%" to create the log. The quotes are not required if the filename doesn't contain separators like spaces.
Note that if the first character of the directory specified is \ then the directory is relative to the root, but if it is not then the directory is relative to the current directory on the destination drive. u: is a drive specifier, not a directoryname; I use u: as my test drive. Your choice is up to you.
If the directory structure already exists, it's pretty simple:
#echo off
set /p UserData=Write some text here:
#echo %UserData% >> "subfolder1\subfolder2\TheDataIsHere.txt"
If it doesn't exist already, you have to create it first (tested on Win7 64 bit):
#echo off
if not exist "subfolder1" md "subfolder1"
if not exist "subfolder1\subfolder2" md "subfolder1\subfolder2"
set /p UserData=Write some text here:
#echo %UserData% >> "subfolder1\subfolder2\TheDataIsHere.txt"
Although your description is ample, some important points are missing. It is obvious that you know "How to change the directory that a batch file will output text to", because you use the >> \subfolder\... notation, so this is not your problem. I can only guess that you want to know "How to get the directory where a batch file is located", so you can write to a log file placed two levels below that directory. If this is your problem, then you may use the %~P0 notation, that represent the path of the batch file; that is:
>> "%~P0subfolder1\subfolder2\ThedataisHere.txt" echo %UserData%
Note that the value returned by %~P0 ends in a backslash, so %~P0 must not be separated by an additional backslash from subfolder1; also note that the quotes must enclose the whole path of the file.
If this is not what you want, please carefully describe your real problem. Anyway, try to be clearer in future questions.

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%

Determining Folder Attribute in a Batch File

I'm writing a batch file in which I need to determine whether or not a particular folder on my hard drive is a Hidden folder. The batch file will then proceed depending on the answer.
I've studied the responses to some similar questions here but I'm still not getting it. Any help with this part of my batch file appreciated.
Note: It's the same folder all the time but the System attribute changes from time to time.
Thanks!
The output of attrib command is in the format 'A_SHR_I', so we will test if the fifth character in output is an H to determine if the folder is hidden
attrib "c:\Myfolder" | findstr /r /c:". .H" > nul
if errorlevel 1 (
echo Folder is NOT HIDDEN
) else (
echo Folder IS HIDDEN
)
This is the behaviour in a Windows 7 Spanish installation. Try on your systems and adapt as needed.

Resources