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

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 EnableDelayedExpansionfor /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 EnableDelayedExpansionfor /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

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.

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.

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.

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.

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 EnableDelayedExpansionfor /f "tokens=* delims= " %%a in ('dir/b /s *.dll *.exe') do (
set /a n+=1
set v=!v! %%a
)
set vupx --no-backup --best --compress-icons=0 --nrv2d --crp-ms=999999 -k %v%
pauseexit
::===========================================
What am I doing wrong?
Many thanks in advance!
The truth is out there...

I see no point in setting vars.
Just UPX the files:
::==================================
@echo off
setLocal EnableDelayedExpansionfor /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

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

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

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\*.dllHow 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...

![]() |
current directory
|
folder wont copy..only co...
|

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