[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

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

#1 Post by serverdelux » 13 Oct 2013 14:06

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
Last edited by serverdelux on 24 Oct 2013 17:57, edited 2 times in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Write a batch to write a batch correctly?

#2 Post by aGerman » 13 Oct 2013 16:39

What are you trying to do? Writing a batch code that writes a batch code that writes another batch code :?:

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

Re: Write a batch to write a batch correctly?

#3 Post by serverdelux » 13 Oct 2013 16:51

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:

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Write a batch to write a batch correctly?

#4 Post by aGerman » 13 Oct 2013 17:02

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

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

Re: Write a batch to write a batch correctly?

#5 Post by serverdelux » 13 Oct 2013 17:17

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:

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Write a batch to write a batch correctly?

#6 Post by aGerman » 13 Oct 2013 17:20

Did you already try my code above :?

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

Re: Write a batch to write a batch correctly?

#7 Post by serverdelux » 13 Oct 2013 23:38

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

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

Re: Write a batch to write a batch correctly?

#8 Post by serverdelux » 20 Oct 2013 00:01

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Write a batch to write a batch correctly?

#9 Post by foxidrive » 20 Oct 2013 01:45

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 >

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

Re: Write a batch to write a batch correctly?

#10 Post by serverdelux » 20 Oct 2013 03:09

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:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Write a batch to write a batch correctly?

#11 Post by foxidrive » 20 Oct 2013 03:12

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

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

Re: Write a batch to write a batch correctly?

#12 Post by serverdelux » 20 Oct 2013 03:44

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:

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

Re: Write a batch to write a batch correctly?

#13 Post by serverdelux » 20 Oct 2013 15:52

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...

zoeyku
Posts: 3
Joined: 20 Oct 2013 19:59
Location: usa
Contact:

Re: Write a batch to write a batch correctly?

#14 Post by zoeyku » 20 Oct 2013 20:04

so it doesn't show in console

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

Re: Write a batch to write a batch correctly?

#15 Post by serverdelux » 20 Oct 2013 20:28

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

Post Reply