Need help creating a .bat file moving output into a variable


  1. Posts : 51
    Windows 7 Home Premium 64bit
       #1

    Need help creating a .bat file moving output into a variable


    Hello everyone,

    I am looking to create a .bat file to automate task that I need to do about a thousand times. I have some experience with .bat files, however I would still consider myself a beginner. I also didn't really see a good forum for something like this so [Moderators] if it need to be moved I understand. Currently I am running windows 7 x64 SP1 on a HP pavilion a1657c.

    I am using this program
    http://www.makemkv.com/developers/usage.txt
    and with the command "makemkvcon -r info" will get an output of my DVD drive that looks like this

    Code:
    C:\>makemkvcon -r info
    MSG:1005,0,1,"MakeMKV v1.6.16 win(x86-release) started","%1 started","MakeMKV v1
    .6.16 win(x86-release)"
    MSG:1004,0,1,"Debug logging enabled, log will be saved as C:/MakeMK
    V_log.txt","Debug logging enabled, log will be saved as %1","C:/Mak
    eMKV_log.txt"
    DRV:0,1,1,1,"DVD+R-DL HL-DT-ST DVD-RAM GH22NP20 1.04","HOLIDAY_MOVIES_1","\\Device\\
    CdRom0"
    DRV:1,0,0,0,"","",""
    DRV:2,0,0,0,"","",""
    DRV:3,0,0,0,"","",""
    DRV:4,0,0,0,"","",""
    DRV:5,0,0,0,"","",""
    DRV:6,0,0,0,"","",""
    DRV:7,0,0,0,"","",""
    DRV:8,0,0,0,"","",""
    DRV:9,0,0,0,"","",""
    DRV:10,0,0,0,"","",""
    DRV:11,0,0,0,"","",""
    DRV:12,0,0,0,"","",""
    DRV:13,0,0,0,"","",""
    DRV:14,0,0,0,"","",""
    DRV:15,0,0,0,"","",""
    Use: makemkvcon [switches] Command [Parameters]
    
    Commands:
      info <source>
          prints info about disc
      mkv <source> <title id> <destination folder>
          saves a single title to mkv file
      stream <source>
          starts streaming server
      backup <source> <destination folder>
          backs up disc to a hard drive
    
    Source specification:
      iso:<FileName>    - open iso image <FileName>
      file:<FolderName> - open files in folder <FolderName>
      disc:<DiscId>     - open disc with id <DiscId> (see list Command)
      dev:<DeviceName>  - open disc with OS device name <DeviceName>
    
    Switches:
      -r --robot        - turn on "robot" mode, see http://www.makemkv.com/developer
    s
    In this output the title holiday movies 1 is what I want to use to create a new folder that has the same name as this. If I can do this then when I convert my DVD I can save it to this new folder. Right now I can go through a GUI which is slow, or I can use the command line interface but have to specify a destination folder that is named correctly before I convert.

    Thank you for any help you can give me even if it is just a link to another web site with help in creating bat files. I posted to sevenforums as I have gotten a lot of help with windows 7 in the past (the tutorials are really good).

    -S
      My Computer


  2. Posts : 450
    Windows 7
       #2

    Direct cut/paste from working .bat script....

    You have to delete all the leading .. that I added so it would indent correctly to be readable.

    ***********************************************

    @ECHO OFF
    SETLOCAL ENABLEDELAYEDEXPANSION

    REM for testing, I used a static variable here
    REM if cut/pasting this be sure to rejoin these 2 lines to make them 1 line
    SET var0=DRV:0,1,1,1,"DVD+R-DL HL-DT-ST DVD-RAM GH22NP20 1.04","HOLIDAY_MOVIES_1","\\Device\\CdRom0"

    REM process each line of output from command


    REM You will replace ("!var0!") below with ('your command')


    FOR /F "TOKENS=*" %%a IN ("!var0!") DO (
    ..REM save in local variable for easier complex DOS script lines
    ..SET vara=%%a

    ..REM parse 1st value before first colon for this output line
    ..FOR /F "TOKENS=1 DELIMS=:" %%f IN ("!vara!") DO (

    ....REM if drv then we have something to examine
    ....IF /I %%f == drv (

    ......REM replace double-quote with @ for parsing
    ......SET varanq=!vara:"=@!

    ......REM get 1st chunk after first @
    ......FOR /F "TOKENS=2 DELIMS=@" %%h IN ("!varanq!") DO (

    ........SET var=%%h
    ........REM parse 3 bytes from beginning which is just after @
    ........SET var2=!var:~0,3!

    ........REM DVD? note slashI option to ignore case
    ........IF /I !var2! == dvd (

    ..........REM parse 6th chunk comma-delimited
    ..........FOR /F "TOKENS=6 DELIMS=," %%t IN ("!varanq!") DO (

    ............REM now remove @s on each end
    ............FOR /F "TOKENS=1 DELIMS=@" %%y IN ("%%t") DO (

    ..............SET mydir=%%y
    ..............ECHO final !mydir!
    ............)
    ..........)
    ........)
    ......)
    ....)
    ..)
    )
    Last edited by JimLewandowski; 16 Dec 2011 at 14:17.
      My Computer


  3. Posts : 51
    Windows 7 Home Premium 64bit
    Thread Starter
       #3

    Thanks for the quick reply. So I copied this into a bat file and get an error. Maybe I copied it incorrectly or should put it into the bat file differently?

    Here is my bat
    Code:
    ::mkvbat
    Path="C:\Program Files (x86)\MakeMKV"
    makemkvcon -r info
    
    FOR /F %%a IN ('makemkvcon -r info') DO (
    REM save in local variable for easier complex DOS script lines
    ..SET vara=%%a
    
    ..REM parse 1st value before first colon for this output line
    ..FOR /F "TOKENS=1 DELIMS=:" %%f IN ("!vara!") DO (
    
    ....REM if drv then we have something to examine 
    ....IF /I %%f == drv (
    
    ......REM replace double-quote with @ for parsing
    ......SET varanq=!vara:"=@!
    
    ......REM get 1st chunk after first @ 
    ......FOR /F "TOKENS=2 DELIMS=@" %%h IN ("!varanq!") DO (
    ........SET var=%%h
    
    ........REM parse 3 bytes from beginning which is just after @
    ........SET var2=!var:~0,3!
    
    ........REM DVD? note slashI option to ignore upper/lower case
    ........IF /I !var2! == dvd (
    
    ..........REM parse 6th chunk comma-delimited
    ..........FOR /F "TOKENS=6 DELIMS=," %%t IN ("%%p") DO (
    
    ............REM now remove @s on each end
    ............FOR /F "TOKENS=1 DELIMS=@" %%y IN ("%%t") DO (
    ..............SET mydir=%%y 
    ............)
    ..........)
    ........)
    ......)
    ....)
    ..)
    )
    And I get the following error
    Code:
    C:\Users\Kira\Desktop>makemkvcon -r info
    MSG:1005,0,1,"MakeMKV v1.6.16 win(x86-release) started","%1 started","MakeMKV v1
    .6.16 win(x86-release)"
    MSG:1004,0,1,"Debug logging enabled, log will be saved as C:\\Users\\Kira/MakeMK
    V_log.txt","Debug logging enabled, log will be saved as %1","C:\\Users\\Kira/Mak
    eMKV_log.txt"
    DRV:0,1,1,1,"DVD+R-DL HL-DT-ST DVD-RAM GH22NP20 1.04","HOLIDAY_MOVIES_1","\\Device\\
    CdRom0"
    DRV:1,0,0,0,"","",""
    DRV:2,0,0,0,"","",""
    DRV:3,0,0,0,"","",""
    DRV:4,0,0,0,"","",""
    DRV:5,0,0,0,"","",""
    DRV:6,0,0,0,"","",""
    DRV:7,0,0,0,"","",""
    DRV:8,0,0,0,"","",""
    DRV:9,0,0,0,"","",""
    DRV:10,0,0,0,"","",""
    DRV:11,0,0,0,"","",""
    DRV:12,0,0,0,"","",""
    DRV:13,0,0,0,"","",""
    DRV:14,0,0,0,"","",""
    DRV:15,0,0,0,"","",""
    Use: makemkvcon [switches] Command [Parameters]
    
    Commands:
      info <source>
          prints info about disc
      mkv <source> <title id> <destination folder>
          saves a single title to mkv file
      stream <source>
          starts streaming server
      backup <source> <destination folder>
          backs up disc to a hard drive
    
    Source specification:
      iso:<FileName>    - open iso image <FileName>
      file:<FolderName> - open files in folder <FolderName>
      disc:<DiscId>     - open disc with id <DiscId> (see list Command)
      dev:<DeviceName>  - open disc with OS device name <DeviceName>
    
    Switches:
      -r --robot        - turn on "robot" mode, see http://www.makemkv.com/developer
    s
    DO was unexpected at this time.
    C:\Users\Kira\Desktop>..FOR /F "TOKENS=1 DELIMS=:" %f IN ("!vara!") DO (
    Again thanks for the quick response. I highlighted the error in red for easy viewing.
    -S
      My Computer


  4. Posts : 450
    Windows 7
       #4

    Checking...

    Replaced 2nd post in and fixed variable bug.....

    You have to delete all the leading .. that I added so it would indent correctly to be readable.
      My Computer


  5. Posts : 51
    Windows 7 Home Premium 64bit
    Thread Starter
       #5

    Thank you again. So I will cut and past this into my bat and give it a try. I'll remove all the ....
    One thing about this line

    SET var0=DRV:0,1,1,1,"DVD+R-DL HL-DT-ST DVD-RAM GH22NP20 1.04","HOLIDAY_MOVIES_1","\\Device\\CdRom0"

    The HOLIDAY_MOVIES_1 is the title of the DVD, so if I run this bat against another DVD that is not titled HOLIDAY_MOVIES_1 will this var0 be set correctly? (I will past the line together when I cut and paste)

    Your time is much appreciated.
    -S
    Last edited by nzdreamer55; 16 Dec 2011 at 14:29. Reason: clarification
      My Computer


  6. Posts : 450
    Windows 7
       #6

    nzdreamer55 said:
    Thank you again. So I will cut and past this into my bat and give it a try. I'll remove all the ....
    One thing about this line

    SET var0=DRV:0,1,1,1,"DVD+R-DL HL-DT-ST DVD-RAM GH22NP20 1.04","HOLIDAY_MOVIES_1","\\Device\\CdRom0"

    The HOLIDAY_MOVIES_1 is the title of the DVD, so if I run this bat against another DVD that is not titled HOLIDAY_MOVIES_1 will this var0 be set correctly? (I will past the line together when I cut and paste)

    Your time is much appreciated.
    -S
    In the final version:

    ALL LEADING .. removed.
    You can REMOVE the whole var0 line. I simply had that in in case you wanted to test MY script prior to changing that one FOR line and inserting ('YOUR COMMAND') line syntax. IOW, I can't test your command as I don't have access to it.

    At the bottom, for EACH line that has DRV: something, the 6th comma-delimited field that has "" around it will be placed in the variable mydir.

    You could then, at that point in the code, add something like this for example:

    MKDIR D:\mymovies\!mydir!\

    This would create a directory called D:\mymovies\HOLIDAY_MOVIES_1\

    Or whatever you want to do with the parsed value....
      My Computer


  7. Posts : 51
    Windows 7 Home Premium 64bit
    Thread Starter
       #7

    WOW! That worked! I know that this is really simple coding, but I am totally amazed when something like this works. It is like magic. Thanks for the help. I tried it and it printed at the bottom of the CMD window final HOLIDAY_MOVIES_1. That totally proved to me that it took that info from the DVD and parsed it out correctly into the %%y variable which gets set into mydir at the end.

    So if I understand this process now, I need to replace var0 with the command that generated the info for which the code you made will check. Does that sound right? I've tried several different things in the "!var0!" spot and none of them seem to do what happens when I set the variable in the beginning.

    -S
      My Computer


  8. Posts : 450
    Windows 7
       #8

    nzdreamer55 said:
    WOW! That worked! I know that this is really simple coding, but I am totally amazed when something like this works. It is like magic. Thanks for the help. I tried it and it printed at the bottom of the CMD window final HOLIDAY_MOVIES_1. That totally proved to me that it took that info from the DVD and parsed it out correctly into the %%y variable which gets set into mydir at the end.

    So if I understand this process now, I need to replace var0 with the command that generated the info for which the code you made will check. Does that sound right? I've tried several different things in the "!var0!" spot and none of them seem to do what happens when I set the variable in the beginning.

    -S
    No. Currently I had the syntax as: ("!var0!"). Inside DOUBLE quotes, the FOR loop acts upon the CONTENTS of that variable (in this case the literal string I embedded as a cut/paste from your command output). I've asked you to replace that syntax with: ('yourcommand') as in SINGLE quotes/ticks, DOS RUNS! that command and THEN the FOR loop acts upon the OUTPUT of that command.
      My Computer


  9. Posts : 51
    Windows 7 Home Premium 64bit
    Thread Starter
       #9

    RIGHT ON!!! Now it is working perfectly. Thanks again and please let me know what I need to do so that you get some good will your way.

    I just wanted to show what was the final product of all your hard work. Now all I have to do is link it with the autoplay when a disk is inserted and I should be able to fly through these things.

    Thanks

    -S
    Code:
    ::mkvbat
    
    @ECHO OFF
    
    SETLOCAL ENABLEDELAYEDEXPANSION
    REM process each line of output from command
    
    REM You will replace ("!var0!") below with ('your command')
    
    Path="C:\Program Files (x86)\MakeMKV"
    FOR /F "TOKENS=*" %%a IN ('makemkvcon -r info') DO (
    
    REM save in local variable for easier complex DOS script lines
    SET vara=%%a
    
    REM parse 1st value before first colon for this output line
    FOR /F "TOKENS=1 DELIMS=:" %%f IN ("!vara!") DO (
    
    REM if drv then we have something to examine
    IF /I %%f == drv (
    
    REM replace double-quote with @ for parsing
    SET varanq=!vara:"=@!
    
    REM get 1st chunk after first @ 
    FOR /F "TOKENS=2 DELIMS=@" %%h IN ("!varanq!") DO (
    
    SET var=%%h
    REM parse 3 bytes from beginning which is just after @
    SET var2=!var:~0,3!
    
    REM DVD? note slashI option to ignore case
    IF /I !var2! == dvd (
    
    REM parse 6th chunk comma-delimited
    FOR /F "TOKENS=6 DELIMS=," %%t IN ("!varanq!") DO (
    
    REM now remove @s on each end
    FOR /F "TOKENS=1 DELIMS=@" %%y IN ("%%t") DO (
    
    SET mydir=%%y
    ECHO final !mydir!
    MKDIR D:\mymovies\!mydir!\
    makemkvcon mkv disc:0 all D:\mymovies\!mydir!\
      My Computer


  10. Posts : 450
    Windows 7
       #10

    Good deal. Your question was very topical as I only wrote my first .bat script 2 weeks ago. Having programmed since 1977 with a dozen languages and 4 different hardware platforms, the DOS syntax was simply...unbelievably cryptic. So, now that my System Image (see the backup and restore forum) .bat is complete, I jumped on having a chance to help someone else who is new to .bat. And that Sys. Image .bat had some tricky stuff to a newbie.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 15:01.
Find Us