Where is the error in the. bat file?

Task :
To write a batch file that will copy from the current directory all
files with the file extension .txt except one file specified as the first
a command-line parameter, the directory that is specified by the second argument.
To overwrite only those files that are newer than the same name in the directory
the receiver. If there is a copy error, give message and abort execution of the file.
A solution was found:
@Echo Off
If '%2==' GoTo :Eof
Echo %1>~Xcopy.Tmp
xcopy *.txt %2\ /D /Y /Exclude:~Xcopy.Tmp >Nul 1>Nul 2>Nul
If Not %ErrorLevel%==0 (
 Echo Error while copying
Pause
 GoTo :Eof 
)
del ~Xcopy.Tmp

But there is a problem, the files are copied, but copied also the file specified in the first parameter, what is the error and how to fix it?
March 23rd 20 at 18:57
2 answers
March 23rd 20 at 18:59
Solution
When you call test.cmd "zhzhzhzh цццц.txt" C:\Temp\-2 in the file ~Xcopy.Tmp gets the %1 with double quotes, but for xcopy you need without the quotation marks.
@Echo Off
If '%2==' GoTo :Eof
using the setlocal DisableDelayedExpansion
set "file=%~1"
using the setlocal EnableDelayedExpansion
echo !file!>~Xcopy.Tmp
xcopy *.txt %2\ /D /Y /Exclude:~Xcopy.Tmp
If Not %ErrorLevel%==0 (
 Echo Error while copying
Pause
 GoTo :Eof 
)
del ~Xcopy.Tmp


Addition:
The above remove the quotes from the input parameter universal. And for files there is a more interesting way:DIR /B %1 > ~Xcopy.Tmp. Which allows you to use support characters (* and ?).

It
Do not delete the file of the exception (he still is overwritten) and to verify correctness.
And for him to try an absolute path %CD%\~Xcopy.Tmp.
An absolute path is used - did not help.File exceptions checked - all right. - Vergie.Bashiri commented on March 23rd 20 at 19:02
@Vergie.BashiriIn cmd there are no errors. Errors are possible in its invocation. - boy commented on March 23rd 20 at 19:05
March 23rd 20 at 19:01
Overwrite all files - wrong.
EXCLUDE accepts a file from the file list. Have obtained or display the name in the file and use it.
Or you need to go through all the files before copying to check the file name
for %%f in (path\*.txt) do (
If not %%f==%1 xcopy %%f %2\ /D /Y
If Not %ErrorLevel%==0 (
Echo Error while copying
Pause
GoTo :Eof
)
Maybe I'm a fool, but I have not worked( - Vergie.Bashiri commented on March 23rd 20 at 19:04

Find more questions by tags cmd/bat