Batch to archive files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
ashleyco
Posts: 3
Joined: 03 Sep 2013 23:46
Contact:

Re: Batch to archive files

#46 Post by ashleyco » 05 Dec 2013 03:25

It processes a folder full of directories.

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

Re: Batch to archive files

#47 Post by foxidrive » 05 Dec 2013 04:57

Yes, that is so.

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

Re: Batch to archive files

#48 Post by tiagocampos » 09 Dec 2013 05:03

Just had the chance to test it now.

The result was this http://i.imgur.com/cF6WA0v.png
He did everthing OK but didn't zip the folders at the end.

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

Re: Batch to archive files

#49 Post by foxidrive » 09 Dec 2013 05:16

What change did you make?

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

Re: Batch to archive files

#50 Post by tiagocampos » 09 Dec 2013 09:08

This is what I current have.
I changed what you said.

Code: Select all

@echo off
call :getdate today -30
set "folder=%cd%"

:: 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:\MTSOMS\Scripts\FileArchiver\3_folders_WebBox.txt
:: To specify the location of the file from where we filters the files we don't want to archive
set exclusion=C:\MTSOMS\Scripts\FileArchiver\3_filters_WebBox.txt
:: To specify the location of the program that will handle the compression
set programzip=C:\Program Files\7-Zip\7z.exe

::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 ^|findstr /v /g:%exclusion%') 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 & >>0x22%folder%\%dt%.log0x22 echo %dt%;@file;moving @file to 0x22%dt%-%%e_%%d0x22.zip"
  )
 )
popd
)
SET LOG_FOLDER=C:\
SET datetime="%date:~6,4%%date:~3,2%%date:~0,2%_%time:~0,2%%time:~3,2%%time:~6,2%"
SET LOG_FILE=%LOG_FOLDER%\LOG_%datetime%.CSV

::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') if /i not "%%b"=="Archive" do (
   pushd "%%b"
   echo %%a;%%b>>%LOG_FILE%
   echo Making ZIP file from "%%b" folder
   "%programzip%" a -r -tzip "%%b_%dt%.zip" *
   move "%%b_%dt%.zip" .. >nul
   popd
   rd /s /q "%%b"
   move /y "%%b_%dt%.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)

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

Re: Batch to archive files

#51 Post by foxidrive » 09 Dec 2013 17:15

That change is only going to stop a folder called 'archive' from being processed.

Something else is going on here I think.

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

Re: Batch to archive files

#52 Post by tiagocampos » 10 Dec 2013 05:18

Well I just replaced again and the zip worked fine :cry:

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

Re: Batch to archive files

#53 Post by tiagocampos » 26 Dec 2013 06:22

I've tested several times now and what's causing the script to break is this instruction:

for /f "delims=" %%b in ('dir "????_??" /ad /b') if /i not "%%b"=="Archive" do (

If I replaced with the previous one it is ok:

for /f "delims=" %%b in ('dir "????_??" /ad /b') do (

Do you have any idea what's wrong?

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

Re: Batch to archive files

#54 Post by foxidrive » 26 Dec 2013 06:26

tiagocampos wrote:I've tested several times now and what's causing the script to break is this instruction:

for /f "delims=" %%b in ('dir "????_??" /ad /b') if /i not "%%b"=="Archive" do (

If I replaced with the previous one it is ok:

for /f "delims=" %%b in ('dir "????_??" /ad /b') do (

Do you have any idea what's wrong?


The do was in the wrong spot. You must have been getting error messages on the console...
Try this:

Code: Select all

for /f "delims=" %%b in ('dir "????_??" /ad /b') do if /i not "%%b"=="Archive"  (

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

Re: Batch to archive files

#55 Post by tiagocampos » 26 Dec 2013 06:37

Indeed that fixed it.

I wasn't getting error messages, at least I couldn't see them cause the window shuts down too fast.
I even tried doing some echos/pauses before and after that line to try and figure it out but no luck.

Thanks for your help, now I'll test and see if the problem with the previous Archives being deleted is fixed, although it worked now I must test with a full archive to ensure it's solved.

Post Reply