Creating argument from bat, problem

April 21, 2009 at 07:07:53
Specs: Windows Vista
I'm using the following batch file to automatically create an argument for a program from user input and a filename listing using the DIR command.

Set /P output=Enter filename:
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in ('dir/b/OD *.txt') do (
set /a n+=1
set v=!v! %%a

program.exe %output%.txt %v%

I've had to change the batch since the program has file size restrictions. I found out that the copy /b command does the same with no restrictions.

I've revised the batch, to work with the copy command by adding a "+" in line 5:

set v=!v! %%a +

As the copy command uses "+" to add files, the revised batch file with a "+" added has one extraneous "+" at the end, causing the batch file to fail.

The output ends up as file1 + file2 + file3 +, instead of file1 + file2 + file3.

Is it possible to delete the last "+" within the batch file or redirecting the output to a file then automatically deleting it from that file, then using that file as input for the variable to create the argument.

Or am I going about this the long way?

See More: Creating argument from bat, problem

Report •

April 21, 2009 at 12:34:48

set V=%V:~0,-1%

Report •

April 22, 2009 at 05:39:46
Thanks, that's exactly what I was looking for.

Can you explain what the relevance is of the "~0"?

After playing around with it, it seems to drop any characters from the start of the variable.

Report •

April 22, 2009 at 06:29:18
The statement I posted is an instance of the substring manipulation facility of NT batch scripting. Its genaral format is


where m is the position of the substring (starting from 0) and n the lenght. if negative the lenght is computed in reverse way, starting from the end. The position too may be omitted leading to interesting tricks. Another substring expression is


where any occurence of string1 in the variable is replaced by string2. The statements apply to delayed variables too.

To know more type at prompt Set /?.

Report •

Related Solutions

Ask Question