1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Batch file assistance needed

Discussion in 'DOS/Other' started by RTS352, May 5, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. RTS352

    RTS352 Thread Starter

    Joined:
    May 5, 2010
    Messages:
    3
    I got a lot of help from this thread:

    http://forums.techguy.org/dos-pda-other/740729-batch-file-read-input-file.html

    But still not quite there.

    I am trying to generate an output file in the following format:
    <Counts>
    <OccupiedCount>300</OccupiedCount>
    <ReservedCount>0</ReservedCount>
    <TotalCount>500</TotalCount>
    <Timestamp>"2009-11-21T14:06"</Timestamp>
    </Counts>
    Text in Red is variable and updated every two minutes. It is generated from an input file (updated every two minutes) that contains:

    300
    500
    2009-11-21T14:06
    The following code works:
    @echo off
    ECHO ^<COUNTS^> > w1.txt
    ECHO ^<OccupiedCount^>%1^</OccupiedCount^> >> w1.txt
    ECHO ^<ReservedCount^>0^</ReservedCount^> >> w1.txt
    ECHO ^<TotalCount^>%2^</TotalCount^> >> w1.txt
    ECHO ^<Timestamp^>%3^</Timestamp^> >> w1.txt
    ECHO ^</Counts^> >> w1.txt
    On a one-time basis when running the batch file as follows:
    C:\Users>w1 300 500 "2009-11-21T14:06"
    C:\Users>type w1.txt
    <COUNTS>
    <OccupiedCount>300</OccupiedCount>
    <ReservedCount>0</ReservedCount>
    <TotalCount>500</TotalCount>
    <Timestamp>"2009-11-21T14:06"</Timestamp>
    </Counts>
    C:\Users>
    Which would be great if I only had to run the job once, but as stated I need to update it every two minutes and I need to pass the command line input via an input file.

    The following code (from the above link) gets me closer:
    @echo off
    SetLocal EnableDelayedExpansion
    ::This line is just in case n has been defined before the batch file is run
    Set n=
    Set _InputFile=C:\Users\input.txt
    For /F "tokens=*" %%I IN (%_InputFile%) DO (
    Set /a n+=1
    Set _var!n!=%%I
    echo %%I
    )
    :: This line will display the variables just assigned
    :: For testing only, delete when not needed
    Set _
    EndLocal
    Generating:
    C:\Users>test3
    300
    500
    "2009-11-21T14:06"
    _InputFile=C:\Users\input.txt
    _var1=300
    _var2=500
    _var3="2009-11-21T14:06"
    C:\Users>
    Which is one step close to getting what I want, but when I try passing '_var1' or '%%I' to an echo statement to generate the output it does not work. I need a way to tie the two together where I run the batch file with a SLEEP 120/GOTO START statement at the end to re-run it every two minutes. Any assistance would be greatly appreciated.
     
  2. RTS352

    RTS352 Thread Starter

    Joined:
    May 5, 2010
    Messages:
    3
    Solved it the best way, I figured it out by myself, I think. I just needed a trailing '%' after _var1, _var2, _var3 to make it work:
    @echo off
    SetLocal EnableDelayedExpansion
    ::This line is just in case n has been defined before the batch file is run
    Set n=
    Set _InputFile=C:\Users\input.txt
    For /F "tokens=*" %%I IN (%_InputFile%) DO (
    Set /a n+=1
    Set _var!n!=%%I
    )
    :: This line will display the variables just assigned
    :: For testing only, delete when not needed
    @echo off
    ECHO ^<COUNTS^> > w1.txt
    ECHO ^<OccupiedCount^>%_var1%^</OccupiedCount^> >> w1.txt
    ECHO ^<ReservedCount^>0^</ReservedCount^> >> w1.txt
    ECHO ^<TotalCount^>%_var2%^</TotalCount^> >> w1.txt
    ECHO ^<Timestamp^>%_var3%^</Timestamp^> >> w1.txt
    ECHO ^</Counts^> >> w1.txt
     
  3. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    How did you know to surround your delayed expansion variables in exclamations but not your regular variables in percent signs? You even surrounded your input file variable in percent signs.

    The way you have it now should work just fine.
     
  4. RTS352

    RTS352 Thread Starter

    Joined:
    May 5, 2010
    Messages:
    3
    About the only way I know how to code, take someone else's code and modify it to meet my needs. And if it doesn't work hack away at it until it does work. :)
     
  5. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Here is a good link to learn about batch files. Like any other language it is just learning the syntax.
    http://www.robvanderwoude.com/batchfiles.php
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Similar Threads - Batch file assistance
  1. niedernsill
    Replies:
    3
    Views:
    424
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/921333

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice