0

Can I have your help & expertise in creating a batch file for the below please

I have below files in a folder \\abcdef123\test\root\child\source

AC_FDM_TREE_EXTRACT_20161216203034.touch
AC_FDM_TREE_EXTRACT_20170118204420.touch
AC_FDM_TREE_EXTRACT_20170214204730.touch
PS_Z_VT_SUPER_TREE_20161216203034.txt
PS_Z_VT_SUPER_TREE_20170118204420.txt
PS_Z_VT_SUPER_TREE_20170214204730.txt

I would like to dynamically delete all files except for the current month. ie., If YYYYMM is the current month, delete all files(do not include sub folders) not equal to YYYYMM.*

It is unlikely that files will be generated with future dates. But, if it is a quick win, may be say delete all files before current month is even better.

3

1 Answer 1

0

I would like to dynamically delete all files except for the current month. ie., If YYYYMM is the current month, delete all files(do not include sub folders) not equal to YYYYMM.*

Here is a PowerShell script that does it:

Push-Location "\\abcdef123\test\root\child\source"

$USculture=New-Object -TypeName CultureInfo -ArgumentList "en-US"
$targetDateString=(Get-Date).ToString("yyyyMM", $USculture)

Remove-Item * -Exclude ("*"+$targetDateString+"*")
Pop-Location

The first line changes the folder to "\abcdef123\test\root\child\source"

The second and third line (second paragraph) gets the current year and month of the Gregorian calendar in the "yyyyMM" format, where "yyyy" is a four-digits year and "MM" is a two-digits month.

The fourth line (last paragraph) deletes all files in the current folder except those that match the "yyyyMM" pattern.

The last line returns you to the folder where you started.

2
  • Thanks Fleet Command. Can you suggest a beginnners link to PowerShell?
    – Learner16
    Feb 28, 2017 at 10:19
  • @Learner16, I'll go over my bookmarks to see which one is suitable for what you want. In the mean time, if you find the answer what you wanted, please don't forget to click on the large checkmark near the answer.
    – user477799
    Feb 28, 2017 at 11:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .