Windows – Two batch files won’t work together, where is the issue

batchbatch filecommand linewindows

I have a file called fix.bat in the F:\ directory, that I need to move to C:\Users. For a good reason, I decided that I should code a file within fix.bat, that would be created when fix.bat is launched, and would move fix.bat from the F:\ directory to C:\Users, start up fix.bat, and in the process of all this create a few folders in C:\Users.

Now, at the very beginning of the fix.bat file, a check is done to determine whether or not fix.bat is already in C:\Users. If it is, then start.bat is not made, but fix.bat continues with it's task. If it is not, start.bat is made and moves fix.bat to C:\Users. At least that is the idea.

For some reason, however, it will not work. What happens, is that an endless loop of CMD-windows open up and I have to restart the computer.

Here is the relevant part of the fix.bat code:

@echo off

if %cd% == C:\Users goto z
if not %cd% == C:\Users goto x

:x
echo pushd c:\users >>start.bat
echo md results-main >>start.bat
echo cd results-main >>start.bat
echo md results-e >>start.bat
echo md results-p >>start.bat
echo md results-s >>start.bat
echo md results-x >>start.bat
echo pushd %cd% >>start.bat
echo move fix.bat c:\users >>start.bat
echo pushd c:\users >>start.bat
echo start fix.bat >>start.bat
call start.bat
exit

:z
insert code here

Best Answer

1: Make FIX.bat delete Start.bat before writing to it --- as it keeps appending to it, so it gets longer and longer. This is probably why you are seeing it run over and over.

2: Rename "start.bat" to something else to avoid any confusion.

3: Instead of "call start.bat", just plain "start.bat" because you want to ensure that fix.bat isn't being held open by CMD, and you don't want to continue running it anyways.

4: In start.bat, change move fix.bat C:\users to move fix.bat C:\users || echo MOVE FAILED

The "||" operator means "If the move failed, do the echo"

You have to escape the | characters: echo move fix.bat C:\users >>qstart.bat ^|^| echo MOVE FAILED

Related Question