0

I'm trying to get a .bat script to copy files from a user's documents to a flash drive, however of course not all of us have our documents in our user directory - the following is what we have:

for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
IF EXIST %%a:\STUDYvault.id SET drive=%%a:\ && GOTO Meep:
)
xcopy "%USERPROFILE%\Documents\ExampleFolder" "%drive%Backup\ExampleFolder" /S /D /Y /I

Is there an argument or workaround we can do to replace "%USERPROFILE%\Documents" with say, %userdocumentsdir% or something? Can't find anything via google.

Thanks for your time!

eis
  • 51,991
  • 13
  • 150
  • 199
Julian White
  • 1,603
  • 3
  • 12
  • 12
  • just noticed (after answering) that this is probably a duplicate of [this question](http://stackoverflow.com/questions/3492920/is-there-a-system-defined-environment-variable-for-documents-directory) – eis Dec 02 '14 at 07:05

2 Answers2

1

There isn't such environment value, you'll have to grab it from registry.

You can do it like this:

FOR /F "tokens=3 " %%G IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') DO (SET userdocumentsdir=%%G)

echo %userdocumentsdir%

Note that the code is meant to run on .bat file.

Source: this discussion (that shouldn't really be in serverfault I guess)

Community
  • 1
  • 1
eis
  • 51,991
  • 13
  • 150
  • 199
  • 1
    You should allow for spaces in the returned path - either XP, customised or localised systems. `FOR /F "skip=4 tokens=2,*" %%G IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') DO (SET userdocumentsdir="%%H")` – Scott C Dec 01 '14 at 11:03
0

This code will do the trick.

@echo off
echo Tested and worrking in win XP, vista, 7, 8, 8.1 and 10
    for /f "skip=2 tokens=2*" %%c in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do @set "docs=%%d" && echo WIN XP - 10
    xcopy "%docs%\test.txt" "I:\" /f /y
    xcopy "%docs%\test folder" "I:\test folder\" /d /e /y
echo %docs%
pause
EXIT