Home / Microsoft / Microsoft DOS / adding a character to a txt file
0 Members and 2 Guests are viewing this topic. « previous next »
Pages: 1 2 [All] - (Bottom) Print
Author Topic: adding a character to a txt file  (Read 2134 times)
Sheetal
Guest
« on: February 09, 2005, 03:37:21 AM »

Hi

I have a txt file called hello.txt. I need to create a batch file that will add a character 'H' to the beginning of the hello.txt file.

Im not sure how to do this, or if it can be done at all

thanks
Sheetal
IP logged
MalikTous
Guest
« Reply #1 on: February 09, 2005, 07:50:42 PM »

Are you adding the character to the name (hhello.txt) or the contents of the file?

First make a file temp.txt:

Code: [Select]
copy con: temp.txt
h
CTRL-Z


Then run the appending code as a batch file:

Code: [Select]
copy temp.txt+hello.txt hello2.txt
ren hello.txt goodbye.txt
ren hello2.txt hello.txt
IP logged
Sheetal
Guest
« Reply #2 on: February 10, 2005, 08:22:35 AM »

Hi,

thanks for the reply, but im adding the new character to the contents of the file NOT to the filename.

Thanks
Sheetal
IP logged
tgenie
Guest
« Reply #3 on: February 15, 2005, 07:32:40 PM »

Hello....
according this issue, i got a problem...
how to make a new file within the batchfiles...

i know the way type

"copy con xxx.txt"
enter
enter
ctrl+z

then how can i excute the "ENTER" & "CTRL+Z"
or is there another way to make a new file? ???
IP logged
Zzyzx
Guest
« Reply #4 on: February 16, 2005, 06:04:12 AM »

If this is a DOS machine only, use EDIT or in a pinch EDLIN, to create/change your file. If you're creating a batch file save your file with a bat extension. (ie: yourfilename.bat).

If this is a windows machine you can use notepad or wordpad as your editor.

Hope this helps.  8)
« Last Edit: February 16, 2005, 06:04:50 AM by Zzyzx » IP logged
Sheetal
Guest
« Reply #5 on: February 16, 2005, 08:06:00 AM »

goin back to the original thread....

the file hello.txt has a number of line on it as shown below:

aaaaaaa
bbbbbbb
ccccccccc
ddddddd

I need t add a 'H' to the beggining of each file as shown below:

Haaaaaaa
Hbbbbbbb
Hccccccccc
Hddddddd

I dont want any line breaks, it must be as shown above. Also there may be different number of lines in the hello.txt file each time

please HELP!
IP logged
Zzyzx
Guest
« Reply #6 on: February 16, 2005, 11:54:12 AM »

DOS batch language was not designed to do what you are asking. If you have Windows (any flavor) you can use the following code:


  Const ForReading = 1, ForWriting = 2, UseDefault = -2
  Dim fso, filein, fileout, retstring
  filein = "C:\Hello.txt"
  fileout = "C:\NewHello.txt"
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set filein = fso.OpenTextFile(filein, ForReading,False)
Set fileout =fso.OpenTextFile(fileout,ForWriting,UseDefault)
  Do While filein.AtEndOfStream <> True
     retstring = filein.ReadLine
     retstring = "H" & retstring
     fileout.Write(retstring)
  Loop
  filein.Close
  fileout.Close
  WScript.Quit
 
Copy and Paste the above code into your editor and save it with a VBS extension. You may have to change the paths of the filein and fileout to match your situation.

After running the file (ie. myscript.VBS), you will have a NewHello.txt file with the "H" inserted in each line.

Good luck.  8)
IP logged
Sheetal
Guest
« Reply #7 on: February 24, 2005, 04:47:48 AM »

Hi

the VBs solution works great :o) many thanks for that. I have a question about it though.

My input filename hello.txt is actually called H838876.txt but the numbers after the H may be different everytime. In batch i can use H??????.txt to refer to the file. Is there any way of doin this in VBs?

Thanks
Sheetal
IP logged
Sheetal
Guest
« Reply #8 on: February 24, 2005, 04:48:58 AM »

sorry that should have read H??????.txt
IP logged
Zzyzx
Guest
« Reply #9 on: February 24, 2005, 11:07:02 AM »

If all your files are in the same directory, you can modify the existing script to run over a folder:

Const ForReading = 1, ForWriting = 2, UseDefault = -2
Dim fso, f, f1, fc,filein, fileout, retstring
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)                      ' <-- Change Line
Set fc = f.Files
For Each f1 in fc
      if mid(f1,1,1) <> "H" then Next
  fileout = fso.GetFileName(f1) & ".new"
  Set filein = fso.OpenTextFile(f1, ForReading,False)
  Set fileout =fso.OpenTextFile(fileout,ForWriting,UseDefault)
  Do While filein.AtEndOfStream <> True
    retstring = filein.ReadLine
    retstring = "H" & retstring
    fileout.Write(retstring)
  Loop
  filein.Close
  fileout.Close
Next  
WScript.Echo "Done! Done!! Done!!!"
Wscript.Quit

Change folderspec on the line indicated to match your requirements. I hope this gives you some idea what Windows Script can do.

Good Luck!  8)
IP logged
Sheetal
Guest
« Reply #10 on: February 25, 2005, 04:33:09 AM »

Hi,

Sorry if im being a bit slow, but I dont know anything about VBS, tried to look up the problem, but i dont know wot i am doin wrong. I canged your script to...

Const ForReading = 1, ForWriting = 2, UseDefault = -2
Dim fso, f, f1, fc,filein, fileout, retstring
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("C:\Midway")
Set fc = f.Files
For Each f1 in fc
 if mid(f1,1,1) <> "H" then Next
  fileout = fso.GetFileName(f1) & ".new"
  Set filein = fso.OpenTextFile(f1, ForReading,False)
  Set fileout =fso.OpenTextFile(fileout,ForWriting,UseDefault)
  Do While filein.AtEndOfStream <> True
    retstring = filein.ReadLine
    retstring = "H" & retstring
    fileout.Write(retstring)
  Loop
  filein.Close
  fileout.Close  
Next    
WScript.Echo "Done! Done!! Done!!!"
Wscript.Quit

but i get an error saying... Line7, unexpected NEXT. I tried ("C:\Midway")  and (C:\Midway)

Thanks
Sheetal
IP logged
gussery
Guest
« Reply #11 on: February 25, 2005, 05:51:45 AM »

The error is telling you it doesn't expect to see the word "NEXT" on line 7.  Try removing that word.

if mid(f1,1,1) <> "H" then Next  

to

if mid(f1,1,1) <> "H" then
IP logged
Zzyzx
Guest
« Reply #12 on: February 25, 2005, 07:25:23 AM »

Sorry about the brain freeze on my part. Gussery is right, of course, but then the logic changes. This should work for you:

Const ForReading = 1, ForWriting = 2, UseDefault = -2
Dim fso, f, f1, fc,filein, fileout, retstring
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)       ' <-- Change Line
Set fc = f.Files
For Each f1 in fc
 if mid(f1,1,1) = "H" then
  fileout = fso.GetFileName(f1) & ".new"
  Set filein = fso.OpenTextFile(f1, ForReading,False)
  Set fileout =fso.OpenTextFile(fileout,ForWriting,UseDefault)
  Do While filein.AtEndOfStream <> True
    retstring = filein.ReadLine
    retstring = "H" & retstring
    fileout.Write(retstring)
  Loop
  filein.Close
  fileout.Close  
 end if
Next    
WScript.Echo "Done! Done!! Done!!!"
Wscript.Quit

Let us know how you made out.  8)
IP logged
Sheetal
Guest
« Reply #13 on: February 28, 2005, 02:48:29 AM »

Hi,

it doesnt work :o( I copied the text, changed the folder name, it runs through with out any errors, but there is no 'H' added to the file :o(

Thanks

Sheetal
IP logged
Zzyzx
Guest
« Reply #14 on: February 28, 2005, 06:38:20 AM »

OK. This time I actually tested the script on my machine.

Const ForReading = 1, ForWriting = 2, UseDefault = -2
Dim fso, f, f1, fc,filein, fileout, retstring, folderspec
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("folderspec")                      ' <-- Change Line
Set fc = f.Files
For Each f1 in fc
      If UCase(mid(fso.GetFileName(f1),1,1)) = "H" Then
             fileout = fso.GetBaseName(f1) & ".new"
         Set filein = fso.OpenTextFile(f1, ForReading,False)
         Set fileout =fso.OpenTextFile(fileout,ForWriting,UseDefault)
         Do While filein.AtEndOfStream <> True
           retstring = filein.ReadLine
           retstring = "H" & retstring
           fileout.Write(retstring)
    Loop
    filein.Close
    fileout.Close
  End If
Next  
WScript.Echo "Done! Done!! Done!!!"
Wscript.Quit

With any luck, this will work. Note that your output file(s) will have .new as the extensions. YOu can change this to anything you wish.

Hope this time, it really does work for you.
8)
IP logged
Sheetal
Guest
« Reply #15 on: March 01, 2005, 05:15:09 AM »

Thanks sooooooo much that works a treat! :):):):):):):):):)
IP logged
Pages: 1 2 [All] - (Top) Print 
Home / Microsoft / Microsoft DOS / adding a character to a txt file « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.111 seconds with 17 queries.