none
Renaming a file to match a wildcard result RRS feed

  • Question

  • I am trying to compress a file using built-in Windows tools. I need to use a wildcard to read the file because the input filename changes slightly each day (to reflect the date stamp). The current batch file that I have found will ZIP up a file based on a wildcard, but it gives the resulting ZIP output file a static name.

    I need it to have the exact same name as the input file.

    E.g.: if the input filename is blahblahblah02132014.txt, the output filename needs to be blahblahblah02132014.zip.

    Please help?

    Here is my current script:

    set FILETOZIP=C:\Users\<my.name>\Documents\blah*.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%\blah*.*
    rmdir %TEMPDIR%
    
        pause


    Tuesday, February 18, 2014 9:31 PM

Answers

  • Hi. It is working now. I was using "rename" instead of "move".

    Broken version:

    @echo on
    setlocal enableextensions
    set IN_NAME=C:\Users\<my.name>\Documents\blah*.txt
    for /f "delims=" %%I in ('echo %IN_NAME%') do set OUT_NAME=%%~dpnI.zip
    rename C:\Users\<my.name>\Documents\ZipFile.zip %OUT_NAME%
    endlocal

    This next one works. Thanks for all of your help and patience.

    @echo on
    setlocal enableextensions
    set IN_NAME=C:\Users\<my.name>\Documents\blah*.txt
    for /f "delims=" %%I in ('echo %IN_NAME%') do set OUT_NAME=%%~dpnI.zip
    move C:\Users\<my.name>\Documents\ZipFile.zip %OUT_NAME%
    endlocal

    Wednesday, February 19, 2014 5:08 PM

All replies

  • HELP will help

    I posted that on you other thread.   Read the help for SET


    ¯\_(ツ)_/¯

    Tuesday, February 18, 2014 9:52 PM
  • You can use cmd.exe's for /f command. Sample script:


    @echo off
    setlocal enableextensions
    set IN_NAME=C:\Temp\blahblahblah02132014.txt
    for /f %%I in ('echo %IN_NAME%') do set OUT_NAME=%%~dpnI.zip
    echo %OUT_NAME%
    endlocal
    

    Script output:


    C:\Temp\blahblahblah02132014.zip
    

    Enter the command for /? at a cmd.exe prompt for more information.

    Bill

    Tuesday, February 18, 2014 10:27 PM
    Moderator
  • You can use cmd.exe's for /f command. Sample script:


    .

    .

    .

    Bill

    Thanks, Bill.

    Almost there. I forgot to mention that the desired output filename contains spaces. Sorry.

    How do I use the rename command to accommodate this?

    rename C:\Users\<my.name>\Documents\ZipFile.zip %OUT_NAME%

    The actual syntax of the filename is "aaaa bbbb cccc for 20140317.txt", so there are four (4) spaces in all.

    Again, I apologize. I did not realize earlier that this would be significant.


    • Edited by Dougmeister2 Tuesday, February 18, 2014 11:10 PM
    Tuesday, February 18, 2014 11:08 PM
  • Here is a built-in zipper for Windows: http://blogs.technet.com/b/heyscriptingguy/archive/2010/05/22/hey-scripting-guy-weekend-scripter-using-windows-powershell-to-compress-folders.aspx


    ¯\_(ツ)_/¯


    Thanks, but unless I'm mistaken, that solution uses a PowerShell script, and I don't have the permissions to execute those on my machine.
    Tuesday, February 18, 2014 11:09 PM
  • What do you mean you don't have permission?  Have you tried?

    ¯\_(ツ)_/¯

    Tuesday, February 18, 2014 11:30 PM
  • With a minor modification (delims=), it works with spaces:


    @echo off
    setlocal enableextensions
    set IN_NAME=C:\Temp\blah blah blah 02132014.txt
    for /f "delims=" %%I in ('echo %IN_NAME%') do set OUT_NAME=%%~dpnI.zip
    echo %OUT_NAME%
    endlocal
    

    Output:


    C:\Temp\blah blah blah 02132014.zip
    

    Bill


    Wednesday, February 19, 2014 1:21 AM
    Moderator
  • Bill, the line I am using is this:

    >set IN_NAME=C:\Users\<my.name>\Documents\blah*.txt

    with the wildcard.

    So my code looks like this:

    setlocal enableextensions
    set IN_NAME=C:\Users\<my.name>\Documents\blah*.txt
    for /f "delims=" %%I in ('echo %IN_NAME%') do set OUT_NAME=%%~dpnI.zip
    echo %OUT_NAME%
    rename C:\Users\<my.name>\Documents\ZipFile.zip %OUT_NAME%
    endlocal

    and I'm getting a syntax error on the "rename" command. ("The syntax of the command is incorrect.")

    Wednesday, February 19, 2014 4:25 AM

  • @echo off
    setlocal enableextensions
    set IN_NAME=C:\Temp\*.txt
    for %%I in (%IN_NAME%) do echo %%~dpnI.zip
    endlocal
    

    This sample script will output the full path and filename of all *.txt files in C:\Temp, but with a .zip extension for each file.

    Enter the command for /? at a cmd.exe prompt more information.

    Bill


    Wednesday, February 19, 2014 3:40 PM
    Moderator
  • Thanks.

    I can get the right filename now, with the ZIP extension and everything.

    But I'm still getting the error when I try to rename the file. This line:

    rename C:\Users\<my.name>\Documents\ZipFile.zip %OUT_NAME%

    gives the following error when it executes:

    "The syntax of the command is incorrect."

    The environment variable %OUT_NAME% is not being set in your last example, but I think you were doing that to teach me something.

    I used the code from your prior example again:

    for /f "delims=" %%I in ('echo %IN_NAME%') do set OUT_NAME=%%~dpnI.zip

    and I can see that the correct filename ("C:\Users\<my.name>\Documents\Blah blah blah 20140218.zip") is being set in the environment variable %OUT_NAME%. But again, when I go to rename "ZipFile.zip" to that new name, I get the syntax error. I think it is because of the spaces in the filename.

    Sorry for making this harder than it needs to be...

    Wednesday, February 19, 2014 4:29 PM
  • Hi,

    Please post a short example script that contains only the minimum amount of code needed to reproduce the problem.

    Bill

    Wednesday, February 19, 2014 4:38 PM
    Moderator
  • Hi. It is working now. I was using "rename" instead of "move".

    Broken version:

    @echo on
    setlocal enableextensions
    set IN_NAME=C:\Users\<my.name>\Documents\blah*.txt
    for /f "delims=" %%I in ('echo %IN_NAME%') do set OUT_NAME=%%~dpnI.zip
    rename C:\Users\<my.name>\Documents\ZipFile.zip %OUT_NAME%
    endlocal

    This next one works. Thanks for all of your help and patience.

    @echo on
    setlocal enableextensions
    set IN_NAME=C:\Users\<my.name>\Documents\blah*.txt
    for /f "delims=" %%I in ('echo %IN_NAME%') do set OUT_NAME=%%~dpnI.zip
    move C:\Users\<my.name>\Documents\ZipFile.zip %OUT_NAME%
    endlocal

    Wednesday, February 19, 2014 5:08 PM
  • It is best to quote the path/filename parameters to rename or move to ensure your intent.

    Bill

    Wednesday, February 19, 2014 5:55 PM
    Moderator