Batch to archive files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
tiagocampos
Posts: 38
Joined: 21 Feb 2013 12:41

Re: Batch to archive files

#16 Post by tiagocampos » 25 Feb 2013 05:30

It's working properly now :)

This is the result for one of the folders: http://prntscr.com/u3hbt

But the .js and .txt files didn't work right: http://prntscr.com/u3hox

It's hard to make an exception to delete the folders you create after you zip them?
And importing the directory to run the script from a csv file instead of current directory?

What you done so far was extremely helpful, thank you!

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

Re: Batch to archive files

#17 Post by foxidrive » 25 Feb 2013 07:42

Those files may not have been more than 30 days old - check them and let me know.

And the folders that are created - do you want to delete them after the ZIP files are created? Is that what you mean?

tiagocampos
Posts: 38
Joined: 21 Feb 2013 12:41

Re: Batch to archive files

#18 Post by tiagocampos » 25 Feb 2013 08:02

foxidrive wrote:Those files may not have been more than 30 days old - check them and let me know.


How stupid of me, of course not, you're right!

foxidrive wrote:And the folders that are created - do you want to delete them after the ZIP files are created? Is that what you mean?


Yes exactly.

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

Re: Batch to archive files

#19 Post by foxidrive » 25 Feb 2013 08:28

EDITED:

This is the same batch file but it has a line to delete the folders after they have been ZIP'ed up. rd /s /q "%%b"


To process a set of folders from a plain text file, one c:\folder\source folder per line then change this in two places

for /f "delims=" %%a in ('dir /a:d /b') do (

To this

for /f "delims=" %%a in (file.txt) do (

with the drv:\folder list in file.txt and test it before using it on real data.

If your list is faulty then it could delete data that you need, from the folders below where the batch file is located.


Code: Select all

@echo off
call :getdate today -30

for /f "delims=" %%a in ('dir /a:d /b') do (
pushd "%%a"
 for /f "delims=" %%b in ('dir /b') do (
  for /f "tokens=1,2,3 delims=/-" %%c in ('forfiles /M "%%b" /c "cmd /c echo @fdate"') do (
   echo checking "%%b"
   forfiles /m "%%b" /d -%day% /c "cmd /c MD 0x22%%e_%%d0x22 2>nul & move @file 0x22%%e_%%d0x22 >nul"
  )
 )
popd
)

for /f "delims=" %%a in ('dir /a:d /b') do (
pushd "%%a"
for /f "delims=" %%b in ('dir "????_??" /ad /b') do (
   pushd "%%b"
   echo Making ZIP file from "%%b" folder
   "c:\Program Files\7-Zip\7z.exe" a -r -tzip "%%b.zip" *
   move "%%b.zip" .. >nul
   popd
 rd /s /q "%%b"
   )
popd
)

pause
goto :EOF


:getdate
:: Date foward & backward
@echo off
:: from code by Phil Robyn
setlocal
if [%1]==[] (
  echo to get todays date use
  echo call "%~n0" today 0
  echo.
  echo to get yesterdays date use
  echo call "%~n0" today -1
  echo.
  echo to get the date 25 days ago:
  echo call "%~n0" today -25
  echo.
  echo to get the date 1250 days in the future
  echo call "%~n0" today +1250
  goto :EOF)

set date1=%1
set qty=%2
if /i "%date1%" EQU "TODAY" (
 set date1=now
) else (
 set date1="%date1%"
)
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)
for /f %%a in (
  'cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set "day=%result:~6,2%-%result:~4,2%-%result:~0,4%"
:: echo %%day%% is set to "%day%" (without the quotes)

tiagocampos
Posts: 38
Joined: 21 Feb 2013 12:41

Re: Batch to archive files

#20 Post by tiagocampos » 26 Feb 2013 04:41

It's working very smoothly :D

But I have one concern regarding the archiving process, I'll try to explain in my bad English :P

I require the files to be older than 30 days and then I group them in folders by month.
What if on 15th February I run this script? I will possibly archive files from 10th January for example, but files from 16th January won't. So 2 days later I run the script again, now the files from 16th January that I didn't archive will be archived, but will overwrite the previous January folder zip.

Do you have any advice to solve this matter?

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

Re: Batch to archive files

#21 Post by foxidrive » 26 Feb 2013 05:25

One way to solve that is to add the date and time to the filename. in YYYYMMDDHHMM_YYYY_MM format it will sort nicely in a folder and you will have an archive for every time it is run.

Note that the _YYYY_MM will be different from YYYYMMDD

tiagocampos
Posts: 38
Joined: 21 Feb 2013 12:41

Re: Batch to archive files

#22 Post by tiagocampos » 26 Feb 2013 12:23

foxidrive wrote:One way to solve that is to add the date and time to the filename. in YYYYMMDDHHMM_YYYY_MM format it will sort nicely in a folder and you will have an archive for every time it is run.

Note that the _YYYY_MM will be different from YYYYMMDD


Can you make the changes accordantly?
I have changed the last version to move all zipped folders to a folder named Archive. This is how it looks now:

Code: Select all

@echo off
call :getdate today -30
:: To specify the location of the file from where we import the list of directories to take action
set file=C:\file.txt

::for /f "delims=" %%a in ('dir /a:d /b') do (
for /f "delims=" %%a in (%file%) do (

pushd "%%a"
 for /f "delims=" %%b in ('dir /b') do (
  for /f "tokens=1,2,3 delims=/-" %%c in ('forfiles /M "%%b" /c "cmd /c echo @fdate"') do (
   echo checking "%%b"
   forfiles /m "%%b" /d -%day% /c "cmd /c MD 0x22%%e_%%d0x22 2>nul & move @file 0x22%%e_%%d0x22 >nul" 
  )
 )
popd
)

::for /f "delims=" %%a in ('dir /a:d /b') do (
for /f "delims=" %%a in (%file%) do (
pushd "%%a"
md Archive
for /f "delims=" %%b in ('dir "????_??" /ad /b') do (
   pushd "%%b"
   echo Making ZIP file from "%%b" folder
   "c:\Program Files\7-Zip\7z.exe" a -r -tzip "%%b.zip" *
   move "%%b.zip" .. >nul
   popd
   rd /s /q "%%b"
   move /y "%%b.zip" Archive
   )
popd
)

pause
goto :EOF


:getdate
:: Date foward & backward
@echo off
:: from code by Phil Robyn
setlocal
if [%1]==[] (
  echo to get todays date use
  echo call "%~n0" today 0
  echo.
  echo to get yesterdays date use
  echo call "%~n0" today -1
  echo.
  echo to get the date 25 days ago:
  echo call "%~n0" today -25
  echo.
  echo to get the date 1250 days in the future
  echo call "%~n0" today +1250
  goto :EOF)

set date1=%1
set qty=%2
if /i "%date1%" EQU "TODAY" (
 set date1=now
) else (
 set date1="%date1%"
)
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)
for /f %%a in (
  'cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set "day=%result:~6,2%-%result:~4,2%-%result:~0,4%"
:: echo %%day%% is set to "%day%" (without the quotes)


Thank you very much.

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

Re: Batch to archive files

#23 Post by foxidrive » 26 Feb 2013 19:23

Try this

The changes are in lines 3-5 which are added to create a timestamp of the current day_time (XP Home may not have the WMIC tool it needs)
and in the archiving section "%%b.zip" is changed to "%dt%_%%b.zip" in three places.

I haven't tested it.

Code: Select all

@echo off
call :getdate today -30

:: get timestamp in YYYYMMDD_HHMMSS format
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set dt=%dt:~0,8%_%dt:~8,6%

:: To specify the location of the file from where we import the list of directories to take action
set file=C:\file.txt

::for /f "delims=" %%a in ('dir /a:d /b') do (
for /f "delims=" %%a in (%file%) do (

pushd "%%a"
 for /f "delims=" %%b in ('dir /b') do (
  for /f "tokens=1,2,3 delims=/-" %%c in ('forfiles /M "%%b" /c "cmd /c echo @fdate"') do (
   echo checking "%%b"
   forfiles /m "%%b" /d -%day% /c "cmd /c MD 0x22%%e_%%d0x22 2>nul & move @file 0x22%%e_%%d0x22 >nul"
  )
 )
popd
)

::for /f "delims=" %%a in ('dir /a:d /b') do (
for /f "delims=" %%a in (%file%) do (
pushd "%%a"
md Archive
for /f "delims=" %%b in ('dir "????_??" /ad /b') do (
   pushd "%%b"
   echo Making ZIP file from "%%b" folder
   "c:\Program Files\7-Zip\7z.exe" a -r -tzip "%dt%_%%b.zip" *
   move "%dt%_%%b.zip" .. >nul
   popd
   rd /s /q "%%b"
   move /y "%dt%_%%b.zip" Archive
   )
popd
)

pause
goto :EOF


:getdate
:: Date foward & backward
@echo off
:: from code by Phil Robyn
setlocal
if [%1]==[] (
  echo to get todays date use
  echo call "%~n0" today 0
  echo.
  echo to get yesterdays date use
  echo call "%~n0" today -1
  echo.
  echo to get the date 25 days ago:
  echo call "%~n0" today -25
  echo.
  echo to get the date 1250 days in the future
  echo call "%~n0" today +1250
  goto :EOF)

set date1=%1
set qty=%2
if /i "%date1%" EQU "TODAY" (
 set date1=now
) else (
 set date1="%date1%"
)
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)
for /f %%a in (
  'cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set "day=%result:~6,2%-%result:~4,2%-%result:~0,4%"
:: echo %%day%% is set to "%day%" (without the quotes)

tiagocampos
Posts: 38
Joined: 21 Feb 2013 12:41

Re: Batch to archive files

#24 Post by tiagocampos » 01 Mar 2013 06:35

Running at full steam :D

I tried to change the order, so the timestamp would appear after the year and month but I failed.

I changed in those three places the %dt%_%%b.zip for %%b_%dt%.zip but nothing.

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

Re: Batch to archive files

#25 Post by foxidrive » 01 Mar 2013 08:45

tiagocampos wrote:Running at full steam :D

I tried to change the order, so the timestamp would appear after the year and month but I failed.

I changed in those three places the %dt%_%%b.zip for %%b_%dt%.zip but nothing.



That should do it. Try it again - you may have not saved the changes correctly.

Use "%%b_%dt%.zip" in three places.

tiagocampos
Posts: 38
Joined: 21 Feb 2013 12:41

Re: Batch to archive files

#26 Post by tiagocampos » 05 Mar 2013 06:29

Solved!

Another issue appeared as I tested it massively: I will need to add file exceptions :/

For a specific type of folder, I have to not include the file named "base_var.js" for example, and more might be needed.

Is there a away to include this in a similar way to what's already done with the folders to process? Maybe a .txt file with the names of the files not to process.

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

Re: Batch to archive files

#27 Post by foxidrive » 05 Mar 2013 06:39

tiagocampos wrote:Another issue appeared as I tested it massively: I will need to add file exceptions :/

For a specific type of folder, I have to not include the file named "base_var.js" for example, and more might be needed.


Are you saying that "base_var.js" has to be excluded in one folder, but not in other folders?
Is that the kind of exclusion you need?

tiagocampos
Posts: 38
Joined: 21 Feb 2013 12:41

Re: Batch to archive files

#28 Post by tiagocampos » 05 Mar 2013 06:40

It can be excluded in all folders, as it's a specific file for 1 type of folder and doesn't exist in others except for this type (don't know if I was clear enough).

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

Re: Batch to archive files

#29 Post by foxidrive » 05 Mar 2013 06:52

With this change it should exclude certain files and folders.

In c:\exclusions.txt put things like this inside, one filetype per line.
base_var.js
ice*.js
*.bin


This segment starts with line 10.

pushd "%%a"
for /f "delims=" %%b in ('dir /b ^|findstr /v /i /g:"c:\exclusions.txt" ') do (
for /f "tokens=1,2,3 delims=/-" %%c in ('forfiles /M "%%b" /c "cmd /c echo @fdate"') do (
echo checking "%%b"
forfiles /m "%%b" /d -%day% /c "cmd /c MD 0x22%%e_%%d0x22 2>nul & move @file 0x22%%e_%%d0x22 >nul"
)
)
popd

tiagocampos
Posts: 38
Joined: 21 Feb 2013 12:41

Re: Batch to archive files

#30 Post by tiagocampos » 05 Mar 2013 08:56

It's working well, except when I include *.js in exclusion files.
At 1st I tought it was not working at all but then I tested with a complete filename and worked.

Post Reply