none
Batch file to compress wildcard target to ZIP file with same name RRS feed

  • Question

  • We are not allowed to use WinZip command line, compress.exe from the Windows Toolkit, PowerShell , or anything else for that matter.

    I have found a few batch files that will "work", but the result is a fixed-name output file.

    Example from Tom Lavedas

    I need the output filename to be dynamic. Using a wildcard for the input (e.g., "DDmmmYYYY*.txt") that will only ever match to one file, but that name changes daily because it has the date embedded in it.

    I'm sure it's a simple tweak. Can anyone help? Thanks.

    Tuesday, February 18, 2014 2:10 PM

Answers

All replies

  • Hi,

    You can pass the filename you want to use in as an argument and set sFilename that way.

    http://ss64.com/vb/arguments.html

    Or you can set sFilename based on Now by using something like this:

    Replace(Replace(Now,"/","-"),":",".")

    http://ss64.com/vb/now.html


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Tuesday, February 18, 2014 2:24 PM
  • Sorry, but I should have tested Tom's script first. It is not working for me. It just says "done" but the output file is not being created.

    How can I debug this? I don't see any output on my screen...

    Tuesday, February 18, 2014 3:24 PM
  • Sorry, but I should have tested Tom's script first. It is not working for me. It just says "done" but the output file is not being created.

    How can I debug this? I don't see any output on my screen...

    Can't really tell unless you post your version of Tom's script. Note also that you must run the script from the console in order to see error messages.
    Tuesday, February 18, 2014 3:35 PM
  • The Shell.Application object' CopyHere method runs asynchronously (meaning it returns immediately without waiting) and has no error checking, so it is not suitable for automation. More information in this thread:

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/760ea95b-4c3e-4f48-a0f6-9d728d5580da/

    You will need to use an alternate solution.

    Bill

    Tuesday, February 18, 2014 3:46 PM
    Moderator
  • Can't really tell unless you post your version of Tom's script. Note also that you must run the script from the console in order to see error messages.

    How do I run the CScript console? We don't have Command line access, either...

    My batch file looks like this:

    cscript //nologo C:\Users\<my.name>\Documents\ZipIt.vbs

    My version of Tom's script looks like this:

    sDestination = "C:\Users\<my.name>\Documents"
    sSource = "C:\Users\<my.name>\Documents"
    sFilename = "test*.txt"
    
    copyToCompressed sSource, sDestination, sFilename
    
    wsh.echo "Done"
    
    WScript.Sleep 2000 
    
    sub copyToCompressed(sSourcePath, sDestinationPath, sFilename)
    
    Dim oFolder, oFile
    Const undoPreserve       =   64 'Preserve undo information, if possible
    Const displayProgress    =  256 'Display progress box without file names
    
    With CreateObject("Shell.Application")
      set ofolder = .NameSpace(sDestinationPath)
      set ofile   = .NameSpace(sSourcePath).ParseName(sFilename)
      ofolder.CopyHere ofile, displayProgress + undoPreserve
    end with
    	
    end sub

    Tuesday, February 18, 2014 3:51 PM
  • How do I run the CScript console? We don't have Command line access, either...

    In my opinion, you have an organizational and not a technical problem. you need to be given sufficient access to do things and the proper tools to do your job.

    Bill

    Tuesday, February 18, 2014 3:54 PM
    Moderator
  • How do I run the CScript console? We don't have Command line access, either...

    You're in an impossible spot. Whoever is setting your requirements needs a reality check.

    The simplest way to handle this is the 7zip standalone utility.

    If you really have to 'compress' things with builtin tools only, try makecab.exe. It's not good compression, but it'll fool a manager.


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Tuesday, February 18, 2014 3:54 PM
  • >In my opinion, you have an organizational and not a technical problem. you need to be given
    >sufficient access to do things and the proper tools to do your job.

    >You're in an impossible spot. Whoever is setting your requirements needs a reality check.

    Tell me about it...

    Tuesday, February 18, 2014 4:01 PM
  • PowerShell can zip files natively at PowerShell V3 and later.


    ¯\_(ツ)_/¯

    Tuesday, February 18, 2014 4:19 PM
  • This worked, but I still end up with a static name for the output file:

    set FILETOZIP=C:\Users\<my.name>\Documents\test*.txt
    
    
        set TEMPDIR=C:\temp738
        mkdir %TEMPDIR%
        copy %FILETOZIP% %TEMPDIR%
    
        echo Set objArgs = WScript.Arguments > _zipIt.vbs
        echo InputFolder = objArgs(0) >> _zipIt.vbs
        echo ZipFile = objArgs(1) >> _zipIt.vbs
        echo CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" ^& Chr(5) ^& Chr(6) ^& String(18, vbNullChar) >> _zipIt.vbs
        echo Set objShell = CreateObject("Shell.Application") >> _zipIt.vbs
        echo Set source = objShell.NameSpace(InputFolder).Items >> _zipIt.vbs
        echo objShell.NameSpace(ZipFile).CopyHere(source) >> _zipIt.vbs
        echo wScript.Sleep 2000 >> _zipIt.vbs
    
        CScript  _zipIt.vbs  %TEMPDIR%  C:\Users\<my.name>\Documents\someArchive.zip
    
    echo Y | del %TEMPDIR%\Batch*.*
    rmdir %TEMPDIR%
    
        pause

    Tuesday, February 18, 2014 4:21 PM
  • It is likely that the user session has script execution blocked. That is why there are no errors.  With user scripting blocked the click on the file just blinks but nothing happens.

    There is no way for a user with these settings to zip a file except by using the GUI.  Just right click on the file and select SendTo Compressed File.  This is usually not blocked in a locked down system.

    Many companies block all access for CSR reps to keep them from playing around.  I have even been asked to force run the CSR program as the shell.  This can be done at times and blocks all access to Windows. Exit the program and you are logged off.

    If a company set this up so you cannot access Explorer or run scripts then there is nothing you can do. Since Snowden I notice that everyone working as a basic operator or CSR is now trying to squirrel away as much data as possible.  They have learned that there might be a market for it. This forces companies to block all access to files and to disable all screen shot capability.  I had my credit info stolen by a mobile carrier employee scavenging files.  Luckily the card expired and I forgot to activate my new card when I started receiving messages from various places telling me the card had been used but rejected.

    If you are an employee and are trying to circumvent any corporate security policies you may be subject to federal charges depending on the data and the company.  Be careful what you try to copy.  Remember that the NSA is still looking and will be until Congress can straighten all of this out.


    ¯\_(ツ)_/¯

    Tuesday, February 18, 2014 4:30 PM
  • Just trying to do my job. With existing policies, permission to get new software is almost impossible to get, even if it is free and from an approved vendor such as WinZip.
    Tuesday, February 18, 2014 4:37 PM
  • Surprisingly, I *do* have Microsoft Access, Excel, VBA, etc.

    Could that be a solution?

    Tuesday, February 18, 2014 4:53 PM
  • I would recommend getting approval to allow access to the command prompt and to install a good freeware tool such as 7-Zip.

    My $0.02 is that disallowing access to the command prompt does not in any way increase an organization's security posture and is merely an impediment to legitimate use. Cmd.exe is a program, and programs are not security boundaries.

    http://sincealtair.blogspot.com/2009/12/disabling-command-prompt-does-not.html

    You need to be allowed access to the tools that let you do your job.

    Bill

    Tuesday, February 18, 2014 5:37 PM
    Moderator
  • Thanks again for everyone's advice.

    I will pursue getting additional tools and/or access, but in the meantime, shouldn't it be possible, via batch file only, to rename a static filename to the name of the original file (minus the ".txt" extension)?

    If that is impossible, the date stamp is *usually* the prior day. So for example, the file received today (February 18th, 2014) would have a name like "blah blah blah 02172014.txt".

    Tuesday, February 18, 2014 6:06 PM
  • Thanks again for everyone's advice.

    I will pursue getting additional tools and/or access, but in the meantime, shouldn't it be possible, via batch file only, to rename a static filename to the name of the original file (minus the ".txt" extension)?

    If that is impossible, the date stamp is *usually* the prior day. So for example, the file received today (February 18th, 2014) would have a name like "blah blah blah 02172014.txt".


    7Zip will do all f that for your including a file stamp.

    ¯\_(ツ)_/¯

    Tuesday, February 18, 2014 6:09 PM
  • 7Zip is not an option.

    Winzip command line *might* be, but I've been trying to get that for months with no luck.

    Tuesday, February 18, 2014 6:41 PM
  • You need access to the correct tools. We can't help you script around limitations that prevent you from doing your work properly.

    Bill

    Tuesday, February 18, 2014 7:17 PM
    Moderator
  • I'm working on it, Bill, but I still don't see why it isn't possible to grab a filename from DOS (using batch commands, Wscript, CScript, vbs, etc.) and rename the statically-named ZIP file that I've already been able to create using my limited toolset.
    Tuesday, February 18, 2014 8:45 PM
  • Sorry, but I don't have a solution for your current scenario. Your problem, as noted, is really an organizational problem, not a scripting problem.

    If you have a different question about renaming files, please start a new question and be as specific as possible about what you want to do.

    Bill

    Tuesday, February 18, 2014 8:48 PM
    Moderator
  • I'm working on it, Bill, but I still don't see why it isn't possible to grab a filename from DOS (using batch commands, Wscript, CScript, vbs, etc.) and rename the statically-named ZIP file that I've already been able to create using my limited toolset.

    If you know what the filename is going to be, you can use ren to rename it:

    http://ss64.com/nt/ren.html


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Tuesday, February 18, 2014 9:05 PM
  • Thanks, Bill. I will do that.

    Mike, I don't "know" what the filename is going to be... at least within the batch file. I am using wildcards to work with the file. I will be starting a new thread as Bill suggested.

    Tuesday, February 18, 2014 9:25 PM
  • Thanks, Bill. I will do that.

    Mike, I don't "know" what the filename is going to be... at least within the batch file. I am using wildcards to work with the file. I will be starting a new thread as Bill suggested.

    Type SET /? at a prompt and read how to use the set command to extract a file name.


    ¯\_(ツ)_/¯

    Tuesday, February 18, 2014 9:28 PM
  • Tuesday, February 18, 2014 9:29 PM