To Compress files using gzip utility

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
vsmeruga
Posts: 35
Joined: 04 Jun 2014 06:42

To Compress files using gzip utility

#1 Post by vsmeruga » 04 Jun 2014 06:54

Dear gurus

I am new to windows batch scripting and struggling to achieve a task.
I have to create a script - to compress files particular files older than 90 days in its directory and corresponding sub directories
(Note: Compressed file should stay on the same directory or sub directory)

I have written a script with the help of google and this is not solving the purpose now.
It tries to delete all the files older than 90 days first and compresses the remaining later on.
** Now I should not delete them but just compress them . Is it possible to modify the below script

Code: Select all

@setlocal enabledelayedexpansion
@echo off

REM Setting root path and changing the directory to it

set ArPathname=D:\Data
pushd %ArPathname%
echo %ArPathname%

REM Deletes the files older than 90 days which matches the search criteria

forfiles -p %ArPathname% -s -m *2012*.txt -d -90 -c "cmd /c del @path"
forfiles -p %ArPathname% -s -m *201302*.csv -d -90 -c "cmd /c del @path"

echo after 90 days delete

REM Compresses the files using gzip which are less than 90 days old in the same folder
path=%path%;"D:\Data\Test"
for /f %%a in ('dir /s /b *2012*.txt *201302*.csv') do if not exist %%a.gz echo %%a | gzip %%a
echo after gzip

goto :eof


Can you please help me on this.
Many Thanks
Sheku

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: To Compress files using gzip utility

#2 Post by foxidrive » 04 Jun 2014 07:53

If your gzip command is correct then this may work for you.

Code: Select all

@echo off
pushd "d:\data" || goto :EOF
for /f "delims=" %%a in ('dir /s /b /a-d *2012*.txt *201302*.csv') do if not exist "%%a.gz" echo "%%a" & gzip "%%a"
popd
pause

vsmeruga
Posts: 35
Joined: 04 Jun 2014 06:42

Re: To Compress files using gzip utility

#3 Post by vsmeruga » 04 Jun 2014 08:51

Getting the error message as below:

"D:\Data\20120219_file.txt"
'gzip' is not recognized as an internal or external command,


Code: Select all


@echo off
pushd "D:\Data" || goto :EOF
for /f "delims=" %%a in ('dir /s /b /a-d *2012*.txt *201302*.csv') do if not exist "%%a.gz" echo "%%a" & gzip "%%a"
popd
pause

vsmeruga
Posts: 35
Joined: 04 Jun 2014 06:42

Re: To Compress files using gzip utility

#4 Post by vsmeruga » 04 Jun 2014 09:10

Hi foxidrive

are we not specifying this 90 days criteria in the command?. which is the main issue.
Thanks
VJ

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: To Compress files using gzip utility

#5 Post by foxidrive » 04 Jun 2014 10:37

If you don't have gzip then you aren't going to .gz anything. :D

I was confused with your question, you said one thing and then said it changed and the task was different and you should just compress them.

vsmeruga
Posts: 35
Joined: 04 Jun 2014 06:42

Re: To Compress files using gzip utility

#6 Post by vsmeruga » 05 Jun 2014 02:43

Hi foxidrive
The below piece of code compress the files perfectly. But the issue is I am not able to set the criteria(only to compress files older than 90 days to it.)
I hope my question is clear to you now.
Thanks
VJ


Code: Select all

REM Compresses the files using gzip which are less than 90 days old in the same folder
path=%path%;"D:\Data\Test"
for /f %%a in ('dir /s /b *2012*.txt *201302*.csv') do if not exist %%a.gz echo %%a | gzip %%a
echo after gzip


foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: To Compress files using gzip utility

#7 Post by foxidrive » 05 Jun 2014 03:11

Test this on some sample files: if your gzip command is right then it should work

Code: Select all

@echo off
setlocal enabledelayedexpansion

REM Setting root path and changing the directory to it

set ArPathname=D:\Data
pushd %ArPathname%
echo %ArPathname%

:: get the date 90 days ago
@echo off
set day=-90
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,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"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "data=%dd%/%mm%/%yyyy%"

forfiles -p %ArPathname% -s -m *2012*.txt -d %data%  -c "cmd /c echo @path" >"file.tmp"
forfiles -p %ArPathname% -s -m *201302*.csv -d %data% -c "cmd /c echo @path" >>"file.tmp"

REM Compresses the files using gzip which are less than 90 days old in the same folder
path=%path%;"D:\Data\Test"
for /f "delims=" %%a in (file.tmp) do if not exist "%%~dpnxa.gz" echo "%%~dpnxa"|gzip "%%~dpnxa"
echo after gzip
del file.tmp
goto :eof

vsmeruga
Posts: 35
Joined: 04 Jun 2014 06:42

Re: To Compress files using gzip utility

#8 Post by vsmeruga » 05 Jun 2014 04:40

Sure Thanks.

vsmeruga
Posts: 35
Joined: 04 Jun 2014 06:42

Re: To Compress files using gzip utility

#9 Post by vsmeruga » 05 Jun 2014 04:49

Output of the script:
============
days are 07/03/2014
ERROR: No files found with the specified search criteria.
ERROR: No files found with the specified search criteria.
after temp file logic
after gzip
-- output of %data% is coming as date: 07/03/2014


vsmeruga
Posts: 35
Joined: 04 Jun 2014 06:42

Re: To Compress files using gzip utility

#10 Post by vsmeruga » 05 Jun 2014 04:57

And the actual file modified date in the folder are "2/5/2013 11:00 AM". It means this file is older than 90 days right!

vsmeruga
Posts: 35
Joined: 04 Jun 2014 06:42

Re: To Compress files using gzip utility

#11 Post by vsmeruga » 05 Jun 2014 05:04

I REPLACED %data% with -90 and seems to be working.

Thanks
VJ

vsmeruga
Posts: 35
Joined: 04 Jun 2014 06:42

Re: To Compress files using gzip utility

#12 Post by vsmeruga » 05 Jun 2014 05:21

Many Thanks foxidrive - for your time and effort on this issue.
VJ

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: To Compress files using gzip utility

#13 Post by foxidrive » 05 Jun 2014 07:49

That was my mistake. I had the wrong end of the stick again and was using files that were less than 90 days old.

Post Reply