/build/static/layout/Breadcrumb_cap_w.png

Implementing %~dp0 to my Robocopy batch script

Hello,


Everytime a computer gets re-imaged my student workers rely on using Windows Easy Transfer which I am totally against. To be honest I believe Windows Easy Transfer does a sloopy job on how it backs up and how it creates all of these random temp folders under the user's profile after being restored.

So I created the following script for them to run in a thumb drive or external HDD:

@echo off

REM ***** This section is used to set the variables for the copy commands *****

set /p computername= Enter the Remote Computer Name:
set /p euid= Enter the EUID to be Backed Up:
set day=%date:~4,2%
set month=%date:~7,2%
set year=%date:~10,4%


robocopy "\\%computername%\c$\users\%euid%\desktop" "e:\backup\%euid%\%day%-%month%-%year%\desktop" /e /copyall /zb /r:2 /w:15
robocopy "\\%computername%\c$\users\%euid%\documents" "e:\backup\%euid%\%day%-%month%-%year%\documents" /e /copyall /zb /r:2 /w:15
robocopy "\\%computername%\c$\users\%euid%\favorites" "e:\backup\%euid%\%day%-%month%-%year%\favorites" /e /copyall /zb /r:2 /w:15
robocopy "\\%computername%\c$\users\%euid%\appdata\local\mozilla\firefox\profiles" "e:\backup\%euid%\%day%-%month%-%year%\profiles" /e /copyall /zb /r:2 /w:15
robocopy "\\%computername%\c$\users\%euid%\appdata\local\microsoft\outlook\roamcache" "e:\backup\%euid%\%day%-%month%-%year%\roamcache" /e /copyall /zb /r:2 /w:15
robocopy "\\%computername%\c$\users\%euid%\appdata\roaming\microsoft\signatures" "e:\backup\%euid%\%day%-%month%-%year%\signatures" /e /copyall /zb /r:2 /w:15
robocopy "\\%computername%\c$\users\%euid%\appdata\roaming\microsoft\templates" "e:\backup\%euid%\%day%-%month%-%year%\signatures" /e /copyall /zb /r:2 /w:15

It works perfectly fine but the only downfall is that the external drive or USB needs to be mounted as drive E: and I was wondering if there is a way to launch this script not mattering what letter drive it is. Because there are times when E: is already taken in a user's PC and it automatically changes the letter drive. Someone told me to use %~dp0 but I'm not familiar on how to implement that into this scrip. Any assistance would be great!


1 Comment   [ + ] Show comment
  • As a lot of the more advanced Windows users' know the expression %~dp0% in a batch file will be evaluated to the batch file's directory including the trailing backslash.

    If I have a batch file in D:\ containing echo %~dp0%, that line will behave as expected and output D:\...I use gs richcopy 360 enterprise for these type of work. - georgeeerenee 6 years ago

Answers (2)

Posted by: dugullett 10 years ago
Red Belt
2

If they are running the script from the external then replace e:\ with %~dp0.

It may make more sense if you create a small bat file with something like this.

@echo off

echo %~dp0

pause

This should return the directory where the .bat is saved.

Posted by: BHC-Austin 10 years ago
4th Degree Black Belt
2

In a batch file, %0-%9 refer to the parameters passed to that batch file. %0 being the batch file itself, then %1-%9 being any parameters afterwards.

When using %0, you can add modifiers, such as ~d or ~p, or you can even stack them (as in the case of ~dp). An explanation of all the modifiers can be found by looking at help for the FOR command. At a command prompt just type:

for /?

Towards the end you will get the following (substitue I for your parameter number, e.g. 0):

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string The modifiers can be combined to get compound results:     %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line In the above examples %I and PATH can be replaced by other valid
values.  The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.

So, in the case of %~dp0, what you're saying is, expand the drive letter and path of the currently executing batch file. Now in your case, you may just want to use %~d0, to get just the drive letter, instead of the full path (if the batch file isn't in the root of the external media, that is)


Comments:
  • Thank you all for your help. The script now works great with out the need of setting the drive letter to E: - TheMessican 10 years ago

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

View more:

Share

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ