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

Author Topic: Run multiple command in a batch file  (Read 20621 times)

0 Members and 1 Guest are viewing this topic.

canislupus

    Topic Starter


    Starter

    • Experience: Experienced
    • OS: Windows 7
    Run multiple command in a batch file
    « on: February 03, 2011, 08:11:36 AM »
    Hi all,
    i need your support cause i'm a newbie about Ms Dos batch.
    My problem is that i want to run into a batch, two command.
    The first one has to compress a file and the other has to change the name of the resulting file adding a suffix.
    The problem i have encountered is that only the first one works, the second one doesn't start.
    Here you are the batch i have written:

    Code: [Select]
    RAR a XXX_ -m1 -df -ep -agYYYYMMDD C:\temp\*.txt
    for /f "tokens=1-2 delims=." %i in ('dir *.rar /b') do ren "%i.%j" "%i0330.%j"

    If i run the second command in the MsDos prompt it works.
    Thanks in advance

    Salmon Trout

    • Guest
    Re: Run multiple command in a batch file
    « Reply #1 on: February 03, 2011, 08:26:31 AM »
    One percent sign at the prompt, two in a batch file, so change all %i to %%i and all %j to %%j

    canislupus

      Topic Starter


      Starter

      • Experience: Experienced
      • OS: Windows 7
      Re: Run multiple command in a batch file
      « Reply #2 on: February 03, 2011, 08:33:01 AM »
      WORKS...
      Thanks a lot.
      Can you explain me why i have to double all %?

      Salmon Trout

      • Guest
      Re: Run multiple command in a batch file
      « Reply #3 on: February 03, 2011, 10:17:56 AM »
      WORKS...
      Thanks a lot.
      Can you explain me why i have to double all %?

      It's to do with the way the command interpreter expands variables. Using a percent sign (%) in a batch file requires that two percent signs (%%) be specified.

      For example, the command to display "5%" from a batch file would be :

         ECHO 5%%

      A single percent sign on a line is treated as a "nul" character in a batch file. For example:

         ECHO %     is processed as ECHO
         ECHO a%b   is processed as ECHO ab

      If a command contains two percent signs, the command interpreter will treat any characters between them as an environment variable to be expanded. For example, if the SET command shows that the current environment variables are

         COMSPEC=C:\COMMAND.COM
         PATH=C:\DOS
         PROMPT=$P$G
         B=C
                  

      then

         ECHO %PATH%     is processed as ECHO C:\DOS
         ECHO a%b%       is processed as ECHO aC
         ECHO a%b b%a    is processed as ECHO aa
                  

      If there are no characters between the two percent signs, one percent sign is stripped off and the other will remain. This is why a FOR command that echos the name of each file with a .COM extension would be

         FOR %V IN (*.COM) DO ECHO %V
                  

      but if the same command is placed in a batch file, the following is required:

         FOR %%V IN (*.COM) DO ECHO %%V
                  
                  
                  

      canislupus

        Topic Starter


        Starter

        • Experience: Experienced
        • OS: Windows 7
        Re: Run multiple command in a batch file
        « Reply #4 on: February 04, 2011, 08:56:56 AM »
        Perfect i have understood all.
        Now i have an other problem related to the same batch.
        I need to launch the for command on a file that isn't in the same directory where the batch run.
        I have tried to modify the batch from:

        Code: [Select]
        for /f "tokens=1-2 delims=." %%i in ('dir *.rar /b') do ren "%%i.%%j" "%%i0330.%j"
        to:

        Code: [Select]
        for /f "tokens=1-2 delims=." %%i in ('dir C:\temp\*.rar /b') do ren "%%i.%%j" "%%i0330.%%j"

        But i receive the error "The System cannot find the file specified".
        This time where is the problem ?

        P.S. Sorry if i asked stupid thing but i am a newbie...  ;)

        Salmon Trout

        • Guest
        Re: Run multiple command in a batch file
        « Reply #5 on: February 04, 2011, 10:37:21 AM »


        Code: [Select]
        for /f "delims=" %%i in ('dir /b C:\temp\*.rar') do ren "%%~dpnxi" "%%~dpni0330%%~xi"

        Salmon Trout

        • Guest
        Re: Run multiple command in a batch file
        « Reply #6 on: February 05, 2011, 04:52:28 AM »
        I apologise, the above won't work, but this should...

        Code: [Select]
        for %%i in ("C:\temp\*.rar") do ren "%%~dpnxi" "%%~ni0330%%~xi"
        explanation

        FOR %%variable IN (set) DO command.

        Using FOR without the /F switch and therefore no tokens/delims block, (set) can be a set of one or more files, wildcards can be used.

        There are FOR variable modifiers... in this case we use ~d, (drive) ~p, (path) ~n (name) ~x (extension) so if (set) is a file or files, then:

        %%~di is the drive letter (with a colon)
        %%~pi is the path
        %%~ni is the file name
        %%~xi is the file extension (including the dot)

        They can be combined so that %%~dpnxi is the full drive letter, path, name and extension of a file. You need to supply this to REN if the file in question is not in the current directory.

        Type FOR /? at the prompt for the command help.




        « Last Edit: February 05, 2011, 05:20:26 AM by Salmon Trout »