If I am understanding your question correctly you want to have the .bat on the USB, plug it in, run it, and copy the files from a directory to the USB. In that case the simple script below will work. Assuming the path you choose on the client is static and does not vary, e.g. %userprofile%\desktop\
or %userprofile%\documents\
.
@echo off
REM This .bat file will be on the USB drive.
set "usb=%~dp0"
set "new_path=%usb%%computername%\%username%"
if not exist "%new_path%" mkdir "%new_path%"
xcopy "%userprofile%\main\folder\files\*" "%new_path%\" /Y
if errorlevel 1 (
echo ERROR OCCURRED - %ERRORLEVEL%
pause
exit /b
)
echo Successfully copied files to "%new_path%"
timeout /t 3
exit /b
EXPLANATION
First we make a directory on the flash drive, if it doesn't already exist, so all the files are neat and organized on the USB.
if not exist
checks for the directory. mkdir
creates the destination directory. Pretty obvious but none-the-less.
%~dp0
defines the working directory where the .bat file is located. For more information Here is a great post.
%userprofile%
environment variable is by default equal to the directory C:\users\%username%\
, and %computername%
expands to the computer's name.
xcopy
takes the source directory and copies to our destination we created prior. /Y
forces the copy and does not prompt to overwrite files.
*
is a wildcard. Here is a good site for that and also everything in this script. Note that a wildcard can only be used at the end of a directory. Something like C:\users\*\desktop\*\files\*
will not resolve. For something like that you would need to use for /D
.
Lastly I always like to check for errors, and see if it was successful. If not we pause
to make sure we see the error, or we put in a timeout /t seconds
to see a success.
EDIT
Set variables for paths to account for spaces in user names, and also fixed a couple other errors in the original script that were brought to my attention.