Computing.Net > Forums > Programming > Creating a variable from a txt file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Creating a variable from a txt file

Reply to Message Icon

Name: theotherguy
Date: May 14, 2008 at 05:18:54 Pacific
OS: WinXP sp3
CPU/Ram: P43.0G 1GB
Comment:

Hi

I want to make a batch file that creates variables by parsing a .txt file made from a "dir /b /n > dirlist.txt" command. Each line in the .txt file is a single filename.

I need each line/filename to be a new variable which I can use as part of the syntax for the program i'm trying to run:

program.exe targetfile %1 %2 %3

Using the above works if I just drag & drop the files onto the .bat, but they aren't in order as required, that is the reason the i've used the dir command to create the file.

I found that using:
FOR /F "USEBACKQ DELIMS==" %%I IN (a.txt) DO (SET var=!%%I)
It sets the variable but only holds the last line in the file.

I've also tried: FOR /F "tokens=1,2*" %%I IN (a.txt) DO SET var=%%I %%J %%K
Which works great (using dir /w) until "a.txt" has more than one line. If there's a way to adjust the file via a batch command to make all the text only on the first line separating the filenames with a space/tab, then it would work fine.

Am I even on the right track?



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: May 14, 2008 at 06:21:22 Pacific
Reply:

The /n in dir/b/n isn't doing anything useful.
But this will set s var to each filename in myfile, incrementing: v1, v2, ... vn

::==========================
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (myfile) do (
set /a n+=1
set v!n!=%%a
)
set v

::==============================

You can nevermind the file creation:

::==================================
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b') do (
set /a n+=1
set v!n!=%%a
)
set v


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

M2


0

Response Number 2
Name: klint
Date: May 14, 2008 at 10:59:53 Pacific
Reply:

As M2 said, and if you also need to get all the filenames into a single line, replace

set v!n!=%%a

with

set v=!v! %%a

Then run your command line as

program.exe targetfile %v%

Finally, yet another way to do it would be to use the GNU xargs utility, available for download from the gnuwin32 website.


0

Response Number 3
Name: theotherguy
Date: May 15, 2008 at 01:17:46 Pacific
Reply:

Thanks for the help, I have been reading up on delayed variable expansion and thought i was heading on the right track but just didn't know how to code it correctly.

The variation to Mechanix2Go's code that Klint provided works exactly as I need, as I can add the one variable to the program syntax and it includes all the files as required.

So from what I can understand:

set /a n+=1

is to change the variable suffix by 1 each time a new line is parsed from the input.

and; set v=!v! %%a

adds each new line to the current variable each time.

Thanks very much.


0

Response Number 4
Name: klint
Date: May 15, 2008 at 04:00:03 Pacific
Reply:

By the way, there is a limit to the maximum length of a variable, which may be exceeded if you have too many files. This limit depends on OS version, service pack etc. It's reasonably large, but you should do some testing to find out if it is sufficient for your needs.


0

Response Number 5
Name: theotherguy
Date: May 15, 2008 at 04:12:00 Pacific
Reply:

There shouldn't ever be any more than about 10 files, so I doubt that should be a problem.

Just found a small problem, but realised it can be fixed simply adding the /OD switch to the dir command.


0

Related Posts

See More



Response Number 6
Name: deschman
Date: July 17, 2008 at 05:28:35 Pacific
Reply:

I have a similar problem.

I want to compress all dll and exe files in all subdirectories using UPX.

I used the code presented here but it seems to do nothing...

::==========================================
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b /s *.dll *.exe') do (
set /a n+=1
set v=!v! %%a
)
set v

upx --no-backup --best --compress-icons=0 --nrv2d --crp-ms=999999 -k %v%
pause

exit

::===========================================

What am I doing wrong?

Many thanks in advance!

The truth is out there...


0

Response Number 7
Name: Mechanix2Go
Date: July 17, 2008 at 05:56:20 Pacific
Reply:

I see no point in setting vars.

Just UPX the files:

::==================================
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b /s *.dll *.exe') do (
upx --no-backup --best --compress-icons=0 --nrv2d --crp-ms=999999 -k %%a
)


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

M2


0

Response Number 8
Name: deschman
Date: July 17, 2008 at 06:07:43 Pacific
Reply:

You are right i could do so. And it works fine but it is more a kind of cosmetic problem.

When I give all files at once as arguments UPX will be started just one time otherwise it will start for each file separately. It works but it looks less nice in the DOS-Window...

Any suggestions about that?

The truth is out there...


0

Response Number 9
Name: klint
Date: July 18, 2008 at 14:27:51 Pacific
Reply:

What is the output of your "set v" command (just before the upx command)? Does it show that v contains all the files you are interested in?

One problem I can imagine you may have is if there are file names with embedded spaces. In that case, you'll need to change the command to set v=!v! "%%a".


0

Response Number 10
Name: deschman
Date: July 22, 2008 at 02:50:36 Pacific
Reply:

I guess the problem is the one earlier mentioned. There are to much files I want to give as arguments. Wat exactly is the limitation?

One idea that comes in my mind is to make a list of directories including the file filter and not of the files. Something like this:
X:\dir\subdir1\*.exe X:\dir\subdir1\*.dll X:\dir\subdir2\*.exe X:\dir\subdir2\*.dll

How can I code this?

Another poosible solution (kind of compromise) is to start UPX once per subdir. Then still I need a list of subdirs.

The truth is out there...


0

Sponsored Link
Ads by Google
Reply to Message Icon

current directory folder wont copy..only co...



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: Creating a variable from a txt file

search for 3 files and create a .txt file www.computing.net/answers/programming/search-for-3-files-and-create-a-txt-file/19141.html

batch to edit a txt file www.computing.net/answers/programming/batch-to-edit-a-txt-file/15327.html

select random no.s from a txt file www.computing.net/answers/programming/select-random-nos-from-a-txt-file/18580.html