Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Shell 

Make batch file create another batch file in specific directory

So I want to make a batch file which creates another batch file with some code in it in a specific folder. How would I do this?
Like this:

Code
@echo off
set "targetDir=C:\myFavouriteDir"
md "%targetDir%"

set "out=%targetDir%\helloWorld.bat"

echo @echo off>%out%
echo echo Hello world>>%out%
echo pause>>%out%

echo File was created.
pause

md stands for Make Directory.

For the part where we create the .bat file, note that the first line uses a single > whereas all subsequent lines use a >>
A single > means "Write this text to the file, overwriting the file if it exists"
A double >> means "APPEND this text to the file, do not overwrite"

Try this code and let me know how you get on.
Once you've confirmed you've got it working, we can start to discuss what happens when you try to send reserved characters like the percentage symbol to %out%. :>
You will need something like this :
Code
@echo off
set newBatch="C:\Your folder\yourbatch.bat"
echo echo hello > %newBatch%
echo pause >> %newBatch%
echo File created
pause

And you will have a file called yourbatch.bat in the folder C:\Your folder.

Explanation :
Code
@echo off      ; I don't need to explain I think
set newBatch="C:\Your folder\yourbatch.bat"    ; Sets the output file in the specified folder with the specified name
echo echo hello > %newBatch%      ; Set the line in the output batch file. A simple ">" will create or replace the file.
echo pause >> %newBatch%      ; Set this line in the output batch file. A double ">>" will add the line to the file but can't create it.
echo File created      ; echo command
pause      ; Pause command.

I hope I have helped !

Edit : I typed too slow :|. Anyway.
(Mar 4, 2015 09:37 AM)Hash G. Wrote:  You will need something like this :
Code
@echo off
set newBatch="C:\Your folder\yourbatch.bat"
echo echo hello > %newBatch%
echo pause >> %newBatch%
echo File created
pause

And you will have a file called yourbatch.bat in the folder C:\Your folder.

Explanation :
Code
@echo off      ; I don't need to explain I think
set newBatch="C:\Your folder\yourbatch.bat"    ; Sets the output file in the specified folder with the specified name
echo echo hello > %newBatch%      ; Set the line in the output batch file. A simple ">" will create or replace the file.
echo pause >> %newBatch%      ; Set this line in the output batch file. A double ">>" will add the line to the file but can't create it.
echo File created      ; echo command
pause      ; Pause command.

I hope I have helped !

Edit : I typed too slow :|. Anyway.

It created the file where I wanted it to be. But there was no text inside the batch file.

The code:
@echo off
set newBatch="C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\faggotcomet.bat"
:calc > %newBatch%
start calc.exe > %newBatch%
goto :calc > %newBatch%
You need to add some Echos there m8 ;)

if you do:

Code
echo text here WOOOOH > %newbatch%
echo more text i want in the batch file >> %newbatch%

it will be added to your %newbatch% :)

And as Annikk just said: For the part where we create the .bat file, note that the first line uses a single > whereas all subsequent lines use a >>
A single > means "Write this text to the file, overwriting the file if it exists"
A double >> means "APPEND this text to the file, do not overwrite"
Code
@echo off
set newBatch="C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\faggotcomet.bat"
:calc > %newBatch%
start calc.exe > %newBatch%
goto :calc > %newBatch%

Two things.

First of all, you need to echo the commands in order the send them to the batch file.
The basic idea here is we use the > symbol to redirect output from the command console to somewhere else, in this case, a file.
So normally when we do:
Code
echo Cats are nice
We expect to see "Cats are nice" on the screen.
But if we do:
Code
echo Cats are nice>cats.txt
..then we would see nothing, and instead, a file called cats.txt would have appeared, containing the text "Cats are nice".

So, the first issue is that you need to put an echo command in front of each line of text that you want to send to the file.

The second issue is with all the single > symbols.

Remember:
A single > means "Write this text to the file, overwriting the file if it exists"
A double >> means "Write this text to the file, but just add it on as the next line, DO NOT overwrite the existing file"

The reason the file appears, but is blank, is because each command overwrites the file with its output. The final command is a "goto" command, which does not have any output, so the file is blank.

To fix it, you should always use DOUBLE "greater than" symbols on every line except the first.
Code
echo Cats are nice>cats.txt
echo They are very soft>>cats.txt

With that in mind, your code should look like this:

Code
@echo off
set newBatch="C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\catsAreBest.bat"
:calc> %newBatch%
start calc.exe>> %newBatch%
goto :calc>> %newBatch%

By the way, this is an immature script... a batch script trying to be "malicious"... we heavily disapprove of such childishness around here, but I am letting it go for now because you might learn something if you read this far. So I guess it's worth it.
(Mar 4, 2015 10:27 AM)ninen2001 Wrote:  It created the file where I wanted it to be. But there was no text inside the batch file.

The code:
@echo off
set newBatch="C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\faggotcomet.bat"
:calc > %newBatch%
start calc.exe > %newBatch%
goto :calc > %newBatch%

I wouldn't mind helping you if it were for something productive or useful. I do feel you should be aware that something in the startup folder does NOT make it run on startup.
(Mar 5, 2015 07:25 AM)Nakamoto. Wrote:  I wouldn't mind helping you if it were for something productive or useful. I do feel you should be aware that something in the startup folder does NOT make it run on startup.

All the files in there does for me.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Shell] by Robert RougeSep 24, 2026 10:02 AM 4 219 Sep 24, 2026 10:02 AM
Last Post: Anonynuk
[Shell] by Eternal BlueMay 14, 2022 04:02 PM 4 228 May 14, 2022 04:02 PM
Last Post: Sajuuk
[C#] by JoseCmanXDIIMar 7, 2022 03:37 AM 10 512 Mar 7, 2022 03:37 AM
Last Post: JoseCmanXDII