1.

Solve : Editing a file [automatically] through batch file??

Answer»

This is probably a very very noob question 
But is is possible to change / insert TEXT in a TXT file through a bat file?

This is then picked up by another program for processing.

I looked at the EDIT function but this opens up a notepad like window in the command screen.

danielplease give example of what you would like to do
I would like to have a file wich is re-written every time the BAT is started.

For example:
File --> C:\info.txt
inside is a line like --> 14-02-2008

Now when I run the BAT tomorrow it must be rewritten to 15-02-2008

( this is purely an example, my textline will be different.....)I understand. You will need the > symbol and the >> symbol.

This symbol > creates a file new each time, any old file with same name is deleted

echo %date% > c:\info.txt



These 2 symbols >> create a file only if it does not yet exist. If it already exists a new line is added at the end.

echo %date% >> c:\info.txt
How would you get the time too?For Time

Echo %time% >C:\info.txtThe "TYPE > my_text.txt" will pipe or redirect the output to my_text.txt, overwriting the original.
The "TYPE >> my_text.txt" will pipe or redirect the output to my_text.txt, appending the original. The original test will still be there, plus the new.

I don't think that either is what you want.

In order to modify the text file, it would be necessary to rewrite the whole file each time.
I've been working on a possible solution, but can't get it to work yet.It would be possible to do some kind of word-processing / text-editing type things to a file fairly simply, such as search & replace, change case, DELETION, insertion, etc.

Linux / Unix has had utilities such as grep and sed for a long time, and they are available for the Windows command line too.


Quote from: Dias de verano on February 16, 2008, 10:43:52 AM

It would be possible to do some kind of word-processing / text-editing type things to a file fairly simply, such as search & replace, change case, deletion, insertion, etc.

How would one go about deleting (not ignoring) the first line of a text file using DOS?Using the "skip" setting in the FOR command, copy the file to another temp one, skipping the first line. Then delete the original file, and rename the temp file with the original file name.

Here's what I got so far...and it's not working...Not sure what I'm doin wrong:

For /F "skip=1" %%x in ("i:\shared_retention\ccbatch\ccbatch.txt") do copy %%x "i:\shared_retention\ccbatch\ccbatch2.txt"

am I WAY off? Quote from: dvnmaya on March 03, 2008, 02:56:39 PM
Here's what I got so far...and it's not working...Not sure what I'm doin wrong:

For /F "skip=1" %%x in ("i:\shared_retention\ccbatch\ccbatch.txt") do copy %%x "i:\shared_retention\ccbatch\ccbatch2.txt"

am I way off?

Not far off really. You need ECHO, not COPY. The lines starting REM are REMarks
and do not affect the operation of the batch file and can be removed if you want,
as can the blank lines.

I have PLACED the SOURCE and destination file names into variables to make the
code a bit clearer but you can code them literally if you want, of course

This should work...

Code: [Select]
set file1="i:\shared_retention\ccbatch\ccbatch.txt"
set file2="i:\shared_retention\ccbatch\ccbatch2.txt"

REM if the target file already exists, delete it
if exist %file2% del %file2%

REM Now the >> redirection will create the file the first time it is used
REM to echo a line and append each line subsequently

For /F "skip=1" %%x in (%file1%) do echo %%x >> %file2%



Discussion

No Comment Found