Page 1 of 2

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

Posted: 13 Oct 2013 14:06
by serverdelux
Hi, I am having hard time for batch to write a batch that writes another batch

Code: Select all

@echo off
echo echo SET file=%SystemDrive%\1\1a ^>^>%SystemDrive%\remove.bat >>%SystemDrive%\2\2.bat
echo echo IF EXIST %%file%% attrib -h %%file%% ^>^>%SystemDrive%\remove.bat >>%SystemDrive%\2\2.bat
echo echo rd /S /Q %%file%% ^>^>%SystemDrive%\remove.bat  >>%SystemDrive%\2\2.bat


But I need remove.bat to end up like this

Code: Select all

SET file=C:\1\1a
IF EXIST %file% attrib -h %file%
rd /S /Q %file%


Not sure how to use the % in 2.bat to get remove.bat to write correctly

Re: Write a batch to write a batch correctly?

Posted: 13 Oct 2013 16:39
by aGerman
What are you trying to do? Writing a batch code that writes a batch code that writes another batch code :?:

Re: Write a batch to write a batch correctly?

Posted: 13 Oct 2013 16:51
by serverdelux
aGerman wrote:What are you trying to do? Writing a batch code that writes a batch code that writes another batch code :?:


Yes, 2.bat writes remove.bat

So in essence one file is written (2.bat), then if prompted will write to another (remove.bat) and need (remove.bat) to be written like

Code: Select all

SET file=C:\1\1a
IF EXIST %file% attrib -h %file%
rd /S /Q %file%


So that 1a folder is deleted

Thanks

:mrgreen:

Re: Write a batch to write a batch correctly?

Posted: 13 Oct 2013 17:02
by aGerman
That way?

Code: Select all

>>"%SystemDrive%\2\2.bat" echo ^>^>"%%SystemDrive%%\remove.bat" echo SET file=C:\1\1a
>>"%SystemDrive%\2\2.bat" echo ^>^>"%%SystemDrive%%\remove.bat" echo IF EXIST %%%%file%%%% attrib -h %%%%file%%%%
>>"%SystemDrive%\2\2.bat" echo ^>^>"%%SystemDrive%%\remove.bat" echo rd /S /Q %%%%file%%%%


Regards
aGerman

Re: Write a batch to write a batch correctly?

Posted: 13 Oct 2013 17:17
by serverdelux
aGerman wrote:That way?

Code: Select all

>>"%SystemDrive%\2\2.bat" echo ^>^>"%%SystemDrive%%\remove.bat" echo SET file=C:\1\1a
>>"%SystemDrive%\2\2.bat" echo ^>^>"%%SystemDrive%%\remove.bat" echo IF EXIST %%%%file%%%% attrib -h %%%%file%%%%
>>"%SystemDrive%\2\2.bat" echo ^>^>"%%SystemDrive%%\remove.bat" echo rd /S /Q %%%%file%%%%


Regards
aGerman


Hi, it needs to be written to 2.bat, then 2.bat writes remove.bat to end up with code like this

Code: Select all

SET file=C:\1\1a
IF EXIST %file% attrib -h %file%
rd /S /Q %file%


Thanks

:mrgreen:

Re: Write a batch to write a batch correctly?

Posted: 13 Oct 2013 17:20
by aGerman
Did you already try my code above :?

Re: Write a batch to write a batch correctly?

Posted: 13 Oct 2013 23:38
by serverdelux
aGerman wrote:Did you already try my code above :?


I had a back slash where there should have been a slash and it didn't detect the folder to delete it

Thank you for your code, it does work!

I just need to successfully include it into my main batch which is turning out to be difficult

:D

Re: Write a batch to write a batch correctly?

Posted: 20 Oct 2013 00:01
by serverdelux
I have another question about a write to batch

Why won't this write :shock:

echo schtasks /query | FINDSTR /I "Task1" >NUL >>%SystemDrive%\1\2\3.bat

I open up 3.bat and it's empty

But this does write :?

echo schtasks /delete /tn "Task1" /f >NUL 2>&1 >>%SystemDrive%\1\2\3.bat

Any help please?

Thanks

:D

Re: Write a batch to write a batch correctly?

Posted: 20 Oct 2013 01:45
by foxidrive
serverdelux wrote:
Why won't this write :shock:

echo schtasks /query | FINDSTR /I "Task1" >NUL >>%SystemDrive%\1\2\3.bat


You haven't escaped the poison characters | and >

Re: Write a batch to write a batch correctly?

Posted: 20 Oct 2013 03:09
by serverdelux
foxidrive wrote:
serverdelux wrote:
Why won't this write :shock:

echo schtasks /query ^^^| FINDSTR /I "Task1" ^^^>NUL >>%SystemDrive%\1\2\3.bat


You haven't escaped the poison characters | and >


I tried this

Code: Select all

echo schtasks /query ^^^| FINDSTR /I "Task1" ^^^>NUL >>%SystemDrive%\3.bat
and writes

Code: Select all

schtasks /query ^| FINDSTR /I "Task1" ^>NUL 


Any help?

Also should I use this

Code: Select all

>NUL 2>&1
like the schtasks delete
instead of just this

Code: Select all

>NUL


so it doesn't show in console

Thanks for your help

:mrgreen:

Re: Write a batch to write a batch correctly?

Posted: 20 Oct 2013 03:12
by foxidrive
As you posed the problem, just one carat is needed in two places.

Code: Select all

echo schtasks /query ^| FINDSTR /I "Task1" ^>NUL >>%SystemDrive%\1\2\3.bat

Re: Write a batch to write a batch correctly?

Posted: 20 Oct 2013 03:44
by serverdelux
foxidrive wrote:As you posed the problem, just one carat is needed in two places.

Code: Select all

echo schtasks /query ^| FINDSTR /I "Task1" ^>NUL >>%SystemDrive%\1\2\3.bat


Thank you for your assistance and teaching

:wink:

Re: Write a batch to write a batch correctly?

Posted: 20 Oct 2013 15:52
by serverdelux
Back to my original question:

I have function here that should create a batch to delete the folders it is inside

I'm not sure I have to use "cd" cmd but here is what I'm working on

Code: Select all

md %SystemDrive%\1
md %SystemDrive%\1\2
schtasks /create /tn "Task1" /tr "%SystemDrive%\1\2\4.bat" /sc onstart /ru ""


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 cd %SystemDrive%^>^>%SystemDrive%\remove1.bat >>%SystemDrive%\1\2\3.bat
echo echo rd /s /q %SystemDrive%\1 ^>^>%SystemDrive%\remove1.bat  >>%SystemDrive%\1\2\3.bat
echo echo rd /s /q %SystemDrive%\1 ^>^>%SystemDrive%\remove1.bat >>%SystemDrive%\1\2\3.bat
echo echo del /F /Q %SystemDrive%\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 cd %SystemDrive%^>^>%SystemDrive%\remove1.bat >>%SystemDrive%\1\2\3.bat
echo echo rd /s /q %SystemDrive%\1\2 ^>^>%SystemDrive%\remove1.bat  >>%SystemDrive%\1\2\3.bat
echo echo rd /s /q %SystemDrive%\1\2 ^>^>%SystemDrive%\remove1.bat >>%SystemDrive%\1\2\3.bat
echo echo del /F /Q %SystemDrive%\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


Basically I need it to manifest remove1.bat with these contents in %systemdrive%

Code: Select all

rd /s /q %SystemDrive%\1
rd /s /q %SystemDrive%\1
del /F /Q %SystemDrive%\remove1.bat


or

Code: Select all

rd /s /q %SystemDrive%\1\2
rd /s /q %SystemDrive%\1\2
del /F /Q %SystemDrive%\remove1.bat


Eventually I will also need it to all be done without seeing any code in console

except an echo msg that says uninstallation successful...

Re: Write a batch to write a batch correctly?

Posted: 20 Oct 2013 20:04
by zoeyku
so it doesn't show in console

Re: Write a batch to write a batch correctly?

Posted: 20 Oct 2013 20:28
by serverdelux
zoeyku wrote:so it doesn't show in console


Yes I'll be using something like >nul but first I need the main code to function and delete the folders so the operations not showing is not needed until the remove1.bat is working

Thanks