I'm writing a script where I will get a file name as a parameter. The file will be absolute. (Ideally the solution would support relative files as well, but I can live with absolute-only). It is unknown if the file already exist.
I want to get the Drive letter of the file.
Example:
myScript.bat C:\exampleFolder\somefile.txt D:\someOtherFolder\differentfile.txt
myScript.bat:
echo First argument: %1
echo Second argument: %2
REM Its the next line I have trouble with.
echo Drive letter of second argument: %MAGIC%2
expected output:
First argument: C:\exampleFolder\somefile.txt
Second argument: D:\someOtherFolder\differentfile.txt
Drive letter of second argument: D:
Context: I want to write a script that does some file copy. But the file may appear at the target destination only at once. As soon as its created it must already be complete. So I want to write a script that copies the file to TARGET_DRIVE\tmp
and then move it to the destination.
I thought about passing the Drive letter as a third argument. But that seems cumbersome.
Best Answer
I want to get the Drive letter of the file in the 2nd argument
Use
%~d2
.myScript.bat:
Example Output:
Parameter Extensions
Source parameters
Further Reading