Marc
Marc

Reputation: 33

Copying files without knowing drive letter

I'm trying to make the setup of new PC's we get easier and started playing around with copying group policies. I put all the instructions (copying the folder and executing the files) in a .bat, but now I have the problem that it only works when the drive letter of the stick I'm using is E:\. Is there any way to dynamically adjust the drive letter and not hard code it?

It's just a few lines of code since most is happening inside the .exe

xcopy "E:\LGPO" "C:\LGPO\" /s/h/e/k/f/c
cd C:\LGPO
lgpo.exe /g C:\LGPO\backup

Upvotes: 3

Views: 3376

Answers (2)

user7818749
user7818749

Reputation:

Simply copy to the path from where the script is located. This case usb uaed (drive), Also use /d with the cd command, as you change drives.

xcopy "%~d0\LGPO" "C:\LGPO\" /s/h/e/k/f/c
cd /d C:\LGPO
lgpo.exe /g C:\LGPO\backup

cd /? specifies:

Use the /D switch to change current drive in addition to changing current directory for a drive.

The variable %~d0 will use drive of the path of the batch file, where %~dp0 is the drive\path.. So in this case, your batch file is inside the folder, we only need to use the drive %~d0

Upvotes: 1

jeb
jeb

Reputation: 82420

Remove the drive letter from your batch script.
When you start the batch from the USB-Stick, the current drive is the USB-Stick.

xcopy "\LGPO" "C:\LGPO\" /s/h/e/k/f/c
pushd C:\LGPO
lgpo.exe /g C:\LGPO\backup

If you really need the current drive letter, you can use %~d0.

Upvotes: 0

Related Questions