Dos Batch File Help: Storing %%c variable from FOR loop [Resolved]

410

What you might have to do for the FOR statement is CALL a subroutine at pass it %%c as an argument.

For Example:

for /f %%c in ('dir %%b\*.mdf /b') do (call :SetVar"%%c")
...
:SetVar
  Set DBFileName=%1
  echo Filename: %DBFileName%
  goto :eof
Share:
410

Related videos on Youtube

Mary
Author by

Mary

Updated on September 18, 2022

Comments

  • Mary
    Mary almost 2 years

    The bubble sort is supposed to be based on the wages array. The employee id, the pay rate and the hours should change after the wages have been sorted in ascending order. I am able to sort the pay rate but my employee id information remains the same. Do I need another do while loop?

    #include "stdafx.h" 
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    // Constant for the array size.
    const int ARRAY_SIZE = 4;
    
    // Function Prototypes
    void getEmployeeInfo(long [], int [], double [], double [], int);
    
    void bubbleSort(long empId[],int hours[],double payRate[],double wages[],int size); 
    
    void displayWages(long empId[], double wages[], int size);
    
    int main()
    {
        // Array of employee ID numbers
        long empId[ARRAY_SIZE] = { 5658845, 4520125, 7895122,
                                   8777541};
    
        // Array to hold the hours worked for each employee
        int hours[ARRAY_SIZE] = {0};
    
        // Array to hold the hourly pay rate for each employee
        double payRate[ARRAY_SIZE] = {0};
    
        // Array to hold the gross wages for each employee
        double wages[ARRAY_SIZE] = {0};
    
        // Get the employee payroll information and store
        // it in the arrays.
        getEmployeeInfo(empId, hours, payRate, wages, ARRAY_SIZE);
    
        // Display the payroll information.
        displayWages(empId, wages, ARRAY_SIZE);
    
        // Sort the payroll information in ascending order with a bubble sort. 
        bubbleSort(empId, hours, payRate, wages, ARRAY_SIZE); 
    
        // Display the payroll information again. 
        displayWages (empId, wages, ARRAY_SIZE); 
    
        system("PAUSE"); 
    
        return 0;
    }
    
    // ********************************************************
    // The getEmployeeInfo function receives four parallel    *
    // arrays as arguments. The 1st array contains employee   *
    // IDs to be displayed in prompts. It asks for input and  *
    // stores hours worked and pay rate information in the    *
    // 2nd and 3rd arrays. This information is used to        *
    // calculate gross pay, which it stores in the 4th array. *
    // ********************************************************
    void getEmployeeInfo(long emp[], int hrs[], double rate[],
                         double pay[], int size)
    {
        cout << "Enter the requested information "
             << "for each employee.\n";
    
        // Get the information for each employee.
        for (int count = 0; count < size; count++)
        {
            cout << "\nEmployee #: " << emp[count] << "\t";
    
            // Get this employee's hours worked.
            cout << "Hours worked: ";
            cin  >> hrs[count];
    
            // Validate hours worked.
            while (hrs < 0)
            {
                cout << "\nHours worked must be 0 or more. "
                     << "Please re-enter: ";
                cin  >> hrs[count];
            }
    
            // Get this employee's pay rate.
            cout << "\tPay rate: $";
            cin  >> rate[count];
    
            // Validate the pay rate.
            while (rate[count] < 6.00)
            {
                cout << "\nPay rate must be 6.00 or more. "
                     << "Please re-enter: $";
                cin  >> rate[count];
            }
    
            // Calculate this employee's gross pay.
            pay[count] = hrs[count]*rate[count]; 
    
            // ADD statement to calculate wages by multiplying
                    // hours with rate of pay;
        }
    }
    
    // ********************************************************
    // The bubbleSort function sorts the information based on *
    // the wages array.                                       *
    // ********************************************************
    void bubbleSort(long empId[],int hours[],double payRate[],double wages[],int size)
    {
        bool swap; 
        int index; 
    
        do
        {
            swap = false; 
            for (int count = 0; count < (size - 1); count++)
            {
                if (wages[count] > wages [count + 1])
                {
                    index = wages[count]; 
                    wages[count] = wages[count + 1]; 
                    wages[count + 1] = index; 
                    swap = true; 
                }
            }
    
        } while (swap); 
    }
    
    // ********************************************************
    // The displayWages function displays employee ID numbers *
    // and their wages.                                       *
    // ********************************************************
    void displayWages(long empId[], double wages[], int size)
    {
        // Set up the numeric output formatting.
        cout << fixed << showpoint << setprecision(2) << endl;
    
        // Display the header.
        cout << "----------------------------\n";
        cout << "Employee               Wages\n";
        cout << "----------------------------\n\n";
    
        // Display each employee's pay.
        for (int count = 0; count < ARRAY_SIZE; count++)
        {
            cout << "Employee #" << empId[count] << "   $";
            cout << setw(7) << wages[count] << endl << endl;
        }
    
    }
    
    • David W
      David W almost 13 years
      I'm sure it's to do with delayed expansion... like !DBFileName! ... i'll keep at it!
    • samgak
      samgak about 9 years
      Inside your bubblesort function where you swap two elements of the wages array, you need to also swap the corresponding elements of the empId, hours and payRate arrays
    • Mary
      Mary about 9 years
      So does that mean I should add 2 more if statements but have it correspond to the empId and hours?
    • samgak
      samgak about 9 years
      no, because you are sorting based on wages, so the if statement should only compare the wages. I'll add an answer to show what I mean
  • David W
    David W almost 13 years
    Thank you for the help. I just worked it out, will post solution
  • Doug Luxem
    Doug Luxem almost 13 years
    If that doesn't work - using a CALL SET in :SetVar might do the trick. Edit: Missed your last comment. :)
  • Mary
    Mary about 9 years
    Okay. Thank you. This was so simple. I didn't think of just adding the statements inside the loop and thought to add more for loops.
  • samgak
    samgak about 9 years
    You're welcome. Remember that the only thing connecting an employee id with a particular wage is that they are in the same position in the two arrays. So if you change one but not the other they will become out of sync and won't match any more. For this reason it's probably better to put all the information that goes together (id, wage etc) into a struct or class and have an array of structs instead of 4 separate arrays.