[SOLVED] Write a batch to write a batch correctly?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
serverdelux
Posts: 36
Joined: 04 Oct 2013 14:14

Re: Write a batch to write a batch correctly?

#16 Post by serverdelux » 20 Oct 2013 22:55

Was actually thinking this might work but haven't worked out the bugs yet :(

Code: Select all

md %SystemDrive%\1
md %SystemDrive%\1\2


Code: Select all

echo :Uninstall1>>%SystemDrive%\1\2\3.bat
echo CLS >>%SystemDrive%\1\2\3.bat
echo echo Uninstalling...>>%SystemDrive%\1\2\3.bat
echo schtasks /query ^| FINDSTR /I "Task1">>%SystemDrive%\1\2\3.bat
echo IF ERRORLEVEL 1 ( >>%SystemDrive%\1\2\3.bat
echo GOTO 1 >>%SystemDrive%\1\2\3.bat
echo ) ELSE ( >>%SystemDrive%\1\2\3.bat
echo GOTO 2 >>%SystemDrive%\1\2\3.bat
echo ) >>%SystemDrive%\1\2\3.bat

echo :1>>%SystemDrive%\1\2\3.bat
echo echo rd /s /q %SystemDrive%\1 ^>^>%SystemDrive%\1\2\remove1.bat >>%SystemDrive%\1\2\3.bat
echo echo rd /s /q %SystemDrive%\1 ^>^>%SystemDrive%\1\2\remove1.bat >>%SystemDrive%\1\2\3.bat
echo echo del /F /Q %SystemDrive%\remove1.bat  ^>^>%SystemDrive%\1\2\remove1.bat >>%SystemDrive%\1\2\3.bat
echo move /y %SystemDrive%\1\2\remove1.bat "%SystemDrive%\remove1.bat" >>%SystemDrive%\1\2\3.bat
echo start %SystemDrive%\remove1.bat >>%SystemDrive%\1\2\3.bat
echo GOTO :Exit >>%SystemDrive%\1\2\3.bat

echo :2>>%SystemDrive%\1\2\3.bat
echo echo rd /s /q %SystemDrive%\1\2 ^>^>%SystemDrive%\1\2\remove1.bat >>%SystemDrive%\1\2\3.bat
echo echo rd /s /q %SystemDrive%\1\2 ^>^>%SystemDrive%\1\2\remove1.bat >>%SystemDrive%\1\2\3.bat
echo echo del /F /Q %SystemDrive%\remove1.bat  ^>^>%SystemDrive%\1\2\remove1.bat >>%SystemDrive%\1\2\3.bat
echo move /y %SystemDrive%\1\2\remove1.bat "%SystemDrive%\remove1.bat" >>%SystemDrive%\1\2\3.bat
echo start %SystemDrive%\remove1.bat >>%SystemDrive%\1\2\3.bat
echo GOTO :Exit >>%SystemDrive%\1\2\3.bat

echo :Exit >>%SystemDrive%\1\2\3.bat
echo exit >>%SystemDrive%\1\2\3.bat


It pulls these errors

Code: Select all

C:\1\2>rd /s /q C:\1
C:\1\2 - The process cannot access the file because it is being used by another
process.

C:\1\2>rd /s /q C:\1
C:\1\2 - The process cannot access the file because it is being used by another
process.

C:\1\2>del /F /Q C:\remove1.bat
The batch file cannot be found.

C:\1\2>

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Write a batch to write a batch correctly?

#17 Post by penpen » 21 Oct 2013 03:38

The problem is your working directory; which one do you expect after executing this code?
C:\1\2>rd /s /q C:\1

penpen

serverdelux
Posts: 36
Joined: 04 Oct 2013 14:14

Re: Write a batch to write a batch correctly?

#18 Post by serverdelux » 21 Oct 2013 10:08

penpen wrote:The problem is your working directory; which one do you expect after executing this code?
C:\1\2>rd /s /q C:\1

penpen


remove1.bat should be moved to C: to delete dir 1 and/or 1/2 then remove1.bat deletes itself

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Write a batch to write a batch correctly?

#19 Post by penpen » 21 Oct 2013 12:23

You've misunderstood me:
Your current working directory is "\1\2" on volume "C:".
You want to delete all directories/subdirectories/files starting with "\1" on volume "C:".
So you want to delete your working directory, too.
This is a problem: Which directory should be your new actual working directory?
Because of that the directory is (b)locked (in use), so your rd command is executed and aborted with error(s).

penpen

serverdelux
Posts: 36
Joined: 04 Oct 2013 14:14

Re: Write a batch to write a batch correctly?

#20 Post by serverdelux » 21 Oct 2013 12:28

penpen wrote:You've misunderstood me:
Your current working directory is "\1\2" on volume "C:".
You want to delete all directories/subdirectories/files starting with "\1" on volume "C:".
So you want to delete your working directory, too.
This is a problem: Which directory should be your new actual working directory?
Because of that the directory is (b)locked (in use), so your rd command is executed and aborted with error(s).

penpen


So should I move 3.bat to C:?

I thought if remove1.bat was moved to C: that that would be new working directory

Do we have to use a CD cmd?

Any ideas or code to try?

Thanks

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Write a batch to write a batch correctly?

#21 Post by penpen » 21 Oct 2013 13:06

Every (cmd) instance has its own (b)locked actual working directory:
The cmd instance that is calling your batch script, just as actual batch script.
Additionally the batch script you are running should be not deleted while running.
So you must move your batch scripts to a another path prior to executing them.
Then you may delete the directories (with no further runnign cmd instances blocking files/directories).
After all the batch script may delete itself if you ensure that this script is not read anymore.

penpen

PS: I would develop an own MSI installer packages, using MS Visual Studio with InterDev, or Flexera Software InstallShield, or any free MSI editor.

serverdelux
Posts: 36
Joined: 04 Oct 2013 14:14

Re: Write a batch to write a batch correctly?

#22 Post by serverdelux » 21 Oct 2013 13:23

penpen wrote:Every (cmd) instance has its own (b)locked actual working directory:
The cmd instance that is calling your batch script, just as actual batch script.
Additionally the batch script you are running should be not deleted while running.
So you must move your batch scripts to a another path prior to executing them.
Then you may delete the directories (with no further runnign cmd instances blocking files/directories).
After all the batch script may delete itself if you ensure that this script is not read anymore.

penpen

PS: I would develop an own MSI installer packages, using MS Visual Studio with InterDev, or Flexera Software InstallShield, or any free MSI editor.


This is just one small aspect of a very large program that I've been working on for years so I won't be doing any "real" programming

So what would you suggest as option to get a clean working directory by moving or changing directories

Sending you pm penpen :shock:

Any creative ideas out there?

Thanks

serverdelux
Posts: 36
Joined: 04 Oct 2013 14:14

Re: Write a batch to write a batch correctly?

#23 Post by serverdelux » 21 Oct 2013 16:41

Trying this now

Code: Select all

md %SystemDrive%\1
md %SystemDrive%\1\2


Code: Select all

echo :Uninstall1>>%SystemDrive%\1\2\3.bat
echo CLS >>%SystemDrive%\1\2\3.bat
echo echo Uninstalling...>>%SystemDrive%\1\2\3.bat
echo schtasks /query ^| FINDSTR /I "Task">>%SystemDrive%\1\2\3.bat
echo IF ERRORLEVEL 1 ( >>%SystemDrive%\1\2\3.bat
echo GOTO 1 >>%SystemDrive%\1\2\3.bat
echo ) ELSE ( >>%SystemDrive%\1\2\3.bat
echo GOTO 2 >>%SystemDrive%\1\2\3.bat
echo ) >>%SystemDrive%\1\2\3.bat

echo :1>>%SystemDrive%\1\2\3.bat
echo rd /s /q %SystemDrive%\1 >>%SystemDrive%\remove1.bat
echo rd /s /q %SystemDrive%\1 >>%SystemDrive%\remove1.bat
echo schtasks /delete /tn "Task1" /f >NUL >>%SystemDrive%\remove1.bat
echo del /F /Q %SystemDrive%\remove1.bat >>%SystemDrive%\remove1.bat
echo schtasks /create /tn "task1" /tr "%SystemDrive%\remove1.bat" /sc onlogon /ru "" >nul >>%SystemDrive%\1\2\3.bat
echo schtasks /run /tn "task1" >>%SystemDrive%\1\2\3.bat
echo GOTO :Exit >>%SystemDrive%\1\2\3.bat

echo :2>>%SystemDrive%\1\2\3.bat
echo rd /s /q %SystemDrive%\1\2 >>%SystemDrive%\remove1.bat
echo rd /s /q %SystemDrive%\1\2 >>%SystemDrive%\remove1.bat
echo schtasks /delete /tn "Task1" /f >NUL >>%SystemDrive%\remove1.bat
echo del /F /Q %SystemDrive%\remove1.bat >>%SystemDrive%\remove1.bat
echo schtasks /create /tn "task1" /tr "%SystemDrive%\remove1.bat" /sc onlogon /ru "" >nul >>%SystemDrive%\1\2\3.bat
echo schtasks /run /tn "task1" >>%SystemDrive%\1\2\3.bat
echo GOTO :Exit >>%SystemDrive%\1\2\3.bat

echo :Exit >>%SystemDrive%\1\2\3.bat
echo exit >>%SystemDrive%\1\2\3.bat


Crossing my fingers :P

serverdelux
Posts: 36
Joined: 04 Oct 2013 14:14

Re: Write a batch to write a batch correctly?

#24 Post by serverdelux » 21 Oct 2013 16:53

It worked :shock:...lol

Thank you foxidrive, penpen and ShadowThief

Post Reply