Bear with me here; I'm super new to coding something like this, and I think I've found a simple way to do it, but I'm running into problems. I also couldn't really find an answer from Googling or looking around here, but if it's already been answered, I apologize!
I'm running the following command in a .bat file:
xcopy D:\SHARENAME \\NAS-IP-ADDRESS\SHARENAME /s /e /d
When I run it once as a .bat file, it does its copy operations, and then closes the terminal window, signaling completion. I can verify that a copy operation occurred, since new files do show up in the Backup directory.
However, once I set this .bat file as a Windows Scheduled Task, it never stops running. The copy still occurs one time, but the task does not end. I want it to run every day at 3AM, which is fine, but it never gets the chance to run after its first time, since it never actually stops. Am I missing something to close out this .bat file in order to stop it properly?
The scheduled task does not end because of XCOPY prompts on copying a file existing already in destination directory if it should be overwritten or not. This prompt is not answered by anybody and so XCOPY waits forever. So cmd.exe never ends the batch file execution and for that reason scheduled task also never ends.
The solution is using the command line:
%SystemRoot%\System32\xcopy.exe "D:\SHARENAME" "\\NAS-IP-ADDRESS\SHARENAME\" /C /E /H /K /Q /R /Y
Most important to fix this issue is the option /Y which avoids the overwrite prompt.
Microsoft's xcopy documentation contains currently the information:
By default, you are prompted to overwrite, unless you run xcopy from within a batch script.
This is a wrong information. XCOPY is an executable in system folder of Windows and does not know if being executed by cmd.exe from within a command prompt window or from within a batch script. /Y must be specified on command line or there is an environment variable COPYCMD containing /Y in value to avoid the overwrite prompt. /Y must not be used on command line on using copy being an internal command of cmd.exe on using COPY in a batch script.
The help of XCOPY output on running in a command prompt window xcopy /? explains all other options used above. At bottom of output help can be read:
The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.
The help of command COPY output on running copy /? ends with:
The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line. Default is
to prompt on overwrites unless COPY command is being executed from
within a batch script.
The additional third sentence makes the difference regarding to /Y between XCOPY and COPY on usage from within a batch script.
The help of XCOPY contains also the explanation for /S and /E.
/S Copies directories and subdirectories except empty ones.
/E Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.
Same as /S /E. is a confusing description. Many beginners think it is necessary to specify /S and /E to get entire directory structure with empty directories copied. But this is not true. It is meant that just /E is enough to copy an entire directory structure with including empty directories and /S /E is interpreted like just /E. It is superfluous to specify /S (copy without empty directories) in addition to /E (copy with empty directories) on XCOPY command line.
I recommend reading the SS64 - XCOPY documentation which is currently better than Microsoft's documentation.
Very important on copying one or more files and directories with XCOPY into a specified destination directory is specifying the destination path with a backslash at end. This makes it clear for XCOPY that the destination string specifies a directory and not a file. Otherwise it is necessary to specify additionally /I on copying multiple files or an entire directory tree to inform XCOPY that the destination argument should be interpreted as folder path. But /I does not avoid a prompt if a single file is copied with XCOPY and the destination argument does not end with a backslash, see BATCH file asks for file or folder for details. Therefore it is highly recommended to specify destination folder path always with a backslash at end on copying one or more files to a folder using XCOPY.
Related
I have a simple copy from-to script for one of my friends who is missing a file 20 km from my desk.
When testing the script out I am prompted if my file shapes.atc is a file or a folder.
I can tell you that its a file. How can I automatically copy it with my friend needs to just double click the batch to get the file copying job done.
xcopy /s/y J:\"My Name"\"FILES IN TRANSIT"\JOHN20101126\"Missing file"\Shapes.atc C:\"Documents and Settings"\"His name"\"Application Data"\Autodesk\"AutoCAD 2010"\"R18.0"\enu\Support\Shapes.atc
A seemingly undocumented trick is to put a * at the end of the destination - then xcopy will copy as a file, like so
xcopy c:\source\file.txt c:\destination\newfile.txt*
The echo f | xcopy ... trick does not work on localized versions of Windows, where the prompt is different.
Actually xcopy does not ask you if the original file exists, but if you want to put it in a new folder named Shapes.atc, or in the folder Support (which is what you want.
To prevent xcopy from asking this, just tell him the destination folder, so there's no ambiguity:
xcopy /s/y "J:\Old path\Shapes.atc" "C:\Documents and Settings\his name\Support"
If you want to change the filename in destination just use copy (which is more adapted than xcopy when copying files):
copy /y "J:\Old path\Shapes.atc" "C:\Documents and Settings\his name\Support\Shapes-new.atc
The real trick is: Use a Backslash at the end of the target path where to copy the file. The /Y is for overwriting existing files, if you want no warnings.
Example:
xcopy /Y "C:\file\from\here.txt" "C:\file\to\here\"
echo f | xcopy /s/y J:\"My Name"\"FILES IN TRANSIT"\JOHN20101126\"Missing file"\Shapes.atc C:\"Documents and Settings"\"His name"\"Application Data"\Autodesk\"AutoCAD 2010"\"R18.0"\enu\Support\Shapes.atc
Referencing XCopy Force File
For forcing files, we could use pipeline "echo F |":
C:\Trash>xcopy 23.txt 24.txt
Does 24.txt specify a file name
or directory name on the target
(F = file, D = directory)?
C:\Trash>echo F | xcopy 23.txt 24.txt
Does 24.txt specify a file name
or directory name on the target
(F = file, D = directory)? F
C:23.txt
1 File(s) copied
For forcing a folder, we could use /i parameter for xcopy or using a backslash() at the end of the destination folder.
The /i switch might be what your after.
From xcopy /?
/I If destination does not exist and copying more than one file,
assumes that destination must be a directory.
Well, for the task as asked by just me the perhaps best solution would be the following command according to the incomplete advice of Andy Morris:
xcopy "J:\My Name\FILES IN TRANSIT\JOHN20101126\Missing file\Shapes.atc" "C:\Documents and Settings\His name\Application Data\Autodesk\AutoCAD 2010\R18.0\enu\Support\" /Q /R /S /Y
This works for this simple file copying task because of
specifying just the destination directory instead of destination file and
ending destination directory with a backslash which is very important as otherwise XCOPY would even with /I prompt for file or directory on copying just a single file.
The other parameters not related to the question are:
/Q ... quiet
/Y ... yes (OS language independent) on overwrite existing file
/R ... overwrite also read-only, hidden and system file
/S ... from specified directory and all subdirectories.
Well, I don't know if /S is really needed here because it is unclear if just J:\My Name\FILES IN TRANSIT\JOHN20101126\Missing file\Shapes.atc should be copied or all Shapes.atc found anywhere in directory tree of J:\My Name\FILES IN TRANSIT\JOHN20101126\Missing file.
The explanation for the parameters can be read by opening a command prompt window and running from within this window xcopy /? to get output the help for XCOPY.
But none of the provided solutions worked for a file copying task on which a single file should be copied into same directory as source file, but with a different file name because of current date and time is inserted in file name before file extension.
The source file can have hidden or system attribute set which excludes the usage of COPY command.
The batch file for creating the time stamped file should work also on Windows XP which excludes ROBOCOPY because by default not available on Windows XP.
The batch file should work with any file including non typical files like .gitconfig or .htaccess which are files without a file extension starting with a point to hide them on *nix systems. Windows command processor interprets such files as files with no file name and having just a file extension because of the rule that everything after last point is the extension of the file and everything before last point is the file name.
For a complete task description and the final, fully commented solution see the post Create a backup copy of files in UltraEdit forum.
Patrick's, Interociter Operator's and CharlesB's solutions do not work because using /Y does not avoid the file or directory prompt if the destination file does not already exist.
Andy Morris' and grenix's solutions can't be used for the single file copying task as destination must be the name of destination file and not the name of destination directory. The destination directory is the same as the source directory, but name of destination file is different to name of source file.
DosMan's and Govert's solutions simply don't work for files starting with a point and not having a file extension.
For example the command
xcopy C:\Temp\.gitconfig C:\Temp\.gitconfig_2016-03-07_15-30-00* /C /H /K /Q /R /V /Y
results in following error message on execution:
English: Could not expand second file name so as to match first.
German: Zweiter Dateiname konnte nicht so erweitert werden, dass er zum ersten passt.
And finally Denis Ivin's solution has the restriction that the operating system language dependent character for an automatic answering of the file OR directory prompt must be known.
So I thought about methods to get F for File on English Windows or D for Datei on German Windows or ? for ... on ... Windows automatically.
And it is indeed possible to determine the language dependent character for an automatic answering of the prompt.
A hack is used to get the language dependent letter from prompt text without really copying any file.
Command XCOPY is used to start copying the batch file itself to folder for temporary files with file extension being TMP for destination file. This results in a prompt by XCOPY if there is not already a file with that name in temporary files folder which is very unlikely.
The handler of device NUL is used as an input handler for XCOPY resulting in breaking the copying process after the prompt was output by XCOPY two times.
This output is processed in a FOR loop which is exited on first line starting with an opening parenthesis. This is the line on which second character defines the letter to use for specifying that destination is a file.
Here is a batch file using XCOPY with the code to determine the required letter for an automatic answering of the file or directory prompt to create a time stamped copy of a single file in same directory as the source file even if source file is a hidden or system file and even if the source file starts with a point and does not have a file extension.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Batch file must be started or called with name of a single file.
if "%~1" == "" exit /B
for /F "delims=*?" %%I in ("#%~1#") do if not "%%I" == "#%~1#" exit /B
if not exist "%~1" exit /B
if exist "%~1\" exit /B
rem Determine the character needed for answering prompt of
rem XCOPY for destination being a file and not a directory.
del /F "%TEMP%\%~n0.tmp" 2>nul
for /F %%I in ('%SystemRoot%\System32\xcopy.exe "%~f0" "%TEMP%\%~n0.tmp" ^<nul') do (
set "PromptAnswer=%%I"
setlocal EnableDelayedExpansion
if "!PromptAnswer:~0,1!" == "(" set "PromptAnswer=!PromptAnswer:~1,1!" & goto CopyFile
endlocal
)
echo ERROR: Failed to determine letter for answering prompt of XCOPY.
exit /B
:CopyFile
endlocal & set "PromptAnswer=%PromptAnswer%"
rem This is a workaround for files starting with a point and having no
rem file extension like many hidden files on *nix copied to Windows.
if "%~n1" == "" (
set "FileNameWithPath=%~dpx1"
set "FileExtension="
) else (
set "FileNameWithPath=%~dpn1"
set "FileExtension=%~x1"
)
rem Get local date and time in region and language independent format YYYYMMDDHHmmss
rem and reformat the local date and time to format YYYY-MM-DD_HH-mm-ss.
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS get LocalDateTime /format:value') do set "LocalDateTime=%%I"
set "LocalDateTime=%LocalDateTime:~0,4%-%LocalDateTime:~4,2%-%LocalDateTime:~6,2%_%LocalDateTime:~8,2%-%LocalDateTime:~10,2%-%LocalDateTime:~12,2%"
rem Do the copy with showing what is copied and with printing success or
rem an error message if copying fails for example on sharing violation.
echo Copy "%~f1" to "%FileNameWithPath%_%LocalDateTime%%FileExtension%"
for /F %%I in ('echo %PromptAnswer% ^| %SystemRoot%\System32\xcopy.exe "%~f1" "%FileNameWithPath%_%LocalDateTime%%FileExtension%" /C /H /K /Q /R /V /Y') do set "FilesCopied=%%I"
if "%FilesCopied%" == "1" (echo Success) else echo ERROR: Copying failed, see error message above.
This batch code was tested on German Windows XP SP3 x86 and English Windows 7 SP1 x64.
See the post Create a backup copy of files in UltraEdit forum for a similar, fully commented batch file explaining all parts of the batch code.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
del /?
echo /?
exit /?
for /?
goto /?
if /?
rem /?
set /?
setlocal /?
wmic OS get /?
xcopy /?
Further the Microsoft article about Using command redirection operators should be read, too.
The trick of appending "*" can be made to work when the new extension is shorter. You need to pad the new extension with blanks, which can only be done by enclosing the destination file name in quotes. For example:
xcopy foo.shtml "foo.html *"
This will copy and rename without prompting.
"That's not a bug, it's a feature!" (I once saw a VW Beetle in the Microsoft parking lot with the vanity plate "FEATURE".) These semantics for rename go all the way back to when I wrote DOS v.1. Characters in the new name are substituted one by one for characters in the old name, unless a wildcard character (? or *) is present in the new name. Without adding the blank(s) to the new name, remaining characters are copied from the old name.
xcopy /s/y J:\"My Name"\"FILES IN TRANSIT"\JOHN20101126\"Missing file"\Shapes.atc C:\"Documents and Settings"\"His name"\"Application Data"\Autodesk\"AutoCAD 2010"\"R18.0"\enu\Support\*.*"
..should do it.
Good idea to do an:
IF NOT EXIST "C:\Documents and Settings\His name\Application Data\Autodesk\AutoCAD 2010\R18.0\enu\Support\Shapes.atc" ECHO/ && ECHO/ && ECHO * * * * * COPY FAILED - Call JustME at 555-555-1212 && ECHO/ && pause
(assuming you've done a rename of previous version to .old)
XCOPY /Z <----- restartable mode - good for large files.
The virtual parent trick
Assuming you have your source and destination file in
%SRC_FILENAME% and %DST_FILENAME%
you could use a 2 step method:
#REM on my win 7 system mkdir creates all parent directories also
mkdir "%DST_FILENAME%\.."
xcopy "%SRC_FILENAME% "%DST_FILENAME%\.."
this would be resolved to e.g
mkdir "c:\destination\b\c\file.txt\.."
#REM The special trick here is that mkdir can create the parent
#REM directory of a "virtual" directory (c:\destination\b\c\file.txt\) that
#REM doesn't even need to exist.
#REM So the directory "c:\destination\b\c" is created here.
#REM mkdir "c:\destination\b\c\dummystring\.." would have the same effect
xcopy "c:\source\b\c\file.txt" "c:\destination\b\c\file.txt\.."
#REM xcopy computes the real location of "c:\destination\b\c\file.txt\.."
#REM which is the now existing directory "c:\destination\b\c"
#REM (the parent directory of the "virtual" directory c:\destination\b\c\file.txt\).
I came to the idea when I stumbled over some really wild ../..-constructs in the command lines generated from a build process.
I had exactly the same problem, where is wanted to copy a file into an external hard drive for backup purposes.
If I wanted to copy a complete folder, then COPY was quite happy to create the destination folder and populate it with all the files.
However, I wanted to copy a file once a day and add today's date to the file.
COPY was happy to copy the file and rename it in the new format, but only as long as the destination folder already existed.
my copy command looked like this:
COPY C:\SRCFOLDER\MYFILE.doc D:\DESTFOLDER\MYFILE_YYYYMMDD.doc
Like you, I looked around for alternative switches or other copy type commands, but nothing really worked like I wanted it to.
Then I thought about splitting out the two different requirements by simply adding a make directory ( MD or MKDIR ) command before the copy command.
So now i have
MKDIR D:\DESTFOLDER
COPY C:\SRCFOLDER\MYFILE.doc D:\DESTFOLDER\MYFILE_YYYYMMDD.doc
If the destination folder does NOT exist, then it creates it.
If the destination folder DOES exist, then it generates an error message.. BUT, this does not stop the batch file from continuing on to the copy command.
The error message says:
A subdirectory or file D:\DESTFOLDER already exists
As i said, the error message doesn't stop the batch file working and it is a really simple fix to the problem.
Hope that this helps.
I'm creating a batch file that deletes all Rar$DIa0.??? folders in the %TEMP% directory.
Is this possible, and how to do it?
The ??? is three randomized numbers. And that's where I have trouble with - deleting all folders that have Rar$DIa0. in the name.
for /d is designed for just this type of use. Something like this should work (remove one of the % if you're testing from the command line):
for /d %%i in ("%TEMP%\Rar$DIa0.???") do rd "%TEMP%\%%i"
The /d makes it work on directory names instead of file names.
If you want to make it easier on yourself, change to the %TEMP% folder first:
pushdir
cd /d %TEMP%
for /d %%i in ("Rar$DIa0.???") do rd "%%i"
The ??? makes it only act on folders that have three letters after a .. If your folders don't have just a three letter extension, change .??? to .*. If you've got a typo, and there is no actual . in the foldername, just remove it and use Rar$DIa0??? or Rar$DIa0*
You may want to test it first by changing rd to echo to make sure you get the folders you want before actually deleting them.
For more information about for (pun intended) type for /? from a command prompt.
The command line to use in a batch file for this task is:
#for /D %%I in ("%TEMP%\Rar$DIa0.*") do #rd /Q /S "%%I"
Command FOR with option /D searches in folder defined by environment variable TEMP for subfolders with folder name starting with Rar$DIa0. not having hidden or system attribute set.
The loop variable I holds for each found subfolder matching this folder pattern the name of found folder with full path without double quotes although the path to temp folder very often contains 1 or more spaces.
For that reason just the command RD with the parameters /Q for quiet execution and /S for deleting also all subfolders in the specified folder must be called with referencing the current value of loop variable I enclosed in double quotes.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
for /?
rd /?
By the way: WinRAR deletes the temporary folders usually automatically, except a file is opened from within an archive for viewing/modifying it in another application and WinRAR is closed before the other application is exited with the opened file. In this case WinRAR can't delete the temporary folder with temporarily extracted file because the file is still opened in another application. Of course also command RD can't delete the temporary folder if this folder is still the current directory of another application or a file in this folder is still opened by another application with a read/write access lock.
I want to use a batch file to synchronize 2 folders on Windows, and I'm using the xcopy command like this
xcopy /s /d <Original Folder> <Destination Folder>
It's working perfectly because I just want to overwrite only the modified ones, but I have 1 tiny problem, when I delete a file from the original folder, that file remains in the destination folder when i run the batch file, breaking the synching I want to achieve... I think this can't be done with xcopy, but I don't know which command I should use in case I delete a file in the original folder.
EDIT: This programs is for people who don't understant a lot of PC's so i don't want to install anything in their pc's. Just want a plain batch file to keep the 2 folders synchronized
SOLUTION: Thanks to Kuba Wyrostek I got to this command
robocopy <Original Folder> <Destination Folder> /e /purge
Thank you all for your time
xcopy has no such option. Your alternatives are:
rsync - http://rsync.samba.org
robocopy - http://technet.microsoft.com/en-us/library/cc733145.aspx
To refine your answer, you can simply do:
robocopy <Original Folder> <Destination Folder> /mir
/mir mirrors a directory tree (equivalent to /e plus /purge).
The XCOPY command for copying the updates from one folder to another is as follows:
C:\> XCOPY C:/SOURCE D:/DESTINATION /E /D /C /Y
The flags used in the above example have the following meanings:
/E causes all folders and sub-folders to be copied
/D causes a DATE comparison to be made, only copying items that are newer than the DESTINATION item. If the DESTINATION is older, or does not contain the file, then it will be copied.
/C tells XCOPY to continue if it encounters an error - typically errors occur with read-only files, or files that have protected permissions
/Y tells XCOPY to suppress prompting you for confirmation, which is necessary if you intend to create an automation script or scheduled task
I know this is old, but I have a question. Can this not be done with the correct xcopy commands.
Folder 1: c:\workingFolder\"contents here"
Folder 2: d:\workingFolderSync\"contents here"
Batch file below
xcopy "c:\workingFolder\*.*" "d:\workingFolderSync\" /i /s /d /y
xcopy "d:\workingFolderSync\*.*" "c:\workingFolder\" /i /s /d /y
pause
( I usually remove the pause after i test and know the batch works. )
But this will only copy contents from the folder to the other if its newer.
Then it looks in the destination folder and copies contents from destination back to source if it is newer. This reproduces what syncing does, except for the in real time monitoring of modified files. Creating a proper task schedule can actually implement running this command at specific times to almost perfectly reproduce syncing. You can also run this before modification on a machine and after modification on a machine. Even doing the same thing on a second machine if your using a network drive to sync.
Your method will have two main problems:
Files deleted from one folder will be reinstated by the copy left in the other folder the next time you run your xcopy
Renaming files can cause chaotic results.
I would like to know how to copy a folder to another folder in a batch file.
I would like stuff with the same name to be overwritten.
Thanks.
Unless I'm missing something, the easiest way is with xcopy (provided you actually mean .bat files on Windows):
xcopy <SOURCE> <DEST> /e /d /y /h /r /c
To explain those flags:
/e: Include directories and sub directories even if empty
/d: Only copy files which have changed on SOURCE more recently than DEST
/y: Suppress prompts
/h: Also copy hidden and system files
/r: Override read-only files (e.g. ignore Read Only flag)
/c: Continue even if there are errors
Source: My standard backup script, been running every day for 6 years.
or take a look at ROBOCOPY en.wikipedia.org/wiki/Robocopy
I have a strange problem with xcopy in Windows XP Professional. I don't know if its a stupid question as I am specifying only a file as the source, so should I even expect any other behavior ? This is it:
I am using xcopy <src> <dest> /s/y.
<src>=C:\sourcefolder\a\b\c\d\something.java and
<dest>=C:\destinationfolder.
Now xcopy copies the file but does not create the directory structure \a\b\c\d\ inside C:\destinationfolder .
what I want is C:\destinationfolder\a\b\c\d\something.java and
what I get is C:\destinationfolder\something.java
I have tried to run it in destination folder C:\destinationfolder by specifying a . for target folder
Tried it without any target in above
There is a script I have which calls xcopy iteratively so I am left with C:\destinationfolder\many java files without any directory structure.
A. Yes I have done xcopy /? to see all options
B. /T also does not create any empty directory structure
C. I can not go to source folder a\b\c\d\ and run xcopy . <dest>
UPDATE
I removed my previous answer on using ROBOCOPY. I believe the following will do what you want using XCOPY.
Assuming your folder structure is like this:
SOURCE = C:\MyJavaStuff\A\B\C\D\something.java
DEST = C:\MyDestination
Run XCOPY like this:
XCOPY C:\MyJavaStuff\something*.java C:\MyDestination /S /E
Note the * in something*.java.
The problem is that you are specifying which file to copy in the source. xcopy won't create the folder structure in this case. However, if you change your call to xcopy to
xcopy *.java C:\myfolder /s/y
it will copy the .java files and the folder structure as well. You need to specify a wildcard for this call to work as you want. If you want only to copy specific files, you will have to adjust the call to xopy, e.g.:
xcopy something.jav* C:\myfolder /s/y
Edit
You say that you get the list of files to copy from another command. If you can output this list of files in a text file, you could do the following:
FOR /F "tokens=* delims=," %F in (d:\test\list.txt) DO xcopy src\%~nxF* .\dest /S /Y
What this command does is read a text file ("d:\test\list.txt" in this case), read every line, and for each file, run xcopy, adding a wildcard at the end of the file name to make sure it creates the folder structure.
I'm assuming here that:
You can get the list of files in a text file, with only the file names (and optinally the paths)
You know the source folder ("C:\sourcefolder" in your example, the folder structure "a\b\c\d" does not need to be known) and can use it in the FOR command.
You can also use the following form:
FOR /F "tokens=* delims=," %F in ('cmd') DO xcopy src\%~nxF* .\dest /S /Y
where cmd needs to be replace with the command you use to generate your list of files to copy.
Note that if you use this FOR command in a batch file, you need to replace %F with %%F (and %~nxF* with %%~nxF*).
I had a look at the xcopy switches and you can copy the directory structure with /T, although that doesn't copy empty directories you can override this with /E. So your command would look like this:
xcopy C:\sourcefolder\a\b\c\d\something.java C:\destinationfolder /T /E /S /Y
Hope this helps!
In order to get C:\destinationfolder\a\b\c\d\something.java XCOPY needs to know how much of C:\sourcefolder\a\b\c\d\something.java to duplicate.
You can use:
C:
cd \sourcefolder
XCOPY something.java* C:\destinationfolder\ /S
Just be aware that this may have the side effect of also copying C:\sourcefolder\oops\something.java to C:\destinationfolder\oops\something.java as well as any other matches for something*.java under C:\sourcefolder\.
It seems to me that xcopy is typically used for copying directory trees, not single files (though it can work). And, xcopy will recreate the directory structure under the source folder in the target folder. If xcopy is given the /i switch, the target folder is assumed to be a directory. It will be created if it does not exist, even if there are multiple parents that need to be created.
You have C:\MyJavaStuff\A\B\C\D\something.java - that is your source. You want to end up with something.java not in C:\destinationfolder, but in C:\destinationfolder\A\B\C\D - so that is your target. You don't even have C:\destinationfolder. That is OK, with /i the entire path will be created.
xcopy /i c:\MyJavaStuff\A\B\C\D\something.java C:\destinationfolder\A\B\C\D
If something.java were the only file under C:\MyJavaStuff, you could also use
xcopy /sei c:\MyJavaStuff C:\destinationfolder
That would recreate the entire tree structure, copying your file. But if there are other files (and folders) under MyJavaStuff they would also be copied.
I have written a very similar batch file using xcopy. Perhaps what I did will help you.
This is the command I used:
xcopy "c:\Data Files\Dave's Data\*.*" "m:\Dave's Data" /R/D /E/H
In this case, Dave's Data on the source contains an entire directory tree containing at least 50,000 files & exceeding 75GB data. It runs perfectly on Windows XP
I found /T was unnecessary as the directory tree is copied. I also found /S was unnecessary as /E copied directories & sub-directories including empty ones. I included /R to copy & overwrite read only files on the destination. /H copied hidden directories. /D copied only newer files. I use this as a daily backup tool for my data.
The only problem I have is while this command will work on Windows 7 the first time, it will not work on subsequent runs when the destination directory tree exists. I suspect this is due to a privilege issue as the xcopy command will work on subsequent runs on Windows 7 within a cmd.exe window.