Tolga Demir
Tolga Demir

Reputation: 161

Pause command if not succeeded instead of automatically closing

Lets say I want to copy a file or run a program, but the file or the location is not found. How can I let my computer pause the command window when these kind of problems occure? I made a batch file to copy files to another location, but also to run programs.

Any help will be appreciated.

Upvotes: 2

Views: 1702

Answers (2)

user1736703
user1736703

Reputation:

In your batch script, use xcopy to do the actual copying, rather than the copy command - xcopy, unlike copy, returns error codes depending on the result of the copy, which are documented here in the remarks section.

As Aleksandr mentioned, you can use the error codes as part of your script to pause on error. Assuming you would like to pause on all errors, you could do script this as below:

xcopy /HECY <source> <destination>
if %ERRORLEVEL% NEQ 0 pause

Obviously you'll need to replace and in the above with the locations you are copying from and to respectively.

The /HECY switches are just an example, but in this case could be used to instruct xcopy to copy hidden files, recurse directories, and automatically overwrite files in the destination if they exist. You can tweak these to your specific needs.

Upvotes: 4

Aleksandr Dubinsky
Aleksandr Dubinsky

Reputation: 23505

Check the error code and pause if it is non zero

Upvotes: 0

Related Questions