1.

Solve : Get a batch script to detect the drive letter it is running from??

Answer»

I am working on a batch script that I will be using to keep a folder updated across four computers. This script will be located on a memory stick, and it's function will be to copy modified files from the memory stick to the computer, and to copy modified files from the computer to the memory stick, and that is where I run into issues - At the moment, I have to go and modify the TARGET letter for the USB memory stick each time I move it to a different computer.

What I WOULD like is if someone could help me, by telling me how to make a batch script detect the drive letter.

I am aware of UTILITIES like dropbox, which would do what I ask - keeping things synchronized across four computers, but in this situations, that is not an option.Something like (you can just copy and paste into a DOS prompt - the first line will create the file with the rest of the lines in it, control-Z to save/exit):


copy con: show-current-drive.bat
@echo off
echo.
echo Starting Copies %date% %time%
echo.
for %%F in (%0) do (
echo Current drive is %%~dF
xcopy %%~dF\batchfiles\* G:\batchfiles\* /d /v /-y
xcopy G:\batchfiles\* %%~dF\batchfiles\* /d /v /-y
)
echo.
echo Copies complete %date% %time%
echo.
exit /b

control-Z

The %0 translates to the current FILENAME being executed and the for-loop further parses the name to find the drive letter. %%F (CASE sensitive) is the file name coming out of (into?) the loop and the small d says you just want the drive letter. And as a "bonus" the /d on the XCOPY will only try to update new or newer files - if they have the same time-stamp, it ignores them. The /-y allows YOU to choose when to over-write, but if you want to use / y, it will over-write w/o asking.

I do this a lot, too, but from network drives, etc. I also do file-compares in case I've modified from more than one location.

DaveHey, thanks. That should do the trickYou can use the ~ tilde variable modifiers on the parameters %0 and %1 to %9, as well as the FOR metavariables, so that the drive letter (including colon) on which a batch is running from is simply %~d0 and %~d0\ is the root folder of that drive, with no need for a FOR loop. The other ones - p,n,x,t,z etc all work too.

Yeah, I knew that - I just forget about it sometimes - and I'd never used it on %0...



Discussion

No Comment Found