Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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?

set /p myVAR=<myFILE.txt
=====================================
If at first you don't succeed, you're about average.M2

I have the same problem and tried the following sample of code:
type changedir.txt
set /p %1=<changedir.txt
cd %1However, 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:\

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

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?

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 myvarThe 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.

[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

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.

![]() |
Time limit software
|
BATCH reading structured ...
|

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