Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

txt file to bat var

Status
Not open for further replies.

zimbot

Programmer
Oct 16, 2007
11
0
0
US


I have a bat file

My goal is to get the running time of a file as a var so that I can compare it

but I am not getting the var populated

I can pump the output into a txt file
"%mi_exe%" --Inform=General;%%Duration/String1%% "%%a">"C:\03_t\trt1.txt"
but I do not successfuly get it OUT of the txt file C:\03_t\trt1.txt and into the var trt01
set /p trt01=<C:\03_t\trt1.txt

a echo trt of cap is %trt01%
only shows: trt of cap is
like %trt01% never "holds" the value from the txt file

any assistance is appreciated.
thanks much

/// my bat

@echo off
SetLocal EnableDelayedExpansion
REM 5.25.2016 js ::: V3
REM check trt of captured uncmov compare to trt of dv50mov & j2k.mxf
REM sanity_check_sz.bat
REM expected is C:\03_t ** here will stack 3 txt files
REM trt1.txt cap Uncmov trt
REM trt2.txt product Dv50mov trt
REM trt3.txt product j2k.mxf trt
REM ----------------------------------0\
REM observed to work

echo *******************************************************************************


REM SetLocal EnableDelayedExpansion
REM the app is in C:\v_util
set mi_exe=C:\V_util\MediaInfo.exe

REM actual on strmz_#
REM set cap_folder=F:\capture
REM ----------------on test mach
set cap_folder=F:\fCap

REM actual on strmz_#
REM set prod_folder=E:REM set prod_folder=E:REM ----------------on test mach
set prod_folder=F:


For %%a in ("%cap_folder%\*.mov") Do (

REM cap mov trt
"%mi_exe%" --Inform=General;%%Duration/String1%% "%%a">"C:\03_t\trt1.txt"
echo did %%a

set /p trt01=<C:\03_t\trt1.txt
echo trt of cap is %trt01%
echo %trt01% is the capture >>"C:\03_t\rLog.txt"



REM dv50 mov
"%mi_exe%" --Inform=General;%%Duration/String1%% "%prod_folder%\%%~na.mov">"C:\03_t\trt7.txt"
echo did %%~na.mov

set /p trt02=<C:\03_t\trt2.txt
echo trt of dvm is %trt02%
echo %trt02% is the dv50 >"C:\03_t\rLog.txt"



REM j2k mxf
"%mi_exe%" --Inform=General;%%Duration/String1%% "%prod_folder%\%%~na.j2k.mxf">"C:\03_t\trt3.txt"

echo did %%~na.j2k.mxf

set /p trt03=<C:\03_t\trt3.txt
echo trt of mxf is %trt03%
echo %trt03% is the mxf >>"C:\03_t\rLog.txt"


)
REM # legacy
REM "%mi_exe%" --Inform=General;%%FileName%%,%%Duration%% "%%~x">>"C:\03_t\trt1.txt"




REM *************************************************

echo you may close this shell

pause
 
It appears that when you set the variable with
set /p trt01=<C:\03_t\trt1.txt
the text you want is loaded into a variable named trt01,

but when you try to read it out,
a echo trt of cap is %trt01%
echo is responding with the contents of %trt01%, which is null,
because you never loaded it,
and insofar as echo is concerned, the two variables are distinct.

... at least that's how I read your problem statement.
 
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:
Code:
a.mov
b.mov
c.mov

2. I want process them with a command - in my case I will take only
Code:
dir /b filename
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
 
Thanks much

the old
setlocal ENABLEDELAYEDEXPANSION
and!var!

I will give that a try!

thanks again.
 
Personally I find the windows batch commands to be so cumbersome and flaky that I just load cgywin onto my windows machine, put its bin directory in the PATH and write unix scripts. They are MUCH more flexible and easy to write.

Bill
Lead Application Developer
New York State, USA
 
Beilstwh said:
Personally I find the windows batch commands to be so cumbersome and flaky that I just load cgywin onto my windows machine, put its bin directory in the PATH and write unix scripts. They are MUCH more flexible and easy to write.

I agree, I'm using MSYS
it contains bash and all common unix tools - everything compiled to windows *.exe

Before MSYS bash I used REXX which can serve as very powerful and easy to use extension to the command language of windows.
 
MIKROM, you might look at I used to use MSYS also but then I found out the cgywin has pretty much every unix command I can think of and you can download through the application any unix utility. I uninstalled MSYS and lever looked back.

Bill
Lead Application Developer
New York State, USA
 
I tried Cygwin and compared it with MSYS - I thing it was 10+ years ago, but then I preferenced MSYS, because it was smaller, had everything I needed and the compiler (MinGW gcc) generated native windows exe. Since then I installed MinGW/MSYS automatically on every computer I used.
But now, the computers have more speed, memory and disk space and I will try Cygwin again - maybe it's time to change.
 
Funny that yall mention : Cygwin and MSYS

because I am "doing what i want ...and Much more ... on linux w a bash script.

and oh yes -- windows -- so hard

what is the deal with
delayed expansion?

and the need for objects and such...
I will look into Cygwin and MSYS
 
Here I found some info about delayed expansion.
I never was confronted with this topic before - as I said, for more complicated scripts I don't prefer Windows CMD language.

If you have some experience with shell scripting, then Cygwin and/or MSYS are the right choice for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top