The string passed as first argument to the batch file referenced with %1
is most important here on this issue. Unfortunately this string was not posted.
I suggest to use this batch file:
@echo off
if "%~1" == "" goto :EOF
%SystemRoot%\System32\xcopy.exe *.txt "%~1Folder\" /I /C /K /Q /R /Y >nul
%SystemRoot%\System32\xcopy.exe "%~1.java" "%~1Folder\" /C /K /Q /R /Y >nul
pushd "%~1Folder"
javac.exe -encoding utf8 "%~1.java"
java.exe "%~1"
popd
rem rd /S /Q "%~1Folder" & rem Is commented out while I'm debugging.
On second line it is checked if the batch file is called at all with an argument string and batch processing is exiting if this is not the case. For details see answer on: Where does GOTO :EOF return to?
The batch file above references the first argument string with perhaps existing surrounding double quotes always removed as the argument string is concatenated with other strings to a new argument string for commands and executables. A file/folder name without or with path containing a space or one of the characters &()[]{}^=;!'+,`~
requires the usage of double quotes around the complete argument string.
It is advisable to specify in a batch file executables always with file extension and if path to executable is well known also with full path because that makes the batch file independent on current values of the environment variables PATH
and PATHEXT
. That is the reason for specifying xcopy
twice with full path and with file extension. javac
and java
are specified just with file extension because their storage location on your Windows computer is not known by me. Therefore Windows command interpreter has to find those two executables with usage of environment variable PATH
.
XCOPY can create the entire directory structure to specified destination folder if the destination folder does not already exist. Therefore it is not necessary to explicitly create the destination folder first.
XCOPY can be used to copy a single file, multiple files or an entire directory structure to a single file, multiple files or a directory. The challenge for XCOPY is finding out for destination if the user specified a file name or a directory name.
The destination is interpreted as file name pattern if there is a ?
or *
after last backslash in destination/target string, for example for using xcopy *.txt "%TEMP%\*.tmp"
.
The destination is interpreted as directory name if the target string ends with a backslash.
But if the target string does not contain ?
or *
(after last backslash) and also does not end with a backslash, it is unclear for XCOPY if the target string specifies a directory or a file.
On copying multiple files, i.e. using ?
or *
in source string, the option /I
tells XCOPY that the target is a directory name even if target string does not end with a backslash. For that reason the first XCOPY command line would work also with no backslash at end of target string %~1Folder\
.
But on second XCOPY command line on which a single file is copied, the usage of option /I
would be have no help as XCOPY would nevertheless prompt the user if destination specifies a file or a directory. That's the reason for the backslash at end of destination folder path to avoid this prompt.
The command PUSHD pushes the path of current directory on stack and sets the specified directory as new current directory. This works even for a directory specified with a UNC path if command extensions are enabled as by default because in this case PUSHD dynamically assigns a drive letter to UNC folder path. The command POPD pops directory path from stack and sets this directory again as current directory. I can only suppose that this is the correct current directory management for the task.
I suggest to test this batch file with first line @echo off
changed to @echo ON
by running it from within a command prompt window with current directory set correct instead of double clicking on it to see what Windows command interpreter really executes after preprocessing each command line in the batch file and the perhaps output error messages depending on first argument string.
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.
call /?
... explains %~1
.
cmd /?
... explains when a file/directory name or any other argument string must be enclosed in double quotes.
echo /?
goto /?
if /?
popd /?
pushd /?
rd /?
rem /?
xcopy /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of >nul
.