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

Author Topic: Looping and Zipping of folders  (Read 3910 times)

0 Members and 1 Guest are viewing this topic.

AyakaShota

    Topic Starter


    Starter

    • Experience: Beginner
    • OS: Unknown
    Looping and Zipping of folders
    « on: March 29, 2011, 11:45:22 PM »
    Hi All,

    I am new to Dos Batch. My problem now is that I need to loop through a directory with many subfolders. I need to zip those subfolders (using 7zip) and place the zipped subfolders into another directory.

    Before zipping
    My main root is C:\Users\ayaka\Desktop\ZipTask\TestingDirectory
    Sample Folders:
    C:\Users\ayaka\Desktop\ZipTask\TestingDirectory\April2001\temp123\temp123.html
    C:\Users\ayaka\Desktop\ZipTask\TestingDirectory\April2001\temp234\temp234.html
    C:\Users\ayaka\Desktop\ZipTask\TestingDirectory\May2001\temp345\temp345.html
    C:\Users\ayaka\Desktop\ZipTask\TestingDirectory\May2001\temp456\temp456.html

    After zipping
    C:\Users\ayaka\Desktop\ZipTask\TestDir

    The folders should be zip according to the month and year. So, there should be 2 zipped folders, mainly April2001 and May2001 in C:\Users\ayaka\Desktop\ZipTask\TestDir

    I had done abit of coding, but it doesn't really work.

    Here's the code:

    Code: [Select]
    @ECHO off


    FOR /R C:\Users\ayaka\Desktop\ZipTask\TestingDirectory\ %%G IN (*) DO (


    for /f "tokens=1-7 delims=\ " %%i in ("%%G") do (
    Set folder=%%o
    Set path=%%i\%%j\%%k\%%l\%%m\%%n\%%o

    cd C:\Users\ayaka\Desktop\ZipTask\7za920

    7za.exe a -t7z %path%\%folder%.7z C:\Users\ayaka\Desktop\ZipTask\TestDir


    )

    )
    @ECHO on

    Can anyone help me? Thanks.

    AyakaShota

      Topic Starter


      Starter

      • Experience: Beginner
      • OS: Unknown
      Re: Looping and Zipping of folders
      « Reply #1 on: March 30, 2011, 03:48:34 AM »
      I managed to create the batch file to do it. But I have no idea why the program stopped after the first zipped folder is made.

      Here's the new code:

      Code: [Select]
      @ECHO off
      FOR /R C:\Users\ayaka\Desktop\ZipTask\TestingDirectory\ %%G IN (*) DO (
      FOR /f "tokens=1-7 delims=\ " %%i in ('dir /b /s "%%G"') do (

      IF NOT EXIST C:\Users\ayaka\Desktop\ZipTask\TestDir\%%o.7z (

      echo %%i\%%j\%%k\%%l\%%m\%%n\%%o

      cd C:\Users\ayaka\Desktop\ZipTask\7za920

      7za.exe a -t7z C:\Users\ayaka\Desktop\ZipTask\TestDir\%%o.7z C:\Users\ayaka\Desktop\ZipTask\TestingDirectory\%%o
      )
      )
      )
      @ECHO on

      Output:
      C:\Users\ayaka\Desktop\ZipTask\TestDir\April2004.7z

      The output should also include this file, but the program did not continue:
      C:\Users\ayaka\Desktop\ZipTask\TestDir\May2004.7z

      AyakaShota

        Topic Starter


        Starter

        • Experience: Beginner
        • OS: Unknown
        Re: Looping and Zipping of folders
        « Reply #2 on: March 30, 2011, 07:42:37 PM »
        Finally, I made it. But it will take a very long time if there are alot of folders in each folder and many files in each folder.

        Here's the code:

        Code: [Select]
        @echo off

        echo.going to execute loop
        call:loop
        echo.returned from loop
        echo.&pause&goto:eof

        :loop
        FOR /R C:\Users\ayaka\Desktop\ZipTask\TestDir\ %%G IN (*) DO (
        echo.going to execute cont
        call:cont %%G
        echo.returned from cont
        )
        echo.going back to loop
        echo.&pause&goto:eof

        :cont
        FOR /f "tokens=1-7 delims=\ " %%i in ('dir /b /s "%~1"') do (
        echo %%i\%%j\%%k\%%l\%%m\%%n\%%o >> path.txt
        IF NOT EXIST C:\Users\ayaka\Desktop\ZipTask\testingFolder\%%o.7z (
        cd C:\Users\ayaka\Desktop\ZipTask\7za920
        7za.exe a -t7z C:\Users\ayaka\Desktop\ZipTask\testingFolder\%%o.7z C:\Users\ayaka\Desktop\ZipTask\TestingDirectory\%%o
        )
        )
        goto:eof

        Anybody has any idea on how to improve this? So that it can be done efficiently?