Batch to archive files
Moderator: DosItHelp
-
- Posts: 38
- Joined: 21 Feb 2013 12:41
Re: Batch to archive files
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!
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!
Re: Batch to archive files
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?
And the folders that are created - do you want to delete them after the ZIP files are created? Is that what you mean?
-
- Posts: 38
- Joined: 21 Feb 2013 12:41
Re: Batch to archive files
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.
Re: Batch to archive files
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.
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)
-
- Posts: 38
- Joined: 21 Feb 2013 12:41
Re: Batch to archive files
It's working very smoothly
But I have one concern regarding the archiving process, I'll try to explain in my bad English
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?
But I have one concern regarding the archiving process, I'll try to explain in my bad English
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?
Re: Batch to archive files
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
Note that the _YYYY_MM will be different from YYYYMMDD
-
- Posts: 38
- Joined: 21 Feb 2013 12:41
Re: Batch to archive files
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.
Re: Batch to archive files
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.
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)
-
- Posts: 38
- Joined: 21 Feb 2013 12:41
Re: Batch to archive files
Running at full steam
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.
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.
Re: Batch to archive files
tiagocampos wrote:Running at full steam
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.
-
- Posts: 38
- Joined: 21 Feb 2013 12:41
Re: Batch to archive files
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.
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.
Re: Batch to archive files
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?
-
- Posts: 38
- Joined: 21 Feb 2013 12:41
Re: Batch to archive files
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).
Re: Batch to archive files
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
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
-
- Posts: 38
- Joined: 21 Feb 2013 12:41
Re: Batch to archive files
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.
At 1st I tought it was not working at all but then I tested with a complete filename and worked.