[Undocumented] cmd.exe and XCOPY.EXE

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

[Undocumented] cmd.exe and XCOPY.EXE

#1 Post by carlos » 24 May 2014 04:58

Hello. I found a undocumented compatibility feature of cmd.exe

On msdos when you use xcopy and the destination file exist it ask for overwrite or not. Edit: penpen says that in old msdos xcopy overwrite without ask.
On windows nt days xcopy by default prompt for overwrite or not, unless you specify the /Y option in the command line or in the COPYCMD variable. But if cmd.exe found a XCOPY.EXE (uppercase) automatically create or append the option /Y to the COPYCMD variable. Summary:

if you run xcopy and cmd.exe found this:

xcopy.exe : Normal feature. If options /Y is not specified on command line or COPYCMD variable, ask for overwrite or not.
XCOPY.EXE: Compatibility feature. Append or create the COPYCMD with /Y, overwrite silencely if you not specify /-Y on command line.

Look this example:

Code: Select all

@echo off
rem When cmd.exe call xcopy and the located executable have a uppercase filename (XCOPY.EXE)
rem momentaneously it create or append the COPYCMD enviroment variable with this:" /Y" (without quotes)
rem then when xcopy.exe is executed it will consider the option /Y present
rem and a overwriting of files without ask will do.
rem after the execution of xcopy.exe cmd will restore the previous content of the COPYCMD variable.
rem Feature only on batch script mode, not interactive mode.

echo hello>f.txt
echo bye>g.txt

echo Normal feature. Please reply No
xcopy f.txt g.txt

echo Content of g.txt:
type g.txt

pause
echo Undocumented compatibility feature

rem copy xcopy.exe as uppercase: XCOPY.EXE
copy %windir%\system32\xcopy.exe XCOPY.EXE >Nul
xcopy f.txt g.txt

echo Content of g.txt:
type g.txt

del XCOPY.EXE

pause

Last edited by carlos on 26 May 2014 12:38, edited 4 times in total.

Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

Re: [Undocumented] Obscure hidden feature of cmd.exe xcopy

#2 Post by Dragokas » 24 May 2014 05:44

Hi, Carlos !

Thanks for the feature.
I confirm it also on xp x 32 and win7 x64.

Interactively it's working when you call a separate process:

Code: Select all

cmd /c xcopy f.txt g.txt

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: [Undocumented] Obscure hidden feature of cmd.exe xcopy

#3 Post by penpen » 24 May 2014 06:53

@carlos:
MS-DOS 6.0 - 6.22 doesn't ask by default if files should be overwritten.
These are the results under MS-DOS 6.0 - 6.22 (only F/D is aksed for):

Code: Select all

C:\>test.bat
Normal feature. Please reply No
Does G.TXT specify a file name
or directory name on the target
(F = file, D = directory)?f
Reading source file(s)...
F.TXT
        1 File(s) copied
Content of g.txt:
hello
Press any key to continue . . .

Obscure feature.
Does G.TXT specify a file name
or directory name on the target
(F = file, D = directory)?f
Reading source file(s)...
F.TXT
        1 File(s) copied
Content of g.txt:
hello
Press any key to continue . . .

penpen

Edits:
Changed the upper comment as a result of my misreading.
Removed the wrong info at the end, i tricked out myself... .

Post Reply