Script to rename files – Use extension less files in Dreamweaver

Below are two Powershell scripts that are renaming files with extensions in a directory to files without extensions (first script) and back again (second script). They are intended to be used with Dreamweaver if you want to use Dreamweaver to develop your website but want to use extensionless URIs on the webserver. However, of course the scripts can be useful for you in many other situations where you want to batch rename files!

Use extensionless files in Dreamweaver

If you use extensionless URI (URL, or web addresses to be clear) it is easy to change technology behind your webpages without having to change the URI itself. The web address for your webpage can be widely spread and you really never should change it. But if you develop your website using ASP.Net for example and use the aspx extension for all your files eg. www.yourwebsite.com/content.aspx you get problems if you in the future for instance want to develop you site using PHP or maybe some other technology yet to come. Should you rename your files and risking all those precious links to your webpages? Or should you continue to use the aspx extension even if the technology behind is PHP? At the end of the day, file extensions for your web files is just a bad idea for other reasons as well. It makes your URI harder to remember since you need to remember the extension. It also gives hackers more information than needed about the technology behind your page.

Sadly enough, Dreamweaver doesn’t support extension less file names! Therefore you might want to use these scripts to work in Dreamweaver with extensions on your files and then remove the extensions with the first script before publishing you files to the web server. When you want to work with your files again, you just use the second script to add the extension again. In the scripts conversions are made from html-files to extension less files and back again, but of course you could convert from and to other extensions as well by modifying the scripts accordingly.

Script 1 – Powershell script to convert from files with extensions to extension less files

# Path to where the renaming of files should take place
$path = "c:\yourdevelopmentdirectory\*"

# Get all files with "html" extension except for the 
# file "index.html"
$htmlFiles = get-childitem -path $path -include *.html 
   -exclude index.html

# Loop over all html-files...
foreach ($file in $htmlFiles)
{
  # Get the filename length of each file
  $fileLength = $file.name.length

  # Get the new file name which is the filename with 
  # ".html" removed (the five last charachters)
  $noExtensionFile = 
     $file.name.substring(0,$fileLength - 5)
  # Rename the html-file from <filename>.html to just 
  # <filename> without any file extension
  rename-item $file $noExtensionFile -force
}

Script 2 – Powershell script to convert from extension less files to files with extensions.

# Path to where the renaming of files should take place

$path = "c:\yourdevelopmentdirectory\"

# Get all files with no extension and that are not 
# a directory
$noExtensionFiles = get-childitem -path $path 
   -exclude *.* | where {$_.PSisContainer -eq $false}

# Loop over all extensionless-files...
foreach ($file in $noExtensionFiles) 
{
  # Get the new file name which is the extensionless 
  # filename with ".html" added 
  # (the five last charachters)
  $htmlExtensionFile = $file.name + ".html"

  # Rename the extensionless file from <filename> 
  # to <filename>.html with html-extension
  rename-item $file $htmlExtensionFile -force
}

Leave a comment

Your email address will not be published. Required fields are marked *