PowerShell: Delete files after a number of days

I used to do a lot of “batch commands” back in the good old MS-DOS days. As with everything, technology advances and PowerShell is nothing short of amazing. Support for PowerShell  commandlets seems to pop up all over the place and I use it more and more to complete tasks.

Let’s say that you want to delete files from a folder, recursively, that have a creation date before a specific number of days.  How would you do it?  That’s right – PowerShell.

$path = 'F:\Program Files\Microsoft SQL Server\MSSQL12.SQLSERVER\MSSQL\Backup\Log'
$cutoffdays = (Get-Date).AddDays(-2)

Get-ChildItem $path -Recurse | ? {
-not $_.PSIsContainer -and $_.CreationTime -lt $cutoffdays
} | Remove-Item

You can even look for files that have a specific extension:

$path = 'F:\Program Files\Microsoft SQL Server\MSSQL12.SQLSERVER\MSSQL\Backup\Log'
$cutoffdays = (Get-Date).AddDays(-2)
$extension = '.trn'

Get-ChildItem $path -Recurse | ? {
-not $_.PSIsContainer -and $_.CreationTime -lt $cutoffdays -and $_.Extension -ieq $extension
} | Remove-Item

There are a number of other parameters and options. Give it a shot.

Microsoft Dynamics NAV: Create and Delete Directory dotNET Interop

The ability to extend the development library through dotNET Interoperability within the development environment of Microsoft Dynamics NAV really does open up things up. I have been asked on several occasions about creating directories  via Dynamics NAV code. Directories can easily be created and deleted through the use of the System.IO.Directory class.  The CreateDirectory method allows you to create all directories and subdirectories in the specified path.  This makes creating nested directories rather easy. The DeleteDirectory method also allows for the recursive removal of directories.

Text Constants:

Name ConstValue
DirectoryExistsErr Directory already exists.
DirectoryDoesNotExistErr Directory does not exist.
DirectoryCreatedMsg Directory %1 created.
DirectoryDeletededMsg Directory deleted.



Name DataType Subtype
SystemIODirectory DotNet System.IO.Directory.'mscorlib'
SystemIODirectoryInfo DotNet System.IO.DirectoryInfo.'mscorlib'


LOCAL CreateDirectory(path : Text;showconfirmation : Boolean) : Boolean
	IF SystemIODirectory.Exists(path) THEN
	SystemIODirectoryInfo := SystemIODirectory.CreateDirectory(path);
	IF showconfirmation THEN BEGIN


LOCAL DeleteDirectory(path : Text;recursive : Boolean;showconfirmation : Boolean)
	IF NOT SystemIODirectory.Exists(path) THEN
	SystemIODirectory.Delete(path, recursive);
	IF showconfirmation THEN BEGIN
		IF NOT SystemIODirectory.Exists(path) THEN





I have attached the sample codeunit Cod50051.txt (2.2KB)

Software: MenuRead Updated

An update to MenuRead, a tool that allows you to easily find where objects are located within the Microsoft Dynamics NAV menu structure, is now available. The latest version includes several enhancements:

  • Added support for 2015 objects (Page, Xmlport)
  • Added option to export menu as XML file

MenuRead is a utility that was designed to make an easier task out of finding where items are located on a Microsoft Dynamics NAV menu. Download MenuRead >>here<<

Microsoft Dynamics NAV: Ends with what character?

When working with the files, either for importing, exporting or saving, along with the filename (i.e. abcdef.txt) the file path (i.e. c:\temp\) needs to be specified. The path and filename combined (i.e. c:\temp\absdef.txt) is the absolute path.

In most cases this path is specified in a “setup”. As part of a “setup” this path is often manually entered and may be entered either as “c:\temp” or “c:\temp\”. Combining this path with a filename can lead to some mixed results as “c:\temp\abcdef.txt” and “c:\tempabcdef.txt” are not the same. How do we know if the path text ends with a specific character? In Microsoft Dynamics NAV there isn’t an “EndsWith” function available to pass the text into. However, there is an EndsWith Method in the String Class that we can easily incorporate into Microsoft Dynamics NAV with dotNET Interop.

Name			DataType	Subtype
NetString DotNet		System.String.'mscorlib'
EndsWith(string : Text[250];endswith : Text[5]) : Boolean
//string: is the text value to examine
//endswith: is the text value to compare

NetString := string;
EXIT (NetString.EndsWith(endswith));


Microsoft Dynamics NAV: Delete Remote FTP Files using dotNET Interop

In many data exchanges it is desired that a file be removed from the remote FTP server once it has been downloaded.  Building on the previous posts (List, Upload, Download) that extend FTP within Microsoft Dynamics NAV via dotNET Interop, the code listed below demonstrates how to remove a file via FTP.

The local variables are declared as:

Name	DataType	Subtype
FtpWebRequest	DotNet	System.Net.FtpWebRequest.'System'
FtpWebResponse	DotNet	System.Net.FtpWebResponse.'System'
NetworkCredential	DotNet	System.Net.NetworkCredential.'System'

The function is declared as:

DeleteFile(siteaddress : Text[250];username : Text[80];password : Text[80];filename : Text[250])

FtpWebRequest := FtpWebRequest.Create(siteaddress + '/' + filename);
FtpWebRequest.Credentials := NetworkCredential.NetworkCredential(username, password);

FtpWebRequest.Method := 'DELE';
FtpWebRequest.KeepAlive := TRUE;
FtpWebRequest.UseBinary := TRUE;
//FtpWebRequest.UsePassive := FALSE;

FtpWebResponse := FtpWebRequest.GetResponse;

  MESSAGE('%1 %2', FtpWebResponse.StatusCode, FtpWebResponse.StatusDescription);