Ilya_Kuvshi
Ilya_Kuvshi

Reputation: 51

How to zip folders containing today's date in the filename?

I have a batch and a vbs file to zip a folder with a specific directory name and copy it to another folder.

How could I zip only the folder containing today's date? If it is not possible how could I zip only the folder having today's date as "Date Modified" column?

bat:

@echo off
set "mypath=C:\TEMP\zip\source\JEAR_20190115"
for /f %%i in ('dir /b /a-d "%mypath%"') do set "last=%%~ni"
CScript zip.vbs %mypath% C:\TEMP\zip\target\%last%.zip

vbs:

'Get command-line arguments.
Set objArgs = WScript.Arguments
InputFolder = objArgs(0)
ZipFile = objArgs(1)

'Create empty ZIP file.
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile,         
True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)

Set objShell = CreateObject("Shell.Application")

Set source = objShell.NameSpace(InputFolder).Items

objShell.NameSpace(ZipFile).CopyHere(source)

'Required!
wScript.Sleep 2000

For example the following file is older than today so it shouldn't be zipped.

set "mypath=C:\TEMP\zip\source\JEAR_20190115"

If it would be equal to today's date the script should run:

set "mypath=C:\TEMP\zip\source\JEAR_20190117"

Previous question: https://stackoverflow.com/a/54216776/7420833

Upvotes: 2

Views: 829

Answers (1)

user7818749
user7818749

Reputation:

The explanations remains the same for the rest, as per the previous question, but we simply add some vbs code to get the correct date in the required format.

@echo off
echo >"%temp%\%~n0.vbs" s=DateAdd("d",0,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
if exist "C:\TEMP\zip\source\JEAR_%result%" (
    set "mypath=C:\TEMP\zip\source\JEAR_%result%"
 )  else (
    exit
 )
if "%mypath:~-1%"=="\" set "mypath=%mypath:~0,-1%"
for %%i in (%mypath%) do set "last=%%~nxi"
CScript zip.vbs %mypath% C:\TEMP\zip\target\%last%.zip

The reason for using

Upvotes: 2

Related Questions