Set multiple lines from file.txt to variables and Echo them

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
regaw_leinad
Posts: 4
Joined: 26 Mar 2010 14:25

Set multiple lines from file.txt to variables and Echo them

#1 Post by regaw_leinad » 08 Sep 2010 20:30

Hey,
I am working on a project for the guys at xda-developers. I need help with one part of it, I've been at this part all day and have come up with nothing.

I have a txt file with a variable amount of lines in a certain directory that I want to read each line
(each line has no spaces, just a list of files, ie
com.htc.bookmarks.apk
WifiRouter.apk
*.*
and so on
)

I need to be able to grab all of these lines, store each line in a separate variable (a single letter),
echo something like this:

A. com.htc.bookmarks.apk
B. WifiRouter.apk
C. *.*
and so on

and then I'm going to have:

set /P choice=What Letter?
and depending on what letter they choose, it will delete that file from a certain directory.

So if they choose "A"
it will
del "%CD%\blah\blah\com.htc.bookmarks.apk"

As I'm typing this, I'm realizing that this probably isn't possible lol

Can I get any help?

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Set multiple lines from file.txt to variables and Echo t

#2 Post by ghostmachine4 » 08 Sep 2010 20:55

regaw_leinad wrote:As I'm typing this, I'm realizing that this probably isn't possible lol

yes its possible with batch/vbscript. But i offer an alternative solution.
If you have no restriction on the language used, here's Python script you can try

Code: Select all

import sys
import os
inputfile=sys.argv[1]
certain_directory=os.path.join("c:\\","path")
choices={}
for n,line in enumerate(open(inputfile)):
    choices[str(n+1)]=line.rstrip()
for k in sorted(choices.keys()):
    print k , "->", choices[k]
c=raw_input("Which one to delete ?" )
delete_file=os.path.join(certain_directory,choices[c])
d=raw_input("Delete %s [yn]? "% delete_file )
if d.lower() in ['yes','y']:
    try:
        os.remove(delete_file)
    except OSError,e:
        print e
    else:
        print "remove ok"


save as myscript.py and on command line

Code: Select all

c:\test> python myscript.py myfile

regaw_leinad
Posts: 4
Joined: 26 Mar 2010 14:25

Re: Set multiple lines from file.txt to variables and Echo t

#3 Post by regaw_leinad » 08 Sep 2010 20:58

I do see what you mean with that one, but unfortunately, this is one small part of my program, that is all in batch. I just can't figure out for the life of me how to code it!

Post Reply