copy command in batch file is not getting executed

2019-09-20 11:23发布

i am trying to execute a command xcopy path1 path2 /Y /C and it is getting executed successfully when i tried from command line and also when i copied it to a batch file and double clicking on that batch file.

But it is not getting executed when i cal this batch file from another fail.

Could anyone please help here?

Not working case:

C:\abcd>cmd.exe /C "xcopy "C:\folder1\itsme.bat" "Y:\" /C /Y /Z" 
0 File(s) copied

Working case:

C:\abcd>cmd.exe /C "xcopy "C:\folder1\itsme.bat" "Y:\" /C /Y /Z"
C:\abcd\itsme.bat  
1 File(s) copied

Extra Info:

Runme.bat:

call C:\folder1\copy.bat
call C:\folder1\clean.bat

copy.bat:

@echo off
xcopy "C:\folder1\runrun.bat" "Z:\" /C /Y /Z /Q
xcopy "C:\folder1\runrun.bat" "Y:\" /C /Y /Z /Q
xcopy "C:\folder1\runrun.bat" "X:\" /C /Y /Z /Q

Here, If I double click on Runme.bat, copy.bat is getting executed and copying all the files. 1 File(s) copied 1 File(s) copied 1 File(s) copied

But issue is, it is not copying anything when i try to run the same batch file from windows scheduler. Output: 0 File(s) copied 0 File(s) copied 0 File(s) copied

looks like issue is only with copy command in the second batch file, which will return output. But all the commands in the other batch file clean.bat (which i am calling from the first batch file) are getting executed without any issues. second batch file has simple echo commands, so that is why it is working fine.

3条回答
爷、活的狠高调
2楼-- · 2019-09-20 11:28

The command cmd is for running a new instance of the command line interpreter and need to be used usually only for opening a command prompt window. Execute in a command prompt window cmd /? to get help about this command.

The command cmd does not need to be used usually if a command prompt window is opened already and a command is entered. A batch file is interpreted/processed by cmd and therefore it usually does not make sense to use cmd in a batch file.

So use only xcopy "C:\folder1\itsme.bat" "Y:\" /C /Y /Z in already opened command prompt window and in the batch file.

To process another batch file like batch file 2 from within a batch file like batch file 1 and continue processing of batch file 1 after processing of batch file 2 finished, use in batch file 1 the command call for calling batch file 2 like a subroutine.

Example for batch file 1:

@echo off
echo This is batch 1 calling now batch 2 and is waiting until it finished.
call "batch file 2.bat"
echo Batch 1 continues.

Example for batch file 2:

echo This is batch 2 running XCOPY.
xcopy "C:\folder1\itsme.bat" "Y:\" /C /Y /Z
echo XCOPY finished, batch 2 terminates.

Run batch file 1 and you get the output:

This is batch 1 calling now batch 2 and is waiting until it finished.
This is batch 2 running XCOPY.
XCOPY finished, batch 2 terminates.
Batch 1 continues.

Remove command call in batch file 1, execute it again and look what you get now.

This time without call in batch file 1 the processing of batch file 1 continues on batch file 2 without coming back to batch file 1 on reaching end of batch file 2 processing.

查看更多
在下西门庆
3楼-- · 2019-09-20 11:45

Use xcopy with 'START' command. For example, c:>> START xcopy "C:\folder1\itsme.bat" Y:\ /C /Y /Z

As Mofi pointed out, you can remove 'CMD'

查看更多
太酷不给撩
4楼-- · 2019-09-20 11:47

From your extra information it may be that x: y: z: are network drives and task scheduler doesn't have network privileges under the system account.

Also, change the name of copy.bat because copy is a system command and using system command names will bite you one day, if not today.

查看更多
登录 后发表回答