Hello zimbot,
When I understood you right, then this problem should be as follows:
1. I have in the direcory some *.mov file - for example in my case:
2. I want process them with a command - in my case I will take only
which returns simply the file name
3. I want redirect the output result of the command in a file named
trt1.txt
and then in the script I want to set from the contetnt of this file a variable named
trt01
If I do that individually for every of the files it works as expected:
example_01.cmd
Code:
@[COLOR=#008080]echo[/color][COLOR=#804040][b] off[/b][/color]
[COLOR=#008080]rem[/color][COLOR=#0000ff] THIS IS WORKING[/color]
[COLOR=#008080]rem[/color][COLOR=#0000ff] redirect cmd output into file[/color]
[COLOR=#008080]dir[/color] [COLOR=#6a5acd]/b[/color] a.mov > trt1.txt
[COLOR=#008080]rem[/color][COLOR=#0000ff] rem set the variable value from the file[/color]
[COLOR=#008080]set[/color] [COLOR=#6a5acd]/p[/color][COLOR=#008080] trt01[/color][COLOR=#804040][b]=[/b][/color] < trt1.txt
[COLOR=#008080]echo[/color][COLOR=#ff00ff] variable trt01 = [/color][COLOR=#008080]%trt01%[/color]
[COLOR=#008080]rem[/color][COLOR=#0000ff] redirect cmd output into file[/color]
[COLOR=#008080]dir[/color] [COLOR=#6a5acd]/b[/color] b.mov > trt1.txt
[COLOR=#008080]rem[/color][COLOR=#0000ff] rem set the variable value from the file[/color]
[COLOR=#008080]set[/color] [COLOR=#6a5acd]/p[/color][COLOR=#008080] trt01[/color][COLOR=#804040][b]=[/b][/color] < trt1.txt
[COLOR=#008080]echo[/color][COLOR=#ff00ff] variable trt01 = [/color][COLOR=#008080]%trt01%[/color]
[COLOR=#008080]rem[/color][COLOR=#0000ff] redirect cmd output into file[/color]
[COLOR=#008080]dir[/color] [COLOR=#6a5acd]/b[/color] c.mov > trt1.txt
[COLOR=#008080]rem[/color][COLOR=#0000ff] rem set the variable value from the file[/color]
[COLOR=#008080]set[/color] [COLOR=#6a5acd]/p[/color][COLOR=#008080] trt01[/color][COLOR=#804040][b]=[/b][/color] < trt1.txt
[COLOR=#008080]echo[/color][COLOR=#ff00ff] variable trt01 = [/color][COLOR=#008080]%trt01%[/color]
Output:
Code:
c:\Work>example_01.cmd
variable trt01 = a.mov
variable trt01 = b.mov
variable trt01 = c.mov
Now I will try it in a loop:
example_02.cmd
Code:
@[COLOR=#008080]echo[/color][COLOR=#804040][b] off[/b][/color]
[COLOR=#804040][b]for[/b][/color] [COLOR=#6a5acd]%%[/color]a in (*.mov) Do (
[COLOR=#008080]rem[/color][COLOR=#0000ff] print file name[/color]
[COLOR=#008080]echo[/color][COLOR=#ff00ff] file found = [/color][COLOR=#6a5acd]%%[/color][COLOR=#ff00ff]a[/color]
[COLOR=#008080]rem[/color][COLOR=#0000ff] THIS DOES NOT WORK ! WHY ?[/color]
[COLOR=#008080]rem[/color][COLOR=#0000ff] redirect cmd output into file[/color]
[COLOR=#008080]dir[/color] [COLOR=#6a5acd]/b[/color] [COLOR=#6a5acd]%%[/color]a > trt1.txt
[COLOR=#008080]rem[/color][COLOR=#0000ff] set the variable value from file[/color]
[COLOR=#008080]set[/color] [COLOR=#6a5acd]/p[/color][COLOR=#008080] trt01[/color][COLOR=#804040][b]=[/b][/color] < trt1.txt
[COLOR=#008080]echo[/color][COLOR=#ff00ff] variable trt01 = [/color][COLOR=#008080]%trt01%[/color]
)
Output is
Code:
c:\Work>example_02.cmd
file found = a.mov
variable trt01 = c.mov
file found = b.mov
variable trt01 = c.mov
file found = c.mov
variable trt01 = c.mov
We see that it's not working.
It's very strange: the variable in the loop will not set correctly.
The solution fo this problem seems to be to use in the batch script
Code:
setlocal ENABLEDELAYEDEXPANSION
and then instead of this variable expansion convention
%trt01% use this convention
!trt01!
Here is the working example
example_03.cmd
Code:
@[COLOR=#008080]echo[/color][COLOR=#804040][b] off[/b][/color]
[COLOR=#008080]setlocal[/color] ENABLEDELAYEDEXPANSION
[COLOR=#804040][b]for[/b][/color] [COLOR=#6a5acd]%%[/color]a in (*.mov) Do (
[COLOR=#008080]rem[/color][COLOR=#0000ff] print file name[/color]
[COLOR=#008080]echo[/color][COLOR=#ff00ff] file found = [/color][COLOR=#6a5acd]%%[/color][COLOR=#ff00ff]a[/color]
[COLOR=#008080]rem[/color][COLOR=#0000ff] THIS IS WORKING[/color]
[COLOR=#008080]rem[/color][COLOR=#0000ff] redirect cmd output into file[/color]
[COLOR=#008080]dir[/color] [COLOR=#6a5acd]/b[/color] [COLOR=#6a5acd]%%[/color]a > trt1.txt
[COLOR=#008080]rem[/color][COLOR=#0000ff] set the variable value from file[/color]
[COLOR=#008080]set[/color] [COLOR=#6a5acd]/p[/color][COLOR=#008080] trt01[/color][COLOR=#804040][b]=[/b][/color] < trt1.txt
[COLOR=#008080]echo[/color][COLOR=#ff00ff] variable trt01 = [/color][COLOR=#008080]!trt01![/color]
)
Now, the output is as I needed:
Code:
c:\Work>example_03.cmd
file found = a.mov
variable trt01 = a.mov
file found = b.mov
variable trt01 = b.mov
file found = c.mov
variable trt01 = c.mov