-1

To be more specific, I would like to use a batch file to load "Program X". Then once the program is fully loaded and open on the screen, move a few files from "Folder A" to "Folder B". Then when Program X is manually exited, move the previously moved files back to Folder B. Here is what I tried, but didn't work. I didn't think it would.

start "" "C:\ProgramX.exe"
move /-y "C:\FolderA\File1.txt" "C:\FolderB\"
move /-y "C:\FolderA\File2.txt" "C:\FolderB\"

/wait "C:\ProgramX.exe"

move /-y "C:\FolderB\File1.txt" "C:\FolderA\"
move /-y "C:\FolderB\File2.txt" "C:\FolderA\"

Originally I made two separate batch files that simply move the files back and forth (I manually open and close the program) and it works, but I would like to have just one batch file that loads the program, then moves the files and then moves them back once I have exited the program.

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • Please not only say, that it does not work. Descripe what the code does and what not next time – xs6 Mar 03 '17 at 07:50
  • It successfully started the program, but moved the files before the program fully loaded...and then it promptly moved the files right back to the original folder and exited. I only know this because I inserted pauses between each command so I could see what was going on. – user3048136 Mar 03 '17 at 08:29
  • There's no way to wait for a program to have fully initialized and display its main UI in a generic way, regardless of programming language or platform. The solution is necessarily going to be application-specific. Likewise, there is no way to discern between manual and automated application shutdown. – IInspectable Mar 03 '17 at 09:53
  • @IInspectable: I don't agree. See [my answer](http://stackoverflow.com/a/42576985/778560). – Aacini Mar 04 '17 at 11:44
  • @Aacini: You can agree or disagree with opinions. I stated established facts. If you feel like disagreeing with facts, you need a very strong tool. As it stands, your proposed solution does not address the issue I outlined in my previous comment. There is no way to generically, at the system level know, when any given application's initialization has run to a particular point, where it is ready to respond to a specific change in global state. – IInspectable Mar 05 '17 at 14:09

3 Answers3

0

You can build a loop (goto) which looks if the programm is still running (if ..then) and you put the move back of the files after the loop.

Checkout this snippet:

tasklist /FI "IMAGENAME eq notepad.exe" /FO CSV > search.log

FOR /F %%A IN (search.log) DO IF %%~zA EQU 0 GOTO end

start notepad.exe

:end

del search.log

It checks if notepad.exe is running. If it is not running, it will start notepad.exe.

xs6
  • 860
  • 8
  • 18
  • What is the problem now? To wait some seconds, you can implement the code "ping -n 2 localhost > NUL". The "2" says, how long it has to wait. So choose a time, which is longer then the time needed for full load of the application – xs6 Mar 03 '17 at 12:25
  • @xs6 to sleep [better use `timeout 1 >nul`](http://stackoverflow.com/q/1672338/995714) – phuclv Mar 04 '17 at 03:25
0

I think this is the most efficient way:

@echo off

rem Start the program and wait until it ends before continue
(
start "process running" "arj.exe"
ping -n 2 localhost > NUL
move /-y "C:\FolderA\File1.txt" "C:\FolderB\" > NUL
move /-y "C:\FolderA\File2.txt" "C:\FolderB\" > NUL
) | set /P "="

move /-y "C:\FolderB\File1.txt" "C:\FolderA\"
move /-y "C:\FolderB\File2.txt" "C:\FolderA\"

In this method the waiting state is event driven (controlled by the OS), so it does not waste CPU time! Further details here.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • I wasn't able to get this to work. Am I missing something? – user3048136 Mar 03 '17 at 11:14
  • Please, provide a clear description of what the problem is. Each individual command should run as intended and the wait method was proven already in the linked answer... – Aacini Mar 03 '17 at 11:21
  • I inserted my program and files into this example and the program did not launch and the files did not move. I only changed "arj.exe" with my program location, as well as the folder and file locations. The rest I left as your example. Did I miss something? – user3048136 Mar 03 '17 at 11:30
  • Please, remove the lines with parentheses and be sure that the code run in the same way as your original code, with no wait. After that, reinsert the lines with parentheses... – Aacini Mar 03 '17 at 15:50
  • Removing the parentheses made the program load and the files moved like the original with no wait, but adding the parentheses seems to kill the whole thing. Nothing happened. – user3048136 Mar 03 '17 at 22:13
  • Ops! Remove the `rem` command! I already did it in the posted code. This is a subtle bug that appears now and then... **`:(`** – Aacini Mar 04 '17 at 01:57
  • what's the `| set /P "="` for? – phuclv Mar 04 '17 at 03:31
  • May I ask you to select (check-mark) this answer and upvote it? – Aacini Mar 04 '17 at 11:08
  • @LưuVĩnhPhúc: Is the way to wait for the started process. The `set /P` command would terminate when anyone of the commands in the `( block )` outputs a line, but `start` command don't show any line _in this cmd.exe_. This way, `set /P` keeps waiting for input until the process started by `start` ends. At that point the pipe line associated to the `( block )` is released, the `set /P` Stdin is closed and `set /P` command is terminated by the OS. – Aacini Mar 04 '17 at 11:42
  • if it waits for the started process then why do we need the ping to delay? – phuclv Mar 04 '17 at 13:33
  • @LưuVĩnhPhúc: Because _"the only problem is the files move to the new folder before the program loads. I guess I could just add a 'pause' to wait for it to load"_, as the OP explained in a comment. – Aacini Mar 04 '17 at 16:17
-1

The easy way would be

move /-y "C:\FolderA\File1.txt" "C:\FolderB\"
move /-y "C:\FolderA\File2.txt" "C:\FolderB\"
"C:\ProgramX.exe"

move /-y "C:\FolderB\File1.txt" "C:\FolderA\"
move /-y "C:\FolderB\File2.txt" "C:\FolderA\"

or

move /-y "C:\FolderA\File1.txt" "C:\FolderB\"
move /-y "C:\FolderA\File2.txt" "C:\FolderB\"
start /wait "" "C:\ProgramX.exe"

move /-y "C:\FolderB\File1.txt" "C:\FolderA\"
move /-y "C:\FolderB\File2.txt" "C:\FolderA\"

but if you insist on doing the move after the start,

@ECHO OFF
SETLOCAL
start /b "process running" "arj.exe"
move /-y "C:\FolderA\File1.txt" "C:\FolderB\"
move /-y "C:\FolderA\File2.txt" "C:\FolderB\"

ECHO waiting
:wait
tasklist /FI "IMAGENAME eq arj.exe"|FINDSTR /i "arj.exe">nul&IF NOT errorlevel 1 timeout /t 1 >nul&goto wait

move /-y "C:\FolderB\File1.txt" "C:\FolderA\"
move /-y "C:\FolderB\File2.txt" "C:\FolderA\"
ECHO finished

pause

GOTO :EOF

I used arj.exe here to test the methodology. Naturally, it could be any executable that exits under user-control, unlike notepad that exits immediately having instantiated an interactive instance.

I'm presuming that there will be only one instance of programx active on the system at any one time.

The echos and pause are present simply to demonstrate the batch program state.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • So close @Magoo! The only problem is the files move to the new folder before the program loads. I guess I could just add a 'pause' to wait for it to load...unless there is a better way? Thanks – user3048136 Mar 03 '17 at 09:36
  • You could add a `timeout` so that the user doesn't have to do anything. – Magoo Mar 03 '17 at 09:38
  • That works!! Thank you @Magoo! My only question would be if this program was left open all day, would the batch file cause the computer to bog down since it is continuously looking to see if the program closes? – user3048136 Mar 03 '17 at 10:43
  • 1
    Bog down? No. It's looking for the task once per second - should be minimal impact. By increasing the `timeout` parameter, it will lessen the impact if required, the downside being that the "move back" average time afer mainapp closure will be increased. – Magoo Mar 03 '17 at 11:31
  • Got it. Thanks! Very helpful! – user3048136 Mar 04 '17 at 10:39