0

Is there anyway I can undo all the changes done by a batch file if it is terminated by a user? For example, if I am appending a text file and adding text, renaming files, deleting files by running a batch file... If a user terminates the batch file, is there a way to undo all changes done before it gets terminated by a user? OR anyway that I can not let the user terminate a batch file while its running?

Thanks

WinSupp
  • 361
  • 2
  • 6
  • 20
  • No. There is no "rollback" or "undo" from a batch file, and no way to prevent a batch file from being terminated. – Ken White Jun 11 '14 at 18:32
  • @KenWhite: There is a sort of workarount: a sandbox. Problem is: I don't know if there is any that can be controlled by batch. Doing would be: running the batch in a sandbox and when finished, export the changes to the system. When terminated unexpectedly, all changes whithin the sandbox would be lost. – Stephan Jun 11 '14 at 20:40
  • @Stephan: That's not undoing in a batch file; it's running your changes in a sandbox before applying, which is a different task. There is no "undo" from a batch file; there may be from some other solution, but that wasn't the question asked. (And asking that question would be a generic "tool recommendation" question and most likely off-topic here according to the [help/on-topic] guidelines.) – Ken White Jun 11 '14 at 20:42
  • @KenWhite therefore the "sort of workaround". The effect would be what he wants and maybe the "sandbox idea" gives him another view on his problem. I think most users are not aware that sandboxes even exist and at least "sandbox" is something he can google for. And I'm not recommending a software, but give a hint to another concept, that could possibly help because (as you already stated) there is no "batch solution" possible. – Stephan Jun 11 '14 at 20:54
  • You can backup the files at the start of the batch file and only delete them if the batch file finishes by putting the delete procedure at the bottom of the batch file. – foxidrive Jun 12 '14 at 02:51

2 Answers2

0

You can avoid that the normal user cancel a Batch file with Ctrl-C this way:

@echo off
setlocal

if "%~1" equ "NonCancelable" goto NonCancelable
echo Original!
start "" /B "%~F0" NonCancelable
echo Terminating original...
exit

:NonCancelable
echo I am non cancelable!
echo/
set "var="
set /P "var=Try to cancel me! (enter Exit to end): "
if /I "%var%" neq "exit" goto :NonCancelable
echo Terminating non cancelable...

Of course, the user can click on the window red cross to close the window...

Aacini
  • 65,180
  • 12
  • 72
  • 108
0

Substantially depends on how sophisticated your users are and to which facilities they have access.

You could for instance create an invisible batch which the user-batch executes. Many examples on SO of how to do this.

Or perhaps you could create new files within the batch, then replace the existing files with the new ones (or even create a replacement directory with a completely new fileset) as the very last operation within your batch.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thanks all for the replies.. I guess the users would be sophisticated and would have good technical knowledge but I just wanted to make it fool proof.. Thanks again – WinSupp Jun 12 '14 at 14:14