Computing.Net > Forums > Programming > Simple Batch ? - Txt File to Var

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Simple Batch ? - Txt File to Var

Reply to Message Icon

Name: NickCherryJiggz
Date: July 17, 2006 at 17:34:56 Pacific
OS: Windows XP
CPU/Ram: various
Product: Dell Optiplex GX 1 - 270
Comment:

I apologize for such an elementary question, but I can't seem to find how to accomplish this basic task...

I have a txt file that contains one line of text about 10 characters long. How do I store these 10 characters into a variable?



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: July 17, 2006 at 21:28:11 Pacific
Reply:

set /p myVAR=<myFILE.txt


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 2
Name: cmb124
Date: July 19, 2006 at 09:02:29 Pacific
Reply:

I have the same problem and tried the following sample of code:

type changedir.txt
set /p %1=<changedir.txt
cd %1

However, it did not work (output below). Could anybody point out what I am doing wrong? I am sure that it is something stupid but I just cannot see it.

This is the output I got:
D:\>type changedir.txt
D:\HOME\

D:\>set /p = 0<changedir.txt

D:\>cd
D:\


0

Response Number 3
Name: Mechanix2Go
Date: July 19, 2006 at 19:44:15 Pacific
Reply:

As posted, your script needs a command line parameter, like:

mybat abc

Apparently you are not giving it a parameter, because it's missing in this line:

set /p = 0<changedir.txt

I don't know where you're going with this. If you just want to change to the dir named by the first parameter, you can skip the TYPE & SET.


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 4
Name: cmb124
Date: July 20, 2006 at 00:31:29 Pacific
Reply:

Thank you for your reply, but I am afraid that I cannot do not fully understand your response.

1. You mentioned command line parameters. As far as I understand they are %1 to %9. For your example, I understand that if you do this from command prompt "mybat abc", abc will become %1. Am I correct?

2. I put the TYPE command to just show you what is in the file. As from the program output, it contains the text D:\HOME\

3. You said that I am not giving it a parameter, but I used "set /p %1=<changedir.txt" exactly as you suggested in your last mail. "set /p = 0<changedir.txt" is the output I got when running the bat file.

4. Yes, you are correct. My aim is to change the directory to the contents in the file called changedir.txt. I have to do this because I am running a VB script and it cannot change the cmd directory, so I have to output it to a file and use a bat file to make it happen. I wonder if there is a way to go about it please?


0

Response Number 5
Name: cmb124
Date: July 20, 2006 at 01:13:50 Pacific
Reply:

By the way, this is what I get when use myvar instead of %1. The bat file:

type changedir.txt
set /p myvar=<changedir.txt
cd myvar

The screen output:

D:\>type changedir.txt
D:\dev\

D:\>set /p myvar= 0<changedir.txt

D:\>cd myvar
The system cannot find the path specified.


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: July 20, 2006 at 01:18:33 Pacific
Reply:

[1] yes

[2] OK

If you run:

mybat abc

then the variable abc will be set to the contents of the file changedir.txt

If you don't want to give it a parameter, you can:

set /p myVAR=<changedir.txt
cd %myVAR%

Note that if you want to change DRIVE and directory [I suppose you do.] you need:

cd /d %myVAR%


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 7
Name: cmb124
Date: July 20, 2006 at 01:23:44 Pacific
Reply:

It all makes sense now. Thank you very much for your help. I did not realise that you have to surround the variable in % when you use it in a command.


0
Reply to Message Icon

Time limit software BATCH reading structured ...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Simple Batch ? - Txt File to Var

Move 1.txt file to other folder and www.computing.net/answers/programming/move-1txt-file-to-other-folder-and/16588.html

Batch - Copy Files using .txt List www.computing.net/answers/programming/batch-copy-files-using-txt-list/18010.html

batch to rename txt file using data inside www.computing.net/answers/programming/batch-to-rename-txt-file-using-data-inside/19709.html