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

Author Topic: Zipping files using batch file  (Read 47228 times)

0 Members and 1 Guest are viewing this topic.

ionic

    Topic Starter


    Rookie

    Zipping files using batch file
    « on: January 01, 2008, 02:29:06 PM »
    Is there a way to:
    1)locate files with a  known file extension on a known Drive and
    2)send them to a zip file on windows XP desktop?

    I know XP has a zip function, not sure what it's called....could it be used instead of an old pkzip exec.

    Also: how do you determine the Short File Name with XP as "Open Command Prompt Here" shows only Long File Names.
    « Last Edit: January 01, 2008, 03:53:38 PM by ionic »

    patio

    • Moderator


    • Genius
    • Maud' Dib
    • Thanked: 1769
      • Yes
    • Experience: Beginner
    • OS: Windows 7
    Re: Zipping files using batch file
    « Reply #1 on: January 01, 2008, 04:15:46 PM »
    Actually PKZip is better suited for this task...have you read the PKZip Help files ? ?
    " Anyone who goes to a psychiatrist should have his head examined. "

    ionic

      Topic Starter


      Rookie

      Re: Zipping files using batch file
      « Reply #2 on: January 01, 2008, 04:45:13 PM »
      Apparently microsoft made it impossible to use command line to do this.  Bottom line I still need a way, without software installation to locate files of a particular extension, compile them in a single file for emailing. What about using makecab.exe for the compiling??

      Usage:

      MAKECAB [/V[n]] [/D var=value ...] [/L dir] source [destination]

      MAKECAB [/V[n]] [/D var=value ...] /F directive_file [...]

      source File to compress.

      destination File name to give compressed file. If omitted, the last character of the source file name is replaced with an underscore (_) and used as the destination.

      /F directives A file with MakeCAB directives (may be repeated). Refer to Microsoft Cabinet SDK for information on directive_file.

      /D var=value Defines variable with specified value.

      /L dir Location to place destination (default is current directory).

      /V[n] Verbosity level (1..3).


      Let me clarify:

      1) I need to go to a specific hard drive (EXAMPLE: D:\)

      CD D:\

      2) From there I need to locate all files, including those in any sub-directories with a particular file extension(EXAMPLE: *.mp3)

      If exists *.mp3

      3) Copy them to a folder called (EXAMPLE: MYMP3Z)

      echo | Y copy file to MYMP3Z

      4) Convert the folder or all files in the folder into a single file either .zip, .cab....whatever(EXAMPLE: MYMP3Z.CAB)

      pkzip D:\MP3Z\*.* C:\Documents and Settings\John Doe\Desktop\MP3Z.zip

      5) Place that  file on the windows desktop(EXAMPLE: C:\Documents and Settings\John Doe\Desktop)

      6) Delete the folder MYMP3Z

      ECHO | Y DEL D:\MP3Z\*.*
      RD D:\MP3Z


      I don't think PKZIP for DOS can do all of that.

      I may also need to a DOS zip executable to the Windows\System32 Directiry prior to running.



      « Last Edit: January 01, 2008, 07:21:47 PM by ionic »

      chinna



        Rookie

        Re: Zipping files using batch file
        « Reply #3 on: January 01, 2008, 08:44:44 PM »
        ionic,
                 Iam using 7-zip software for zipping and unzipping files using batch file.


        http://www.7-zip.org/download.html

        Download the .exe file from the above link and install on ur pc.

        suppose u r installed in c:\programfiles\
        Please assign the path in envirnomental variables (my computer--properties) ---system variables---path .
        say: c:\programfiles\7z       paste  this path in the   system variables---path

        cmd for unzpping:
        7z e  c:\a.zip  -oc:\

        -o : output dir.
        unzips the a.zip to a.txt file

        cmd for zipping :
        7z a -tzip c:\b.zip   c:\a.txt
        zips the a.txt to b.zip

        Save the cmd in .bat format and execute

        Hope this may help you.

        regards,
        chinna



        ionic

          Topic Starter


          Rookie

          Re: Zipping files using batch file
          « Reply #4 on: January 01, 2008, 09:20:23 PM »
          I am currently playing around with XCOPY to copy the files from directories and subdirectories and place them in a specified folder.  From there I can run a zip app to place them in one file where I want.

          Not sure if XCOPY can copy only the files from a directory and subdirectories without maintaining the folder structure.

          This is what I have come up with thus far:


          @ECHO OFF
          MD D:\TXTZ

          XCOPY I:\*.txt D:\TXTZ\ /n

          PKZIP -es -m -rp -r- D:\MYTXTZ.ZIP D:\TXTZ\*.*

          IF EXISTS D:\MYTXTZ.ZIP (
          ECHO Y|RD D:\TXTZ /s /q
          )ELSE(
          ECHO Missing file "MYTXTZ.ZIP"
          )
          exit


          The results: Although I included the full path where XCOPY should find files, it copies files from where the batch file is located, in this case on the desktop. XCOPY did not copy any files in subfolders on the desktop.
          If I set the switch for XCOPY to Coy folders and subfolders except empty ones with /S it does copy them, but
          I'd rather leave the folders out of the equation.

          PKZIP worked fine

          RMDIR did not delete the folder D:\TXTZ
          « Last Edit: January 02, 2008, 08:51:12 AM by ionic »

          ionic

            Topic Starter


            Rookie

            Re: Zipping files using batch file
            « Reply #5 on: January 03, 2008, 11:30:58 AM »
            I think I'm stuck here. Any help would be appreciated. I think it should work but I need to lookup the switch options. Then again, maybe xcopy is not the right way to go.

            patio

            • Moderator


            • Genius
            • Maud' Dib
            • Thanked: 1769
              • Yes
            • Experience: Beginner
            • OS: Windows 7
            Re: Zipping files using batch file
            « Reply #6 on: January 03, 2008, 12:46:33 PM »
            " Anyone who goes to a psychiatrist should have his head examined. "

            Sidewinder



              Guru

              Thanked: 139
            • Experience: Familiar
            • OS: Windows 10
            Re: Zipping files using batch file
            « Reply #7 on: January 03, 2008, 02:16:23 PM »
            You're making this harder than it has to be. You had all the pseudo-code laid out, all you needed to do was code it up:

            Code: [Select]
            cd /d d:\
            for /f "tokens=* delims=" %%x in ('dir /s /b /a:-d *.mp3') do (
            copy "%%x" d:\mymp3z
            )
            cscript scriptname.vbs
            rd d:\mymp3z /s/q

            The cscript is below. You can indeed use Window's built-in zip routines; you just can't use batch code.

            Code: [Select]
            strTargetFile = "C:\Documents and Settings\John Doe\Desktop\MP3Z.zip"         

            Set fso = CreateObject("Scripting.FileSystemObject")
            Set zip = fso.CreateTextFile(strTargetFile)
            zip.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
            zip.Close
            WScript.Sleep 500

            Set objShell = CreateObject("Shell.Application")
            Set objFile = objShell.NameSpace(strTargetFile)
            Set src = objShell.NameSpace("d:\MYMP3Z")   

            objFile.CopyHere src
            WScript.Sleep 30000

            Save the script (the name has to match the batch file reference) in the same directory as the batch file and with a vbs extension.

            The Copyhere method runs asynchronous to the batch file. I built in a 30 second time delay but you may have to increase it based on the size and number of the mp3 files. The time interval on the sleep method is measured in milliseconds. This is important as you do not want to remove the mymp3z directory before you finish zipping the files.

            Good luck. 8)
            The true sign of intelligence is not knowledge but imagination.

            -- Albert Einstein

            ionic

              Topic Starter


              Rookie

              Re: Zipping files using batch file
              « Reply #8 on: January 03, 2008, 05:45:51 PM »
              You're making this harder than it has to be. You had all the pseudo-code laid out, all you needed to do was code it up:

              That's just it, the cscript and vbs stuff is completely foreign to me. Coding it up was not possible so I would need expernal programs like pkzip and batch file for a time delay...

              I do appreciate your work and will let you know how it turns out.

              Thanks

              ionic

                Topic Starter


                Rookie

                Re: Zipping files using batch file
                « Reply #9 on: January 04, 2008, 01:42:34 PM »
                I have run the code(bat & vbs) and encountered some problems:

                There is the code I am using at present:
                Code: [Select]
                @echo off
                md c:\docume~1\brianb~1\desktop\TEXTZ\
                cd /d i:\
                for /f "tokens=* delims=" %%x in ('dir /s /b /a:-d *.txt') do (
                copy "%%x" C:\docume~1\brianb~1\desktop\TEXTZ\
                )
                cscript grabnzip.vbs
                rd C:\docume~1\brianb~1\desktop\TEXTZ\ /s/q

                As a test I altered the code to copy some txt files...
                The first problem I had was that a folder was not created, but rather a file with no extension. So I added the
                line to make a directory. I also changed the folder path to be the same as the .zip destination.

                Code: [Select]
                strTargetFile = "C:\docume~1\brianb~1\desktop\TEXTZ.zip"         

                Set fso = CreateObject("Scripting.FileSystemObject")
                Set zip = fso.CreateTextFile(strTargetFile)
                zip.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
                zip.Close
                WScript.Sleep 500

                Set objShell = CreateObject("Shell.Application")
                Set objFile = objShell.NameSpace(strTargetFile)
                Set src = objShell.NameSpace("C:\docume~1\brianb~1\desktop\TEXTZ\")   

                objFile.CopyHere src
                WScript.Sleep 30000

                After all the code ran I did get a folder created and the txt files(15) were copied to the folder. Then the folder
                was deleted, but there was no zip file created.

                It seems that at this point I might as well use pkzip as I can't achieve my goal with a single batch file. So if I use a .vbs file or pkzip.exe makes no difference.

                « Last Edit: January 04, 2008, 02:56:38 PM by ionic »

                patio

                • Moderator


                • Genius
                • Maud' Dib
                • Thanked: 1769
                  • Yes
                • Experience: Beginner
                • OS: Windows 7
                Re: Zipping files using batch file
                « Reply #10 on: January 04, 2008, 02:01:59 PM »
                You're also ignoring how powerful PKZip is/was...it went outside of DOS/batch limitations in alot of ways way before it's time and it would be a good learning experience to do some more research along with the link i provided...

                XTree Gold is another example of batch/scripting on steroids but takes some getting used to...actually that's innaccurate. It's not a batch/script tool rather a powerful DOS shell.
                " Anyone who goes to a psychiatrist should have his head examined. "

                Sidewinder



                  Guru

                  Thanked: 139
                • Experience: Familiar
                • OS: Windows 10
                Re: Zipping files using batch file
                « Reply #11 on: January 04, 2008, 02:44:47 PM »
                Using PKZip is a fine alternative. I only posted because you originally wanted to use the built-in capabilities of Windows.

                When I tested my original code, it worked fine! I'm very surprised the TEXTZ.zip file did not end up in C:\docume~1\brianb~1\desktop. I'm going to take it on faith that brianb~1 is your account.

                Good luck.  8)

                The true sign of intelligence is not knowledge but imagination.

                -- Albert Einstein

                ionic

                  Topic Starter


                  Rookie

                  Re: Zipping files using batch file
                  « Reply #12 on: January 04, 2008, 02:49:18 PM »
                  You're also ignoring how powerful PKZip is/was...it went outside of DOS/batch limitations in alot of ways way before it's time and it would be a good learning experience to do some more research along with the link i provided...

                  I will take the time to read the pkzip documentation.  It's not a short doc but like you said it may be  all I need.

                  ionic

                    Topic Starter


                    Rookie

                    Re: Zipping files using batch file
                    « Reply #13 on: January 04, 2008, 02:55:12 PM »
                    When I tested my original code, it worked fine! I'm very surprised the TEXTZ.zip file did not end up in C:\docume~1\brianb~1\desktop. I'm going to take it on faith that brianb~1 is your account.


                    At first it did not. After using PKZip.exe it did, but it contained files other than the ones from the TEXTZ folder.

                    The trouble I was having was that I refferenced a thumb drive for the test(I:\) and once there it could not change directories back to the hard drive(C:\).  Not that I found this out the original code you wrote might work.

                    I am going to revisit it.

                    Yes brianb~1 is my account.  I also searched all drives for the file but found nothing.

                    P.S. What does the "/d" after the CD command designate? It doesn't show up as syntax for the DOS CD command reference.


                    ionic

                      Topic Starter


                      Rookie

                      Re: Zipping files using batch file
                      « Reply #14 on: January 04, 2008, 04:23:52 PM »
                      Sidewinder,

                      I got your code to run. as wel as a PKZip version.  It might be possible for PKZip to do it all by itself, but for now I am satisfied.

                      This is the code that worked for me:

                      Code: [Select]
                      cd /d d:\
                      md TEXTZ
                      cd /d d:\testfold\
                      for /f "tokens=* delims=" %%x in ('dir /s /b /a:-d *.txt') do (
                      copy "%%x" d:\TEXTZ\
                      )

                      CD /d C:\Docume~1\brianb~1\desktop\
                      cscript grabnzip.vbs

                      rd d:\TEXTZ /s/q
                      exit

                      Code: [Select]
                      strTargetFile = "C:\docume~1\brianb~1\desktop\TEXTZ.zip"         

                      Set fso = CreateObject("Scripting.FileSystemObject")
                      Set zip = fso.CreateTextFile(strTargetFile)
                      zip.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
                      zip.Close
                      WScript.Sleep 1000

                      Set objShell = CreateObject("Shell.Application")
                      Set objFile = objShell.NameSpace(strTargetFile)
                      Set src = objShell.NameSpace("D:\TEXTZ\")   

                      objFile.CopyHere src
                      WScript.Sleep 15000

                      I'd still like to know what CD /d means...

                      Thanks for your help.

                      patio, thanks for your insite as well.  I will be examining the POWER of pkzip
                      soon!


                      Forgot...here is the batch file code using pkzip.

                      Code: [Select]
                      @echo off
                      cd /d d:\
                      md TEXTZ
                      cd /d d:\testfold\
                      for /f "tokens=* delims=" %%x in ('dir /s /b /a:-d *.txt') do (
                      copy "%%x" d:\TEXTZ\
                      )
                      call PKZIP c:\docume~1\brianb~1\desktop\TXTZ.ZIP d:\TEXTZ\*.*
                      pause

                      rd d:\TEXTZ /s /q
                      exit

                      Brian
                      « Last Edit: January 04, 2008, 04:38:47 PM by ionic »