Deleting files inside the folder from specific date using powershell
I would do this by first iterating the source folder path for directories with a name that can be converted to a datetime less than the datetime in variabe $specificDate
.
Then use Get-ChildItem
again inside these folders to find and remove files that do not have .tf.err
in their name:
$specificdate = [datetime]::ParseExact('2022-01-01','yyyy-MM-dd', $null)
$sourceFolder = 'Y:\Data\Retail\ABC\Development\ak\AK_Data\*'
Get-ChildItem -Path $sourceFolder -Directory |
Where-Object { [datetime]::ParseExact($_.Name,'yyyy-MM-dd', $null) -lt $specificdate } |
ForEach-Object {
Write-Host "Removing files from folder $($_.Name).."
Get-ChildItem -Path $_.FullName -File |
Where-Object { $_.Name -notlike '*.tf.err*' } |
Remove-Item -WhatIf
}
Again here, I added the -WhatIf
switch so you can first see what WOULD happen.
If you're OK with that, remove -WhatIf
and run the code again to actually delete the files
How to move or copy files listed by 'find' command in unix?
Adding to Eric Jablow's answer, here is a possible solution (it worked for me - linux mint 14 /nadia)
find /path/to/search/ -type f -name "glob-to-find-files" | xargs cp -t /target/path/
You can refer to "How can I use xargs to copy files that have spaces and quotes in their names?" as well.
VBScript to loop through all files in a folder
Maybe this will clear things up. (Or confuse you more, )
Const ForReading = 1
Const ForWriting = 2
sFolder = "H:\Letter Display\Letters\"
Set oFSO = CreateObject("Scripting.FileSystemObject")
For Each oFile In oFSO.GetFolder(sFolder).Files
If UCase(oFSO.GetExtensionName(oFile.Name)) = "LTR" Then
ProcessFiles oFSO, oFile
End if
Next
Set oFSO = Nothing
Sub ProcessFiles(FSO, File)
Set oFile2 = FSO.OpenTextFile(File.path, ForReading)
str1000 = "1000"
str1100 = "1100"
str1200 = "1200"
str9990 = "9990"
arrCommas1 = Array(14,31,41,59,70,81,101,111,124,138)
arrCommas2 = Array(14,31,41,55,79,144,209,274,409,563,589,608,623)
arrCommas3 = ArraY (14,32,41,73,83,97,106,156,167,184,188,195,207,260,273,332,368,431,461,472,593,617,666,772,810,834,848,894,898)
arrCommas4 = Array(14,31,41)
Do Until oFile2.AtEndOfStream
strLine = oFile2.ReadLine
If Left(strLine, 4) = str1000 then
intLength = Len(strLine)
For Each strComma in arrCommas1
strLine = Left(strLine, strComma - 1) + "," _
+ Mid(strLine, strComma, intLength)
Next
End If
If Left(strLine, 4) = str1100 then
intLength = Len(strLine)
For Each strComma in arrCommas2
strLine = Left(strLine, strComma - 1) + "," _
+ Mid(strLine, strComma, intLength)
Next
End If
If Left(strLine, 4) = str1200 then
intLength = Len(strLine)
For Each strComma in arrCommas3
strLine = Left(strLine, strComma - 1) + "," _
+ Mid(strLine, strComma, intLength)
Next
End If
If Left(strLine, 4) = str9990 then
intLength = Len(strLine)
For Each strComma in arrCommas4
strLine = Left(strLine, strComma - 1) + "," _
+ Mid(strLine, strComma, intLength)
Next
End If
strText = strText & strLine & vbCrLf
Loop
sFile = File.path
oFile2.close
set oFile2 = Nothing
Set File = FSO.OpenTextFile(sFile , ForWriting)
File.Write strText
File.Close
Set File = Nothing
end sub
find and delete file or folder older than x days
You can make use of this piece of code
find /tmp/* -mtime +7 -exec rm {} \;
Explanation
The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.
The second argument,
-mtime
, is used to specify the number of days old that the file is. If you enter+7
, it will find files older than 7 days.The third argument,
-exec
, allows you to pass in a command such as rm. The{} \;
at the end is required to end the command.
Source : http://www.howtogeek.com/howto/ubuntu/delete-files-older-than-x-days-on-linux/
For deleting folders, after emptying inside of them you can rmdir
instad of rm
in the piece of code, also if you only want to see directories you can add
-type d
to piece of code such as below:
find /tmp/*/* -mtime +7 -type d -exec rmdir {} \;
Batch files: List all files in a directory with relative paths
You could simply get the character length of the current directory, and remove them from your absolute list
setlocal EnableDelayedExpansion
for /L %%n in (1 1 500) do if "!__cd__:~%%n,1!" neq "" set /a "len=%%n+1"
setlocal DisableDelayedExpansion
for /r . %%g in (*.log) do (
set "absPath=%%g"
setlocal EnableDelayedExpansion
set "relPath=!absPath:~%len%!"
echo(!relPath!
endlocal
)
How do I delete a file or folder in Python?
os.remove()
removes a file.os.rmdir()
removes an empty directory.shutil.rmtree()
deletes a directory and all its contents.
Path
objects from the Python 3.4+ pathlib
module also expose these instance methods:
pathlib.Path.unlink()
removes a file or symbolic link.pathlib.Path.rmdir()
removes an empty directory.
find and copy file using Bash
I would recommend using find
's -exec
option:
find . -ctime 15 -exec cp {} ../otherfolder \;
As always, consult the manpage for best results.
Related Topics
How to Understand "Cmpl $0X0, -0X30(%Rbp)"/"Je ..."
Linux - Replacing Spaces in the File Names
Centos/Linux Setting Logrotate to Maximum File Size for All Logs
Linux: Set Permission Only to Directories
How to Calculate System Memory Usage from /Proc/Meminfo (Like Htop)
How to Search for Files and Zip Them in One Zip File
Tool to Visualize the Device Tree File (Dtb) Used by the Linux Kernel
Docker Ignores Limits.Conf (Trying to Solve "Too Many Open Files" Error)
Error: "Grep: Argument List Too Long"
Why Is Cpu-Cycles Much Less Than CPU Current Frequency
How to Clear All History in Linux/Ubuntu Terminal or Bash Permanently
How to Have Tcpdump Write to File and Standard Output the Appropriate Data
How the Util of iOStat Is Computed
Does Epoll(), Do Its Job in O(1)
Signing into Slack-Desktop Not Working on 4.23.0 64-Bit (Ubuntu)