Tag Archives: folder

Compress files in Windows

As a Windows Network Administrator I always have to make space delete unneeded files, archive old files, compress files that we don’t need but we might need at some point.
I don’t use the same compression method every time, because the scenario is different. Here are a three compression methods for Windows.

Compress Files Using the NTFS Compression

Compress Contents to Save Disk Space

This method of compression only works on NTFS partitions. You don’t have the option for a FAT or FAT32 partition.
The most usual and easier way to do this is to use the Windows’ Graphical Interface and access the Advanced File Properties and select Compress contents to save disk space.

Compact – Compress files or folders from command line

If you would like to compress files from a batch file use this command line variant. Usually if you access the advanced file properties, (right-click => properties and => advanced) you have the option to compress contents to save space. Another good use of the command line is that it gives you a better view of the progress. The graphic interface sometimes hangs and you don’t know if the job is still active.

The command to compress a folder from within the folder is compact /c /s. It compresses all of the files within the folder and marks the folder as compressed so that new files are created compressed.

Why would you choose Compact and not Zipped Folder?

The main reason is that a compacted folder can be used by applications. In other words if the path of a log file points to a compacted folder this is transparent for the application that writes the log file. As a result your log files will be compacted. You cannot do this with Zipped folders or files.

Compress files using the Windows’ built-in zip archiving utility

Send to Compressed (Zipped) folder

Why would you choose Compressed (Zipped) Folder over the Compacted Folder?

The zipped Folder has better compression and better portability. The zipped folder has a slightly better compression rate, and you can copy the files to any other Operating System, send them by email and they will retain the compression. The compacted folders will only be compressed on the original location unless you compress the file again at the new destination. Note in the image below the difference between the two file compression formats. The Windows shell has the option to “show NTFS compressed files in color”, which is a great option. Note that the zipped file is slightly smaller than the compacted, (blue colored), one.

Compact (the native NTFS compression) vs Compress the send to Zipped Folder
Compact vs Compress click to enlarge

Is there a Compressed (zipped) Folder – command line variant?

Unfortunately, there isn’t any Windows built-in option. The good news though is that there is a free archiver that has a command line version as well. 7zip is a great free utility very flexible that manipulates all of the popular archiving file formats.

Compressing from the command line with 7zip is very simple, the simplest command is 7z a NewFolder.zip “New Folder” that compresses the folder New Folder into the archive: NewFolder.zip. For more options and switches type: 7z –h, note that 7z doesn’t understand the /? switch, usual on any Windows application.

Why would you choose 7Zip over the Windows’ built in compressing solutions?

The main reason is flexibility. 7zip has a lot of options/switches that control a lot of the compression aspects and it is very easy to use in a batch file.

An interesting application is to separately archive a lot of folders from a batch file. Let’s say you have 500 folders that you want to archive and you know you will be accessing those folders on a regular. It makes more sense to archive the folders separately and not in one big file. It is easier to access smaller zip files from the Windows shell and keeps the system responsive. To do this manually for 500 folders is a nightmare. Here is a command that runs from a batch file that will compress those folders separately, each folder one zip file:

for /D %%d in (*.*) do 7z a -tzip “%%d.zip” “.\%%d\*”

To run the command from the command line and not from a batch file the command is slightly different:

for /D %d in (*.*) do 7z a -tzip “%d.zip” “.\%d\*”

There are other great archiving utilities such as Winzip, Winrar, Pkzip, etc… They are not free, but you can get a trial-ware which in many cases is good enough for anybody.

A few tips on what to compress and what not.
Usually Installation files are already compressed so there is no benefit in compressing.
Digital photos in a jpg or gif format are already compressed using special algorithms.
PDF files don’t compress.
Text files, (plain text, log files, etc…) compress the best.
Word files can be compressed but not as much as the text files.

How to use 301 Redirect for moved or missing pages

What is 301 redirect?

301 redirect is a method of redirecting pages on your website to other pages on your site or elsewhere.

Why would you need to redirect content?

It is a method to retain search engine rankings for a page. If a page has previously been ranked by search engines and you changed the file name during a major redesign, or moved some of the content to another website the ranking is lost if you just move the page. If instead you do a 301 redirection the old ranking will be passed on to the new page.
Another use is to redirect traffic for deleted content somewhere on your website so that the traffic is not lost. Sometimes you need to remove outdated content and since you are still receiving traffic for those pages you would like to keep those visits.

What means a 301 Redirection?

301 redirect means “moved permanently”. When a search engine accesses a file marked as “Moved Permanently” it will note the new address and consider the new location as the valid one, passing on the ranking for the new page.

How to do a 301 Redirection

Apache 301 Redirection
Look for the .htacces file on the root of your website’s directory.
If there isn’t one create it. On Windows you might have difficulties to create a file with an empty name and only the extension. But once the file created, for instance on the host operating system you can edit the file using notepad or wordpad.
The simplest method is to place the following line into your .htaccess file:
redirect 301 /old-directory/old.htm http://www.mywebsite.com/new.htm
Don’t add the whole address on the first part, (the www) as the server uses its root directory as the referral point and not the http address.
Copy paste the statement below and modify it to reflect your needed redirection.
Upload the file on the server or overwrite the old file if you use direct access.
Test the redirection.

What if you want to redirect all of your files to another address? Fortunately you don’t need to add a redirect for each of your files. You can use the Apache’s URL Rewriting Engine module, which can handle complex redirections using regular expressions.

Redirect for a Moved Website

A rule that will redirect ALL of the files on your web server to another address:
redirectMatch 301 ^(.*)$ http://www.domain.com

A rule that will redirect http://mywebsite.com to http://www.mywebsite.com for SEO purposes:
RewriteCond %{HTTP_HOST} ^mywebsite\.com
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [R=permanent,L]

Redirect to a Different File Extension

A rule to redirect your .htm pages to .php pages:
RewriteBase /
RewriteRule (.*).htm$ /$1.php

Redirect an Entire Directory

Redirect An Entire Directory/Folder to a single page. You got rid of the old content and you want to keep your visitors and keep the ranking of your old pages.
RewriteBase /
RewriteRule ^old-directory/(.*) http://www.mywebsite.com/new-directory/ [R=301,L]

You moved content in another folder or on another webpage and you would like to redirect visitors to exactly the same page at the new address.
Another use of this, is if your test site got indexed by search engines and is ranking better or the same as the regular website. It happened to me… Somehow my robots file got overwriten
RewriteBase /
RewriteRule ^old-directory/(.*) http://www.mywebsite.com/new-directory/$1 [R=301,L]

Redirection Troubleshooting

Make sure that you leave a single space between the different elements of the statement.
Make sure you have “RewriteEngine on” on your .htaccess file, without it your rules will not function.
Regex Tip:
The content between the round brackets is kept in memory and called when needed with the syntax $1 for the content of the first bracket, $2 for the content of the second bracket, etc.

What if I don’t have an Apache server? I use IIS.
The alternative for IIS is ISAPI_Rewrite for IIS. ISAPI_Rewrite gives you all the nice features that you have with Mod_Rewrite.

More on the Rewriting Engine module and the regex here:
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html