Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Batch input/output


  • Please log in to reply
23 replies to this topic

#1 gigawert

gigawert

    Computer Consultant


  • Members
  • 1,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:San Jose, California
  • Local time:11:16 PM

Posted 16 August 2015 - 08:01 PM

Could someone tell me how to read, write to, and create files in Batch? I know all the basics of Batch except this, and I couldn't find a straight answer on Google. Thank you! :)


John 3:16

 "God loved the world so much that He gave His uniquely-sired Son, with the result that anyone who believes in Him would never perish but have eternal life."


BC AdBot (Login to Remove)

 


m

#2 SleepyDude

SleepyDude

  • Malware Response Team
  • 2,685 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Portugal
  • Local time:06:16 AM

Posted 20 August 2015 - 05:40 AM

Hi,

 

It depends what you want to do. A simple example.

 

dir > out.txt


• Please do not PM me asking for support. Post on the forums instead it will increases the chances of getting help for your problem by one of us.
• Posts in the Malware section that are not replied to within 4 days will be closed. PM me or a moderator to reactivate.
• Please post your final results, good or bad. We like to know! Thank you!

 
Proud graduate of GeekU and member of UNITE
___
Rui

 
 


#3 gigawert

gigawert

    Computer Consultant

  • Topic Starter

  • Members
  • 1,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:San Jose, California
  • Local time:11:16 PM

Posted 20 August 2015 - 09:46 PM

I'm guessing that's how to create a file. How do I write to it? And how do I read a file and display the output?


John 3:16

 "God loved the world so much that He gave His uniquely-sired Son, with the result that anyone who believes in Him would never perish but have eternal life."


#4 SleepyDude

SleepyDude

  • Malware Response Team
  • 2,685 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Portugal
  • Local time:06:16 AM

Posted 21 August 2015 - 04:00 AM

I'm guessing that's how to create a file. How do I write to it? And how do I read a file and display the output?

 

If I'm not mistaken in DOS there are no specific commands to write to a file you should redirect like I show you in the above example.

 

To display the file type out.txt

 

You need to provide more concrete details or part of code for what you want to do.


• Please do not PM me asking for support. Post on the forums instead it will increases the chances of getting help for your problem by one of us.
• Posts in the Malware section that are not replied to within 4 days will be closed. PM me or a moderator to reactivate.
• Please post your final results, good or bad. We like to know! Thank you!

 
Proud graduate of GeekU and member of UNITE
___
Rui

 
 


#5 gigawert

gigawert

    Computer Consultant

  • Topic Starter

  • Members
  • 1,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:San Jose, California
  • Local time:11:16 PM

Posted 21 August 2015 - 07:04 PM

I know how to write to a file in Python, maybe I should just do that...


John 3:16

 "God loved the world so much that He gave His uniquely-sired Son, with the result that anyone who believes in Him would never perish but have eternal life."


#6 RolandJS

RolandJS

  • Members
  • 4,293 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Austin TX metro area
  • Local time:12:16 AM

Posted 08 October 2015 - 12:06 PM

Prior to Windows 95, I used many many batchfiles [the ideas of which I like a programmer borrowed from many many other folks] for my Windows 3.1 - Windows For Workgroups 3.11. If you want a copy of those batchfiles for "reverse-engineering" -- lemmie know.

"Take care of thy backups and thy restores shall take care of thee."  -- Ben Franklin revisited.

http://collegecafe.fr.yuku.com/forums/45/Computer-Technologies/

Backup, backup, backup! -- Lady Fitzgerald (sevenforums)

Clone or Image often! Backup, backup, backup, backup... -- RockE (Windows Secrets Lounge)


#7 technonymous

technonymous

  • Members
  • 2,465 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:10:16 PM

Posted 10 October 2015 - 03:24 AM

Here's a lInk on how to do redirection pipes for dos..http://ss64.com/nt/syntax-redirection.html

 

Just like command > filename you can use command > filename

 

You can also copy con.

 

copy con c:\helloworld.bat (press enter and type your text..)

 

@echo Hello World!!

@echo pause

 

Ctrl+z (enter) to close copy con and write the text above to the batch file.

 

Note: You need to elevate cmd promp to administrator for these commands to work and access to write to C:\ in the example above.

 

Hope that helps you out. :)


Edited by technonymous, 10 October 2015 - 03:24 AM.


#8 Barnack

Barnack

  • Members
  • 87 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Italy
  • Local time:08:16 AM

Posted 19 October 2015 - 04:47 PM

Wait by read you mean print to screen or execute a file?



#9 RolandJS

RolandJS

  • Members
  • 4,293 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Austin TX metro area
  • Local time:12:16 AM

Posted 20 October 2015 - 09:49 AM

Wait by read you mean print to screen or execute a file?

Depending upon what command is given, one either "prints" to screen or "prints" into a text file.


"Take care of thy backups and thy restores shall take care of thee."  -- Ben Franklin revisited.

http://collegecafe.fr.yuku.com/forums/45/Computer-Technologies/

Backup, backup, backup! -- Lady Fitzgerald (sevenforums)

Clone or Image often! Backup, backup, backup, backup... -- RockE (Windows Secrets Lounge)


#10 technonymous

technonymous

  • Members
  • 2,465 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:10:16 PM

Posted 20 October 2015 - 11:57 AM

Here's a lInk on how to do redirection pipes for dos..http://ss64.com/nt/syntax-redirection.html

 

Just like command > filename you can use command > filename

 

You can also copy con.

 

copy con c:\helloworld.bat (press enter and type your text..)

 

@echo Hello World!!

@echo pause

 

Ctrl+z (enter) to close copy con and write the text above to the batch file.

 

Note: You need to elevate cmd promp to administrator for these commands to work and access to write to C:\ in the example above.

 

Hope that helps you out. :)

Woops sorry about that I was up late my bad. The command: type filename will print to screen. Which way you use carets for command is for input or output.

 

command < input

command > output

 

copy con command allows you to keep on typing then end the task with ctrl-z to write it to the file. If you follow along with the examples it's easier to do all this at the desktop directory so you can see how the files are created. This is creating a simple hello world batch file that echos hello world to the screen when ran. The pause command prompts the user for keyboard input to continue. Since there is no other command it closes the command window. No need for administrator cmd for these.

 

Once you open cmd...

 

cd desktop

 

copy con hello.bat

@echo off

echo Hello World!

pause

 

#Press ctrl-z & enter

#To run the batch just type hello.bat and enter.

 

--------------------------------------------------------------------------------------------------------------------------------------------------

 

Example of: Echoing text to output file, append to output file, check results, input and run those commands from ping.txt and ouput results to pingresults.txt.

 

echo ping 127.0.0.1 > ping.txt

echo ping 192.168.1.1 >> ping.txt                   

type ping.txt

cmd < ping.txt > pingresults.txt

 

-First we delete the files from last example:

 

del ping.txt && del pingresults.txt

 

-You can do all of this in one line using ampersands.

 

echo ping 127.0.0.1 > ping.txt && echo ping 192.168.1.1 >> ping.txt && cmd < ping.txt > pingresults.txt && del ping.txt

 

 

-------------Editing---------------------

 

Edit pingresults.txt

or,

notepad pingresults.txt

 

del pingresults.txt

 

--------------Example of Pipe commands with route print----------

 

route print

 

route print | findstr /A:0c /n "IPv4"

 

route print > routeprint.txt

 

type routeprint.txt | findstr /n "IPv4" | clip | notepad.exe

 

ctrl+v pastes the string into notepad.

 

-That last command finds a string inside routeprint.txt it's line number and copies that result to clipboard and then lauches notepad. Then you can ctrl+v paste it into notepad. Since you can just output directly to txt file you don't need to pipe to clipboard or pipe launch notepad.

 

type routeprint.txt | findstr /n "IPv4" > routeprint2.txt

 

So that's what pipes is about. Powershell is even more powerful.

 

Have fun! :)



#11 FreeBooter

FreeBooter

  • Members
  • 3,065 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Turkey - Adana
  • Local time:09:16 AM

Posted 02 July 2016 - 03:16 PM

Batch File Scripting Techniques



Share your knowledge. Its a way to achieve immortality.
- Dalai Lama

 

 


#12 technonymous

technonymous

  • Members
  • 2,465 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:10:16 PM

Posted 02 July 2016 - 06:09 PM

Thanks got it bookmarked. :)



#13 FreeBooter

FreeBooter

  • Members
  • 3,065 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Turkey - Adana
  • Local time:09:16 AM

Posted 02 July 2016 - 06:37 PM

You are very welcome!

Check out below links for more tutorials.

 

The Windows Command Line, Batch Files, and Scripting

 



Share your knowledge. Its a way to achieve immortality.
- Dalai Lama

 

 


#14 unasaindri

unasaindri

  • Banned Spammer
  • 5 posts
  • OFFLINE
  •  
  • Local time:01:16 PM

Posted 27 July 2016 - 02:13 AM

You are very welcome!
Check out below links for more tutorials.
 
The Windows Command Line, Batch Files, and Scripting
 
Batch File Downloads

thanks for tutorials link

Edited by Queen-Evie, 27 July 2016 - 02:15 PM.


#15 FreeBooter

FreeBooter

  • Members
  • 3,065 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Turkey - Adana
  • Local time:09:16 AM

Posted 27 July 2016 - 08:27 AM

You are very welcome!
Check out below links for more tutorials.
 
The Windows Command Line, Batch Files, and Scripting
 
Batch File Downloads

thanks for tutorials link

You are very welcome if anyone have question about batch scripting you can create thread here at this forum and we will do our best to help you answer your questions. I think we should all show of our batch scripting skills by creating batch scripts and letting BC members and public use the batch scripts we have created.

Edited by Queen-Evie, 27 July 2016 - 02:15 PM.


Share your knowledge. Its a way to achieve immortality.
- Dalai Lama

 

 





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users