Batch job to delete folder older than 7 days on windows 7 - windows

Hi I need to delete folders on my Windows 7 computer which are older than 7 days and have data in it.
I have used the following commands which haven't worked out well for me
FORFILES /S /D -10 /C "cmd /c IF #isdir == TRUE rd /S /Q #path"
forfiles -p "C:\what\ever" -s -m *.* -d <number of days> -c "cmd /c del #path"
The second command which I referred from Batch file to delete files older than N days
also didn't work for me.
Can someone please suggest what to use in the second command on C:\what\ever or suggest an alternative command to delete my folders older than 7 days.

So your task is to delete older backup directories no longer needed.
The tricky solution would be working with "delete older than X days".
The simple solution is following:
#echo off
set "BackupDirectory=C:\Backup"
for /F "skip=7 delims=" %%D in ('dir "%BackupDirectory%" /AD /B /O-D 2^>nul') do (
rd /Q /S "%BackupDirectory%\%%D"
)
The command DIR returns a list which contains
because of /AD (attribute directory) just the subdirectories and
because of /B (bare format) just directory names and
because of /O-D ordered by date with newest at top and oldest last.
The command FOR skips the first 7 lines from list, i.e. the 7 newest subdirectories, and executes on the other (older) directories the command to remove the subdirectory.
In your case with directory names starting with yyyy-mm-dd it would be also possible to use /O-N (ordered reverse by name) instead of /O-D to keep skip=x newest subdirectories and delete all others.
Note: On NTFS partitions the last modification date of a folder changes if any file/folder in this folder is added/modified/deleted, but not on FAT16, FAT32 or exFAT partitions.
On DIR command line after /O-D the option /TC can be added get output the directory list sorted reverse by creation date of the folders. But backup folders are usually not modified later after backup was created. It is a matter of opinion to keep the newest folders taking modifications in the folders into account or rate only folder creation time.
In my experience on deleting backups the date is not so important. Important is only limiting the number of backups to avoid filling a storage media. For example if a backup usually needs 5 GiB, the number of backups to keep might be 10 or 20, but if a backup usually needs only 500 KiB, the number could be increased to 100. The date does not matter, just the total amount of bytes required for the backups, as the limit is the storage media size and not the time span.
And for a log file with lines always appended on each execution of a backup operation, the size of the log file must be usually observed and not the time span on which lines are appended to same log file to avoid that the log file becomes larger and larger. Moving a log file with size > x KiB or MiB to *_old.log with /Y to overwrite an already existing *_old.log and then redirect the new lines into a new log file is quite often the right strategy to have just 2 logs files (*.log and *_old.log) with a defined maximum file size containing log lines of the last x backup operations.

Remove old backup folders older than /d days. So use a minus value.
Oneliner...
forfiles /p y:\foldername /d -50 /c "cmd /c if #isdir==TRUE rmdir #file /s /q"
You want to leave off the forfiles "/s" if you only want to scan the top level folders and once you have that folder, you rmdir the entire folder.
The reason for using rmdir is because if you use del or erase, once it deletes a file, it can change the date on the holding folder. Then the command leaves folders behind.
/p - for path
/d - minus for 50 days old
/c - run a command
#isdir - if folder
Rmdir - better than del or erase
#file - what it was looking for
/s - subfolders
/q - quiet

Related

Use echo to empty file content in Windows Command Prompt

I have a folder on a windows laptop running a program for measuring concrete vibrations that runs 24/7.
The software creates 2 files every 10 minutes *.asx and *.pbx
The *.pbx files becomes 28 MB each.
I run backup from the laptop to an ftp server every night.
I would need to empty the files of its data, tried with echo and that works.
but its thousands of files and i cannot figure out how to do it on all *.pbx extensions.
Cannot remove the files, because then program will then restart the number sequence after 10 min.
I need to keep all files in the directory just need to shrink them.
The data in the files are not important after backup is finished.
I have tried
echo delete >*.pbx and most of the arguments.
Please help!
Use FOR /F - loop command: against the results of another command.
From command prompt:
for /f "delims=" %G in ('dir /B *.pbx') do #copy /Y NUL "%~G">NUL
From a Windows batch script (.bat or .cmd)
#for /f "delims=" %%G in ('dir /B *.pbx') do #copy /Y NUL "%%~G">NUL
To empty a file, you can use copy:
copy /Y nul: myfile.myext

LIST (not delete!) directories older than X using command line

I need to get a list of directories older than 90 days from 'today's' date using windows command line so I can schedule a batch file to run these folders through a separate process.
I need to list folders and subfolders only, no files.
I have tried using 'dir' which produces the list I need:
dir /b /s /a:d /o:gen >C:\file_list.txt
But, I can't check for dates greater than X using 'dir'.
I've looked at using 'forfiles' but this won't produce the list as I need it which my dir attempt produces.
To add some more complexity, ideally I need to check the date on the files within a folder rather than the folders date properties.
Any help is greatly appreciated, thanks.
And if you try with Forfiles like that :
#echo off
set OLD=90
FORFILES /S /D -%OLD% /C "cmd /c IF #isdir == TRUE echo #path"
pause
For more information about Forfiles command

Batch script to move file from scource to destination folder for files older than 30 day

i want to create the batch script which could copy the 30 days older files from one folder to another. And also deletes the 30 days old files from the source folder after copying to the destination folder
forfiles /p "c:\sourceFolder" /S /D -30 /C "cmd /c robocopy "C:\DestinationFolder" "C:\sourceFolder" /mir #file : date>=30 days=nul"
Nothing has happened, not even error message is displayed
robocopy sourceFolder targetFolder * /s /mov /minage:30 /l
If the list is correct, remove the /l (only list) from the command to do the move
This robocopy only moves a few files and deleted my rar packages? not working at all as I would like to. I just want it to work as user3302083 wants. Just move folder that is 2 days old no other changes. Now I tried and it moved just a few files and deleted.

Batch script to move compress and delete files

I have a batch script that does the following
#ECHO OFF
REM move files older than 2 days from the incoming directory to the incoming archive directory
robocopy D:\Agentrics\integration\download D:\Agentrics\integration\download\archive /MOV /MINAGE:2
REM Zip files in the Archieve directory that are older than one week
FOR %%A IN (D:\Agentrics\integration\download\archive\*.txt*, D:\Agentrics\integration\download\archive\*.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r -to7d D:\Agentrics\integration\download\archive\"%%~nA.zip" "%%A"
REM Delete Original files after they are zipped
forfiles /p D:\Agentrics\integration\download\archive /s /m *.txt* /d -7 /c "cmd /c del /q #path"
forfiles /p D:\Agentrics\integration\download\archive /s /m *.cpi* /d -7 /c "cmd /c del /q #path"
REM Delete files that are older than 6 months from the archive directory
forfiles /p D:\Agentrics\integration\download\archive /s /m *.zip* /d -180 /c "cmd /c del /q #path"
pause
Question 1:
When i run the script i get WinRAR diagnostic messages for some files. For example if there are files in the incoming directory that are not older than two days i get this message."WinRAR Diagnostic messages: No File To Add". Because of this message the scripts stops until i click on the close button of the dialogue box. I am using the free version of WinRAR and its not expired
Question 2: I have two seprate command in the script above. One is for zipping the files older than a week and the other one is deleting the original files after they are zipped. How can i link those two commands so that if some reason the files did not get zipped they should also not get deleted. Or is is there a command to break the script if the files did not get zipped? I just want to zipp the files first and then delete the original ones
I sugest to use
#ECHO OFF
REM Move files older than 2 days from the incoming directory to the incoming archive directory.
robocopy D:\Agentrics\integration\download D:\Agentrics\integration\download\archive /MOV /MINAGE:2
REM Move each file in the archive directory that is older than one week into a ZIP archive.
FOR %%A IN (D:\Agentrics\integration\download\archive\*.txt*, D:\Agentrics\integration\download\archive\*.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" m -afzip -ep -inul -to7d -y "D:\Agentrics\integration\download\archive\%%~nA.zip" "%%A"
REM Delete files that are older than 6 months from the archive directory.
forfiles /p D:\Agentrics\integration\download\archive /s /m *.zip* /d -180 /c "cmd /c del /q #path"
The entire process can be simplified by using command m which means move to archive instead of command a which means add to archive. WinRAR removes the file only after successful compression.
Using switch -afzip informs WinRAR explicitly to use ZIP instead of RAR compression.
The switch -ep results in removing path from the file names inside the archive.
The output of any error or warning message can be suppressed with switch -inul. This switch is mainly for the console version Rar.exe (not supporting ZIP copmression) for output to stdout and stderr, but may work also for WinRAR. I have never seen a diagnostic message to confirm on my tests using WinRAR.exe version 4.20 when the ZIP file was not created because the file was not older than 7 days. I have seen the warning on using Rar.exe creating a RAR archive and not using -inul, but also with no need for a key hit even without using switch -y.
I removed switch -r for recursive archiving as not needed here with always moving only 1 file to a ZIP archive.
The unmodified switch -to7d results in archiving only files older than 7 days.
Last the switch -y is added to assume Yes on all queries although I have never seen one on my tests.
One more hint:
On NTFS partitions the attribute compressed can be set on a folder resulting in an automatic ZIP compression of all files copied or created in this folder to save disk storage.

batch/bat to copy folder and content at once

I'm writing a batch script that does a copy. I want to script it to copy an entire folder. When I want to copy a single file, I do this
copy %~dp0file.txt file.txt
If I have a folder with this structure, is there a command to copy this entire folder with its contents all at once while preserving the exact structure.
mainfolder/
file1.txt
file2.txt
insidefolder/
file3.txt
file4.txt
file5.txt
if you have xcopy, you can use the /E param, which will copy directories and subdirectories and the files within them, including maintaining the directory structure for empty directories
xcopy [source] [destination] /E
The old way:
xcopy [source] [destination] /E
xcopy is deprecated. Robocopy replaces Xcopy. It comes with Windows 8, 8.1 and 10.
robocopy [source] [destination] /E
robocopy has several advantages:
copy paths exceeding 259 characters
multithreaded copying
More details here.
I suspect that the xcopy command is the magic bullet you're looking for.
It can copy files, directories, and even entire drives while preserving the original directory hierarchy. There are also a handful of additional options available, compared to the basic copy command.
Check out the documentation here.
If your batch file only needs to run on Windows Vista or later, you can use robocopy instead, which is an even more powerful tool than xcopy, and is now built into the operating system. It's documentation is available here.
For Folder Copy You can Use
robocopy C:\Source D:\Destination /E
For File Copy
copy D:\Sourcefile.txt D:\backup\Destinationfile.txt /Y
Delete file in some folder last modify date more than some day
forfiles -p "D:\FolderPath" -s -m *.[Filetype eg-->.txt] -d -[Numberof dates] -c "cmd /c del #PATH"
And you can Shedule task in windows perform this task automatically in specific time.
I've been interested in the original
question here and related ones.
For an answer, this week I did some
experiments with XCOPY.
To help answer the original question, here
I post the results of my experiments.
I did the experiments on Windows 7 64 bit
Professional SP1 with the copy of XCOPY
that came with the operating system.
For the experiments, I wrote some code in
the scripting language Open Object Rexx
and the editor macro language Kexx with
the text editor KEdit.
XCOPY was called from the Rexx code. The
Kexx code edited the screen output of
XCOPY to focus on the crucial results.
The experiments all had to do with using
XCOPY to copy one directory with several
files and subdirectories.
The experiments consisted of 10 cases.
Each case adjusted the arguments to XCOPY
and called XCOPY once. All 10 cases were
attempting to do the same copying
operation.
Here are the main results:
(1) Of the 10 cases, only three did
copying. The other 7 cases right away,
just from processing the arguments to
XCOPY, gave error messages, e.g.,
Invalid path
Access denied
with no files copied.
Of the three cases that did copying, they
all did the same copying, that is, gave
the same results.
(2) If want to copy a directory X and all
the files and directories in directory X,
in the hierarchical file system tree
rooted at directory X, then apparently
XCOPY -- and this appears to be much of
the original question -- just will NOT do
that.
One consequence is that if using XCOPY to
copy directory X and its contents, then
CAN copy the contents but CANNOT copy the
directory X itself; thus, lose the
time-date stamp on directory X, its
archive bit, data on ownership,
attributes, etc.
Of course if directory X is a subdirectory
of directory Y, an XCOPY of Y will copy
all of the contents of directory Y WITH
directory X. So in this way can get a copy
of directory X. However, the copy of
directory X will have its time-date stamp
of the time of the run of XCOPY and NOT
the time-date stamp of the original
directory X.
This change in time-date stamps can be
awkward for a copy of a directory with a
lot of downloaded Web pages: The HTML
file of the Web page will have its
original time-date stamp, but the
corresponding subdirectory for files used
by the HTML file will have the time-date
stamp of the run of XCOPY. So, when
sorting the copy on time date stamps, all
the subdirectories, the HTML files and the
corresponding subdirectories, e.g.,
x.htm
x_files
can appear far apart in the sort on
time-date.
Hierarchical file systems go way back,
IIRC to Multics at MIT in 1969, and since
then lots of people have recognized the
two cases, given a directory X, (i) copy
directory X and all its contents and (ii)
copy all the contents of X but not
directory X itself. Well, if only from
the experiments, XCOPY does only (ii).
So, the results of the 10 cases are below.
For each case, in the results the first
three lines have the first three arguments
to XCOPY. So, the first line has the tree
name of the directory to be copied, the
'source'; the second line has the tree
name of the directory to get the copies,
the 'destination', and the third line has
the options for XCOPY. The remaining 1-2
lines have the results of the run of
XCOPY.
One big point about the options is that
options /X and /O result in result
Access denied
To see this, compare case 8 with the other
cases that were the same, did not have /X
and /O, but did copy.
These experiments have me better
understand XCOPY and contribute an answer
to the original question.
======= case 1 ==================
"k:\software\dir_time-date\"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_1\"
options = /E /F /G /H /K /O /R /V /X /Y
Result: Invalid path
Result: 0 File(s) copied
======= case 2 ==================
"k:\software\dir_time-date\*"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_2\"
options = /E /F /G /H /K /O /R /V /X /Y
Result: Access denied
Result: 0 File(s) copied
======= case 3 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_3\"
options = /E /F /G /H /K /O /R /V /X /Y
Result: Access denied
Result: 0 File(s) copied
======= case 4 ==================
"k:\software\dir_time-date\"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_4\"
options = /E /F /G /H /K /R /V /Y
Result: Invalid path
Result: 0 File(s) copied
======= case 5 ==================
"k:\software\dir_time-date\"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_5\"
options = /E /F /G /H /K /O /R /S /X /Y
Result: Invalid path
Result: 0 File(s) copied
======= case 6 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_6\"
options = /E /F /G /H /I /K /O /R /S /X /Y
Result: Access denied
Result: 0 File(s) copied
======= case 7 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_7"
options = /E /F /G /H /I /K /R /S /Y
Result: 20 File(s) copied
======= case 8 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_8"
options = /E /F /G /H /I /K /O /R /S /X /Y
Result: Access denied
Result: 0 File(s) copied
======= case 9 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_9"
options = /I /S
Result: 20 File(s) copied
======= case 10 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_10"
options = /E /I /S
Result: 20 File(s) copied

Resources