• 0

[.bat]- Using echo to create other .bat files !WITH COMMANDS!


Question

Ok I'm not extremely good with explaining things, so I'm going to jump right into giving the example:

Here's what I'm trying to do:

Create a folder named "New" in the drive "C:".

Then in the "New" folder creat a .bat file called "New".

-In the same line of .bat (the same line as creating the .bat file called "New") add commands such as "echo GOOD NIGHT" then "shutdown /s /t 10 /c "Good Night!" by using the echo parrameter. Here's the .bat code. (for you to correct)

+ I'm ONLY USING *ONE* .bat file to do all of this.(Create folder name "New" and in that folder create a .bat file named "New" that has commands "echo GOOD NIGHT" and "shutdown /s /t 10 /c "Good Night!")

mkdir C:\New
echo echo GOOD NIGHT
shutdown /s /t 10 /c "Good Night!">>C:\New\New.bat

I know this works if I am just putting PLAIN TEXT (With no commands) into the .bat file in the new folder. But my problem is that when I try to "echo" the commands into the new .bat file it only exicutes them in the original .bat file!

Edited by PVVnT(0)L
Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0
No you aint too good at explainging :p

I thought his explanation was just fine. I think I perfectly understood what he's wanting to do.

@echo off
mkdir C:\New
@echo echo GOOD NIGHT>>C:\New\New.bat
@echo shutdown /s /t 10 /c "Good Night!">>C:\New\New.bat

I believe this is the code that you're looking for?

It will create a new directory called "new", then echo the commands into a "new.bat" file located in the "new" folder.

Link to comment
Share on other sites

  • 0
No you aint too good at explainging :p

I think you need to mk the \new\new.bat first

then cd to that dir

then execute the shutdown command...

So your saying to create the "New" dir.

Then make New\New.bat

Then type in the command to shutdown?

Here's the problem:

I just want it to be done with out having to type in anything to the new.bat file(ALL IN ONE, if you will...). The problem still exists if your talking about using "echo echo GOOD MORING!>>New\New.bat".

This is because it does not edit the file it executes it in the .bat file that is creating the dir and the .bat file.

But it would work if it were "echo GOOD MORNING!>>New\New.bat" (With no commands)

EDIT: LOOK BELOW, PROBLEM HAS CHANGED (OLD PROBLEM SOLVED)

Edited by PVVnT(0)L
Link to comment
Share on other sites

  • 0

Void, you got it. Thanks abunch.

But here's the next problem. How could I apply "@echo " and " >>New\New.bat" to thousands of lines of commands? Is there a way to do this?

(The example I gave is just a VERY VERY VERY SHORT version of what i'm doing, and the real file I want to create by using the original .bat has thousands of lines of commands. It would be a very grouwling proccess to apply @echo and >>New\New.bat to everyone of them.)

Edited by PVVnT(0)L
Link to comment
Share on other sites

  • 0

Ok, so say I have a file named "Hi.exe" and I want to (once downloaded) upon to approval of the Admin, copy it to the desktop using a simple .bat file.

The part that i am having trouble understanding is, how do you navigate to the desktop without knowing the users name, or else I would use the C:\users\bob\desktop.

Link to comment
Share on other sites

  • 0

Depends. If you already have the file created and are trying to amend it to do that I would use find and replace, If I'm creating the file I would copy the ">>New\New.bat

@echo" to clipboard and just past it in everytime it's needed.

Or you could also create a for next loop but it really depends on how complex the task you want to execute is.

Link to comment
Share on other sites

  • 0

If you have to process thousands of lines of commands, you might be better using a

VBS script to process the lines and produce the new bat file.

Here is a VBS script that all you have to do is drag and drop the bat file on to it.

Save as ReadMakeBat.vbs

Option Explicit
 Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
 Dim Arg1, Col, File, NewFile, StrInfo, TargetFolder, Ts

  TargetFolder = "C:\New"
  NewFile = "C:\New\New.bat"

 Arg1 = Wscript.Arguments.Count
  If Arg1 = 0 Then
  '-> Nothing Was Drop On The Script
   InValid()
  Else
  '-> Process The Drop File On The Script
  ProcessArg1()
  End If 

  Function ProcessArg1()
  '-> Check For Target Folder Make Target Folder If Not Exists
   If Not Fso.FolderExists(TargetFolder) Then Fso.CreateFolder(TargetFolder)
	For Each Col In Wscript.Arguments
	'-> Check To Make Sure It A Cmd Or Bat File
	 If Right(InStr(LCase(Col),LCase(".bat")),4) Or Right(InStr(LCase(Col),LCase(".cmd")),4) Then 
	  File = Col
	 End If 
	Next
	'-> Read The Bat Or Cmd
	Set Ts = Fso.OpenTextFile(File,1)
	 StrInfo = Ts.ReadAll
	 Ts.Close
	'-> Make New Bat 
	Set Ts = Fso.CreateTextFile(NewFile)
	 Ts.WriteLine StrInfo
	 Ts.close
  End Function

  Function InValid()
  CreateObject("Wscript.Shell").Popup vbTab & "No Source File" & vbCrLf &_
   "This Script needs a .bat Or .cmd to be" & vbCrLf &_
   "Drag and Drop on this scrpt to make active",0,"No Source",4128
  End Function

I tried this script and it produced a new.bat in the new folder

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.