1.

Solve : "Set" in batch file?

Answer»

I need to set two environment variables: test_input and test_output

set test_input=..\..\testfiles\test0.txt
set test_output=..\..\temptest\out.%test_input:..\..\testfiles\=%

This works very well under command prompt and in a batch file only with these two lines. But when I
PUT it into one of my batch file, like

if %test_exefile%==repack (
set test_input=%1 :: this line works fine and %1 will set to .\..\testfiles\test0.txt
set test_output="..\..\temptest\out.%test_input:..\..\testfiles\=%
...
)

The first set line works fine. But the second variable (test_output) will set to ..\..\temptest\out...\..\testfiles\=

I also tried to replace the second set with the
set test_output="..\..\temptest\out.%test_input:~16%"
Use this, I will set test_output="..\..\temptest\out.~16", but it works under command prompt.

Anyone can give any kind of helpful thoughts? Thanks in advance!



I do not know a lot about batch files, but I think temptest is too long a directory name.I thought that was the case. So I tried to put only those two lines into a batch file. It works.
There might be some other REASONS I don't knowMy thoughts are that the punctuation is in error:

Code: [Select]f %test_exefile%==repack (
set test_input=%1
set test_output="..\..\temptest\out.%test_input%:..\..\testfiles\=%
)

I'm not sure you really want the leading quote or the trailing % on test_output. But hey, it's your code. For what it's worth, why the set test_input=%1? Why not just use %1 directly in the set test_output?

Just my 2 cents. 8-)

Hi, expert

It's possible to use %1 to replace test_input.

If %1 is ..\..\testfiles\test0.txt and I want to set test_output is ..\..\temptest\out.test0.txt

Use environment variables, we can set test_output=out.%test_input:..\..\testfiles=%. This will remove ..\..\testfiles in %test_input%.

But if I use %1, I don't know how can I do it? Expecting your idear?
Based on your code:
Code: [Select]test_input=..\..\testfiles\test0.txt
test_output="..\..\temptest\out.%test_input:..\..\testfiles\=%

There is nothing to replace. You are TRYING to RESOLVE a variable named %test_input:..\..\testfiles\=% which doesn't exist.

If you move the %, something like this: %test_input%:..\..\testfiles\=, a replacement will be made although I'm not sure that's the result you want. Environment variables are strings resolved to their actual value and are only recognized by the surrounding %. The interpreter will not see %test_input as a replacable parameter.

What result are you looking for? 8-)



Discussion

No Comment Found