Announcement

Collapse
No announcement yet.

Running command with Variable's

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Running command with Variable's

    Hi,

    I need to run a command i.e :

    commands:

    schtasks /query /TN "\Microsoft\Windows\TestTask\Backup" /fo csv
    schtasks /query /TN "\Microsoft\Windows\TestTask\Backup1" /fo csv
    schtasks /query /TN "\Microsoft\Windows\TestTask\Backup1" /fo csv

    Now i want to run with a single command by passing a variable.

    i.e:
    schtasks /query /TN %variable% /fo csv


    How can i achieve this?


    Regards,
    Mjoshi

  • #2
    Re: Running command with Variable's

    Run the batch with script arguments on the command line, i.e.
    QschedT.cmd "\Microsoft\Windows\TestTask\Backup"

    In the batch use the variable(s)
    %1 = just the first
    %2 = the second argument
    ect.... up to %9, then you can use SHIFT

    %* =all arguments


    i.e.
    schtasks.exe /query /TN %* /fo csv


    /Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Running command with Variable's

      Did you mean this way:


      set TaskName1="\Microsoft\Windows\TestTask\Backup"
      set TaskName2="\Microsoft\Windows\TestTask\Scan"

      schtasks /query /TN %TaskName1% %TaskName2% /fo csv


      I had used this but its failing.

      Comment


      • #4
        Re: Running command with Variable's

        Originally posted by mjoshi87 View Post
        Did you mean this way:


        set TaskName1="\Microsoft\Windows\TestTask\Backup"
        set TaskName2="\Microsoft\Windows\TestTask\Scan"

        schtasks /query /TN %TaskName1% %TaskName2% /fo csv


        I had used this but its failing.
        I must have misunderstood the question.

        You are looking for iteration for each "tasknamenn" variable.
        Here an example,
        Code:
        @echo off
        
        :: Task Names
        set T1="TestTask\Backup"
        set T2="TestTask\Scan"
        
        Set tasks=%T1%, %T2%, %T3%
        For %%! in (%tasks%) do (
           START schtasks.exe /query /TN "\Microsoft\Windows\%%~!" /fo csv
        )
        /Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Running command with Variable's

          Yes now you have judged it correct.

          But one additional point i forgot to add up here:

          For each run, the cmd should redirect the output with Filename as Backup.txt for "T1" and Scan.txt for "T2" and etc
          i.e for T1 say
          schtasks.exe /query /TN "\Microsoft\Windows\TestTask\Backup" /fo csv >c:\Backup.txt

          with each run it should save file on some location , the filename should be passed with Variable too.

          Comment


          • #6
            Re: Running command with Variable's

            is this usable?
            The files backup.csv and scan.csv wil be created in a folder called c:\TestTask

            Code:
            @echo off
            
            :: Task Names
            set T1="TestTask\Backup"
            set T2="TestTask\Scan"
            
            Set tasks=%T1%, %T2%, %T3%
            For %%! in (%tasks%) do (
               mkDir "c:\%%~!.csv.$" & rd /s /q "c:\%%~!.csv.$"
               START schtasks.exe /query /TN "\Microsoft\Windows\%%~!" /fo csv >"c:\%%~!.csv"
            )
            /Rems

            This posting is provided "AS IS" with no warranties, and confers no rights.

            __________________

            ** Remember to give credit where credit's due **
            and leave Reputation Points for meaningful posts

            Comment


            • #7
              Re: Running command with Variable's

              I want to capture the output of this into some text file, and from these text file i will monitor i.e log file monitoring for my Schedule Job status e.g is it failed or disabled.

              From the below script i cant decide what is the result of my cmd, i want to capture in a file for each task name.

              @echo off

              :: Task Names
              set T1="TestTask\Backup"
              set T2="TestTask\Scan"

              Set tasks=%T1%, %T2%, %T3%
              For %%! in (%tasks%) do (
              START schtasks.exe /query /TN "\Microsoft\Windows\%%~!" /fo csv
              )



              But from the updated script which you have shared with me is just creating a empty file. Its not saving the cmd output within the file.

              Comment


              • #8
                Re: Running command with Variable's

                Originally posted by mjoshi87 View Post
                from the updated script which you have shared with me is just creating a empty file. Its not saving the cmd output within the file.
                My bad. Because in the batch schtasks.exe is launched directly using a START statement, the redirection doesn't work as expected.

                Two options to solve this, choose one,
                - Don't use START (but then the separate schtasks.exe jobs will not be able to run simultaneously, that makes the batch run just a little bit longer)
                - Use START to open a new command prompt first.

                here is how to open a new command prompt for each schtasks.exe command:
                Code:
                @echo off
                
                :: Task Names
                set T1="TestTask\Backup"
                set T2="TestTask\Scan"
                
                Set tasks=%T1%, %T2%, %T3%
                For %%! in (%tasks%) do (
                   mkDir "c:\query scheduled tasks\%%~!.csv.$" & rd /s /q "c:\query scheduled tasks\%%~!.csv.$"
                   START ""/MIN/LOW cmd /c "COLOR 72&TITLE %%! &schtasks.exe /query /TN "\Microsoft\Windows\%%~!" /fo csv >"c:\query scheduled tasks\%%~!.csv" 2>&1"
                )
                /Rems
                Last edited by Rems; 16th March 2012, 10:18.

                This posting is provided "AS IS" with no warranties, and confers no rights.

                __________________

                ** Remember to give credit where credit's due **
                and leave Reputation Points for meaningful posts

                Comment


                • #9
                  Re: Running command with Variable's

                  Rems, problem is still the same, Why its creating only empty file only?

                  Comment


                  • #10
                    Re: Running command with Variable's

                    I have adjusted the taskname (the prefix \Microsoft\Windows\ was missing), please try again.

                    One other thing I changed in the script is that now, in case of an error, the error messages willl be rediredted also to the log file. This makes it easier to troubleshoot, because now the logfile will not just be empty on error.


                    /Rems

                    This posting is provided "AS IS" with no warranties, and confers no rights.

                    __________________

                    ** Remember to give credit where credit's due **
                    and leave Reputation Points for meaningful posts

                    Comment


                    • #11
                      Re: Running command with Variable's

                      awesome Man.....its working perfect now...
                      Really appreciate for your help...

                      Comment


                      • #12
                        Re: Running command with Variable's

                        Rems, I want to understand the doubts from the below command :

                        mkDir "c:\query scheduled tasks\%%~!.csv.$" & rd /s /q "c:\query scheduled tasks\%%~!.csv.$"


                        Why " rd /s /q " command is being used out here? Its trying to remove the directory right? Whats the fun to add this here?

                        Comment


                        • #13
                          Re: Running command with Variable's

                          Originally posted by mjoshi87 View Post
                          Rems, I want to understand the doubts from the below command :

                          mkDir "c:\query scheduled tasks\%%~!.csv.$" & rd /s /q "c:\query scheduled tasks\%%~!.csv.$"

                          Why " rd /s /q " command is being used out here? Its trying to remove the directory right? Whats the fun to add this here?
                          Sometimes the thing with batch scripts is, unlike real scripting languages usually, you have to use multiple statements, tokenizing results, capturing, filtering and combining, or what ever more - to construct what ever is desired. So you sometimes have to be creative to accomplish certain things when using (just-) a batch file solution, that's part of the fun I guess. And there are always more ways to Rome.


                          In this case,,, the task name variable is used also to name the logfile.
                          Therefore the shortened task name (TestTask\Backup) is interpred as the file name "Backup" (w/out an file extension) and the name of a folder "TestTask" where the log file should be created. Additionally a file extension (.csv) will be appended to the value of the TN variable - and, a parent path (c:\query scheduled tasks\) prepended to the value.
                          results as:
                          c:\query scheduled tasks\TestTask\Backup.csv
                          c:\query scheduled tasks\TestTask\Scan.csv

                          To be able to shorten the task name and thus shorten the path to the log files, the leading part (\Microsoft\Windows\) of the actual task name is entered as a constance at the schtasks.exe statement.

                          The output of the schtasks.exe statement is redirected to "c:\query scheduled tasks\%TN%.csv". Now to ensure the folder "c:\query scheduled tasks\Backup\" and folder "c:\query scheduled tasks\" do exist, I use the two commands,
                          mkDir "drv\path\%%~!"
                          -and-
                          rd /q "drv\path\%%~!"

                          1st - it creates the directory (creating the path if it does not exist already) using the value of the task name variable. The folders created by this command are "TestTask\Backup\" and "TestTask\Scan\"

                          2nd - it deletes the last subfolder ("Backup" or "Scan") from the path. Because this subfolder will not be used since the last part of the taskname variable is being used to give the file its name, it gets the file extension attached. The file will automatically be created in the "\TestTask\" folder, the subfolders "\Backup\" and "\Scan\" remains empty.

                          In addition - To ensure the RD command does not delete an existing subfolder with the name "Backup" or "Scan" that could be there already for another reason and should not be deleted, the subfolder created and deleted by the script gets ".csv.$" trailing the name of the subfolder. This way the folder name does not interfere with existing files or folders with that name.

                          _

                          Long story short,
                          The mkDir command creates the following directories,
                          c:\query scheduled tasks\TestTask\Backup.csv.$\
                          c:\query scheduled tasks\TestTask\Scan.csv.$\

                          The output of the schtasks.exe is redirected to the files:
                          c:\query scheduled tasks\TestTask\Backup.csv
                          c:\query scheduled tasks\TestTask\Scan.csv

                          The subfolders below are empty thus can be deleted
                          <...>\TestTask\Backup.csv.$\
                          <...>\TestTask\Scan.csv.$\


                          /Rems
                          Last edited by Rems; 20th March 2012, 16:17.

                          This posting is provided "AS IS" with no warranties, and confers no rights.

                          __________________

                          ** Remember to give credit where credit's due **
                          and leave Reputation Points for meaningful posts

                          Comment


                          • #14
                            Re: Running command with Variable's

                            Thanks Rem for your support and suggestion....

                            Comment

                            Working...
                            X