Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file - Filenames when using wildcards  (Read 8379 times)

0 Members and 1 Guest are viewing this topic.

gregory

    Topic Starter


    Beginner

    Thanked: 1
    • Experience: Experienced
    • OS: Linux variant
    Batch file - Filenames when using wildcards
    « on: September 29, 2008, 03:30:04 PM »
    Can it be done, when sending multiple files to a command using a wildcard, such as in...
    Code: [Select]
    FOR %%X IN (*.txt) DO COMMANDthat each filename sent to the command can be somehow got/captured as it is passed along?

    I would like to save each filename to a variable as the file is sent to the command, then, when the next file is sent, replace the variable with the next filename.

    An example:

    File #1 of 10 gets sent to the command, I need to know that filename so i can do something like the following:
    Code: [Select]
    ...DO COMMAND [string manipulation of filename for File #1]
    Hope that makes sense, let me know. ???

    Thanks for the help. :)

    diablo416



      Hopeful
      • Experience: Beginner
      • OS: Unknown
      Re: Batch file - Filenames when using wildcards
      « Reply #1 on: September 29, 2008, 04:48:47 PM »
      try this

      set c=0
      for %%X IN (*.TXT do (
            call set c=%%c%%+1
            call set c.%%c%%=%%X
      )



      that would set all filenames into variables , c.0 + 1
      example:
      %c.1%
      %c.2%
      %c.3%

      gregory

        Topic Starter


        Beginner

        Thanked: 1
        • Experience: Experienced
        • OS: Linux variant
        Re: Batch file - Filenames when using wildcards
        « Reply #2 on: September 29, 2008, 05:04:14 PM »
        Thanks for the quick reply diablo416. :)

        I think that might be close to what I need, but instead of each filename getting its own variable, I need just one. One variable that keeps getting replaced as each new file is sent to the command.

        Thanks again.


        Dias de verano

        • Guest
        Re: Batch file - Filenames when using wildcards
        « Reply #3 on: September 30, 2008, 05:56:34 AM »
        for %%X IN (*.TXT do (
              SET VARIABLE=%%x
              command
              )

        gregory

          Topic Starter


          Beginner

          Thanked: 1
          • Experience: Experienced
          • OS: Linux variant
          Re: Batch file - Filenames when using wildcards
          « Reply #4 on: October 02, 2008, 05:06:34 PM »
          @ Dias de verano
          Thank you, that was remarkably simple. I had actually tried it before but believed it not to work, as I had not looked at the output carefully. Perhaps it still may not work fully with what I need to do.

          Here is the source of my confusion:
          Code: [Select]
          FOR %%X IN (*.TXT) DO (
               SET test=%%x
               command [command parameter in which I call variable(test)]
               )
          I call the variable(test) like so, %test% , however this does not work.
          Additionally, variables I set outside of this specific FOR command, work fine, however it should be noted these have hard set values, unlike variable(test) with its value of %%X.

          So, the question is, how do I call variable(test) in my command's parameters?

          Thanks for the help.

          Dias de verano

          • Guest
          Re: Batch file - Filenames when using wildcards
          « Reply #5 on: October 03, 2008, 12:09:03 AM »
          Google for the topic "delayed expansion".

          gregory

            Topic Starter


            Beginner

            Thanked: 1
            • Experience: Experienced
            • OS: Linux variant
            Re: Batch file - Filenames when using wildcards
            « Reply #6 on: October 03, 2008, 02:02:46 AM »
            Thanks Dias de verano, I got it working now. ;D

            -edit-

            I just read the previous thread I had started, where you explicitly warned me about this, my bad.
            « Last Edit: October 04, 2008, 12:18:45 AM by gregory »