Batch file to move/compress/delete files
I am trying to write a batch script that will run automatically daily to do the following:
- Move files older than 2 days to from the main directory (Jason) to an archive directory.
- Zip files in the archive directory that are older than 1 week and delete files from this directory that are older than 6 months.
- I want to run this script from a different directory (not the directory that has the files).
I wrote the following script but it’s not working correctly.
REM move files older than 2 days to an archive directory
robocopy D:AgentricsintegrationincomingJason D:AgentricsintegrationincomingJasonarchive /MOV /MINAGE:2
Questions:
- How can I change the command below to zip files older than 1 week?
- Is it possible that the zipped files can have the same creation date and time as the original files?
REM zip all files in the backup directory
FOR %%A IN (*.TXT*, *.cpi*) DO "C:Program FilesWinRARWinRAR.exe" a -r "%%~nA.zip" "%%A"
FOR %%A IN (*.TXT, *.cpi) DO DEL "D:AgentricsintegrationincomingJasonarchive.cpi*" "%%A"
REM Delete all files in the backup directory that are older than 6 months
forfiles /p D:AgentricsintegrationincomingJasonarchive /s /m *.* /d -500 /c "cmd /c del /q @path"
batch-file robocopy winrar
add a comment |
I am trying to write a batch script that will run automatically daily to do the following:
- Move files older than 2 days to from the main directory (Jason) to an archive directory.
- Zip files in the archive directory that are older than 1 week and delete files from this directory that are older than 6 months.
- I want to run this script from a different directory (not the directory that has the files).
I wrote the following script but it’s not working correctly.
REM move files older than 2 days to an archive directory
robocopy D:AgentricsintegrationincomingJason D:AgentricsintegrationincomingJasonarchive /MOV /MINAGE:2
Questions:
- How can I change the command below to zip files older than 1 week?
- Is it possible that the zipped files can have the same creation date and time as the original files?
REM zip all files in the backup directory
FOR %%A IN (*.TXT*, *.cpi*) DO "C:Program FilesWinRARWinRAR.exe" a -r "%%~nA.zip" "%%A"
FOR %%A IN (*.TXT, *.cpi) DO DEL "D:AgentricsintegrationincomingJasonarchive.cpi*" "%%A"
REM Delete all files in the backup directory that are older than 6 months
forfiles /p D:AgentricsintegrationincomingJasonarchive /s /m *.* /d -500 /c "cmd /c del /q @path"
batch-file robocopy winrar
1
doesn't this work: forefiles /M *.txt /D -7 /C WinRAR a -r aa.zip @file ?
– harper
Feb 27 '13 at 19:27
Yes that works and thank you. Can you tell me how can i change the following script so that it only compresses files that are older than 7 days. FOR %%A IN (D:AgentricsintegrationincomingMansoorbackup*.txt*, D:AgentricsintegrationincomingMansoorbackup*.cpi*) DO "C:Program FilesWinRARWinRAR.exe" a -r D:AgentricsintegrationincomingMansoorbackup"%%~nA.zip" "%%A" Regards, Mansoor
– Mansoor Ahmad
Feb 28 '13 at 16:23
add a comment |
I am trying to write a batch script that will run automatically daily to do the following:
- Move files older than 2 days to from the main directory (Jason) to an archive directory.
- Zip files in the archive directory that are older than 1 week and delete files from this directory that are older than 6 months.
- I want to run this script from a different directory (not the directory that has the files).
I wrote the following script but it’s not working correctly.
REM move files older than 2 days to an archive directory
robocopy D:AgentricsintegrationincomingJason D:AgentricsintegrationincomingJasonarchive /MOV /MINAGE:2
Questions:
- How can I change the command below to zip files older than 1 week?
- Is it possible that the zipped files can have the same creation date and time as the original files?
REM zip all files in the backup directory
FOR %%A IN (*.TXT*, *.cpi*) DO "C:Program FilesWinRARWinRAR.exe" a -r "%%~nA.zip" "%%A"
FOR %%A IN (*.TXT, *.cpi) DO DEL "D:AgentricsintegrationincomingJasonarchive.cpi*" "%%A"
REM Delete all files in the backup directory that are older than 6 months
forfiles /p D:AgentricsintegrationincomingJasonarchive /s /m *.* /d -500 /c "cmd /c del /q @path"
batch-file robocopy winrar
I am trying to write a batch script that will run automatically daily to do the following:
- Move files older than 2 days to from the main directory (Jason) to an archive directory.
- Zip files in the archive directory that are older than 1 week and delete files from this directory that are older than 6 months.
- I want to run this script from a different directory (not the directory that has the files).
I wrote the following script but it’s not working correctly.
REM move files older than 2 days to an archive directory
robocopy D:AgentricsintegrationincomingJason D:AgentricsintegrationincomingJasonarchive /MOV /MINAGE:2
Questions:
- How can I change the command below to zip files older than 1 week?
- Is it possible that the zipped files can have the same creation date and time as the original files?
REM zip all files in the backup directory
FOR %%A IN (*.TXT*, *.cpi*) DO "C:Program FilesWinRARWinRAR.exe" a -r "%%~nA.zip" "%%A"
FOR %%A IN (*.TXT, *.cpi) DO DEL "D:AgentricsintegrationincomingJasonarchive.cpi*" "%%A"
REM Delete all files in the backup directory that are older than 6 months
forfiles /p D:AgentricsintegrationincomingJasonarchive /s /m *.* /d -500 /c "cmd /c del /q @path"
batch-file robocopy winrar
batch-file robocopy winrar
edited Apr 24 '17 at 17:59
djsmiley2k
5,12612336
5,12612336
asked Feb 27 '13 at 18:49
Mansoor AhmadMansoor Ahmad
1112
1112
1
doesn't this work: forefiles /M *.txt /D -7 /C WinRAR a -r aa.zip @file ?
– harper
Feb 27 '13 at 19:27
Yes that works and thank you. Can you tell me how can i change the following script so that it only compresses files that are older than 7 days. FOR %%A IN (D:AgentricsintegrationincomingMansoorbackup*.txt*, D:AgentricsintegrationincomingMansoorbackup*.cpi*) DO "C:Program FilesWinRARWinRAR.exe" a -r D:AgentricsintegrationincomingMansoorbackup"%%~nA.zip" "%%A" Regards, Mansoor
– Mansoor Ahmad
Feb 28 '13 at 16:23
add a comment |
1
doesn't this work: forefiles /M *.txt /D -7 /C WinRAR a -r aa.zip @file ?
– harper
Feb 27 '13 at 19:27
Yes that works and thank you. Can you tell me how can i change the following script so that it only compresses files that are older than 7 days. FOR %%A IN (D:AgentricsintegrationincomingMansoorbackup*.txt*, D:AgentricsintegrationincomingMansoorbackup*.cpi*) DO "C:Program FilesWinRARWinRAR.exe" a -r D:AgentricsintegrationincomingMansoorbackup"%%~nA.zip" "%%A" Regards, Mansoor
– Mansoor Ahmad
Feb 28 '13 at 16:23
1
1
doesn't this work: forefiles /M *.txt /D -7 /C WinRAR a -r aa.zip @file ?
– harper
Feb 27 '13 at 19:27
doesn't this work: forefiles /M *.txt /D -7 /C WinRAR a -r aa.zip @file ?
– harper
Feb 27 '13 at 19:27
Yes that works and thank you. Can you tell me how can i change the following script so that it only compresses files that are older than 7 days. FOR %%A IN (D:AgentricsintegrationincomingMansoorbackup*.txt*, D:AgentricsintegrationincomingMansoorbackup*.cpi*) DO "C:Program FilesWinRARWinRAR.exe" a -r D:AgentricsintegrationincomingMansoorbackup"%%~nA.zip" "%%A" Regards, Mansoor
– Mansoor Ahmad
Feb 28 '13 at 16:23
Yes that works and thank you. Can you tell me how can i change the following script so that it only compresses files that are older than 7 days. FOR %%A IN (D:AgentricsintegrationincomingMansoorbackup*.txt*, D:AgentricsintegrationincomingMansoorbackup*.cpi*) DO "C:Program FilesWinRARWinRAR.exe" a -r D:AgentricsintegrationincomingMansoorbackup"%%~nA.zip" "%%A" Regards, Mansoor
– Mansoor Ahmad
Feb 28 '13 at 16:23
add a comment |
1 Answer
1
active
oldest
votes
If I were you, I would not use for cycle for this. I would prefer the way proposed by @harper
But still, here is the way you wanted it:
REM move files older than 2 days to an archive directory
forfiles /P D:AgentricsintegrationincomingJason /M *.txt /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
forfiles /P D:AgentricsintegrationincomingJason /M *.cpi /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
REM zip all files in the backup directory
for /f "tokens=*" %%a in ('forfiles /p D:AgentricsintegrationincomingJasonarchive /s /d -7') do "C:Program FilesWinRARWinRAR.exe" a -r "%%~nA.zip" "%%a"
REM Delete all files in the backup directory that are older than 6 months
for /f "tokens=*" %%a in ('forfiles /p D:AgentricsintegrationincomingJasonarchive /s /d -183') do del D:AgentricsintegrationincomingJasonarchive%%a /y
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f558352%2fbatch-file-to-move-compress-delete-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I were you, I would not use for cycle for this. I would prefer the way proposed by @harper
But still, here is the way you wanted it:
REM move files older than 2 days to an archive directory
forfiles /P D:AgentricsintegrationincomingJason /M *.txt /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
forfiles /P D:AgentricsintegrationincomingJason /M *.cpi /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
REM zip all files in the backup directory
for /f "tokens=*" %%a in ('forfiles /p D:AgentricsintegrationincomingJasonarchive /s /d -7') do "C:Program FilesWinRARWinRAR.exe" a -r "%%~nA.zip" "%%a"
REM Delete all files in the backup directory that are older than 6 months
for /f "tokens=*" %%a in ('forfiles /p D:AgentricsintegrationincomingJasonarchive /s /d -183') do del D:AgentricsintegrationincomingJasonarchive%%a /y
add a comment |
If I were you, I would not use for cycle for this. I would prefer the way proposed by @harper
But still, here is the way you wanted it:
REM move files older than 2 days to an archive directory
forfiles /P D:AgentricsintegrationincomingJason /M *.txt /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
forfiles /P D:AgentricsintegrationincomingJason /M *.cpi /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
REM zip all files in the backup directory
for /f "tokens=*" %%a in ('forfiles /p D:AgentricsintegrationincomingJasonarchive /s /d -7') do "C:Program FilesWinRARWinRAR.exe" a -r "%%~nA.zip" "%%a"
REM Delete all files in the backup directory that are older than 6 months
for /f "tokens=*" %%a in ('forfiles /p D:AgentricsintegrationincomingJasonarchive /s /d -183') do del D:AgentricsintegrationincomingJasonarchive%%a /y
add a comment |
If I were you, I would not use for cycle for this. I would prefer the way proposed by @harper
But still, here is the way you wanted it:
REM move files older than 2 days to an archive directory
forfiles /P D:AgentricsintegrationincomingJason /M *.txt /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
forfiles /P D:AgentricsintegrationincomingJason /M *.cpi /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
REM zip all files in the backup directory
for /f "tokens=*" %%a in ('forfiles /p D:AgentricsintegrationincomingJasonarchive /s /d -7') do "C:Program FilesWinRARWinRAR.exe" a -r "%%~nA.zip" "%%a"
REM Delete all files in the backup directory that are older than 6 months
for /f "tokens=*" %%a in ('forfiles /p D:AgentricsintegrationincomingJasonarchive /s /d -183') do del D:AgentricsintegrationincomingJasonarchive%%a /y
If I were you, I would not use for cycle for this. I would prefer the way proposed by @harper
But still, here is the way you wanted it:
REM move files older than 2 days to an archive directory
forfiles /P D:AgentricsintegrationincomingJason /M *.txt /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
forfiles /P D:AgentricsintegrationincomingJason /M *.cpi /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
REM zip all files in the backup directory
for /f "tokens=*" %%a in ('forfiles /p D:AgentricsintegrationincomingJasonarchive /s /d -7') do "C:Program FilesWinRARWinRAR.exe" a -r "%%~nA.zip" "%%a"
REM Delete all files in the backup directory that are older than 6 months
for /f "tokens=*" %%a in ('forfiles /p D:AgentricsintegrationincomingJasonarchive /s /d -183') do del D:AgentricsintegrationincomingJasonarchive%%a /y
answered Dec 10 '15 at 12:11
HardomanHardoman
76339
76339
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f558352%2fbatch-file-to-move-compress-delete-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
doesn't this work: forefiles /M *.txt /D -7 /C WinRAR a -r aa.zip @file ?
– harper
Feb 27 '13 at 19:27
Yes that works and thank you. Can you tell me how can i change the following script so that it only compresses files that are older than 7 days. FOR %%A IN (D:AgentricsintegrationincomingMansoorbackup*.txt*, D:AgentricsintegrationincomingMansoorbackup*.cpi*) DO "C:Program FilesWinRARWinRAR.exe" a -r D:AgentricsintegrationincomingMansoorbackup"%%~nA.zip" "%%A" Regards, Mansoor
– Mansoor Ahmad
Feb 28 '13 at 16:23