This guide covers the use of the WinRAR command-line tools to compress and uncompress files in a directory, and their use in batch files. This guide is an extension of a previous post, Automate Zipping Tasks using the Command-line Interface, that covered the use of command-line tools for two free compression utilities, IZArc and 7-Zip. The information in this guide was tested on a Windows PC running Vista.

compress

WinRAR is a popular and powerful archive manager that includes the command-line tools rar.exe and unrar.exe. Rar.exe is used to compress and unrar.exe to uncompress. Both executables are located in Program Files\WinRAR in the installable version. Although WinRAR is shareware, it can be used on a trial basis for 40 days. Using WinRAR’s command-line tools is similar those for IZArc and 7-Zip. The syntax for the executables is:

WinRAR <command> -<switch1> -<switchN> <archive> <files...> <@listfiles...> <path_to_extract\>

Examples to compress a folder:

rar a -r yourfiles.rar *.txt c:\yourfolder

creates archive yourfiles.rar and compresses all .txt files in c:\yourfolder and subfolders

rar a yourfiles 

creates archive yourfiles.rar and compresses all files in the current folder, but doesn’t include subfolders (note lack of extension, WinRAR will use the default extension .rar)

“a” command adds to archive

“-r”  switch recurses subfolders

Examples to uncompress a folder: 

unrar x c:\yourfile.rar *.gif c:\extractfolder\

extracts all *.gif files from yourfile.rar to c:\extractfolder\ (trailing backslash required) and restores the folder structure

unrar e c:\yourfile.rar 

extracts all files in c:\yourfile.rar to the current folder (folder structure ignored)

“x” command extracts with full paths

“e” command extracts and ignores paths

Basic rules for WinRAR:

  • When files or listfiles are not specified, all files in the current folder are processed
  • When specifying all files in a folder, yourfolder or yourfolder\*.* are equivalent
  • Folder structures are automatically saved in archives
  • WinRAR  uses the default .rar extension, but that can be overridden by specifying the zip extension in the archive name
  • Switches and commands are not case sensitive and can be written in either upper or lower case

Another point is that WinRAR doesn’t appear to use the Windows path environment variable, so it must be specified either at a command prompt, set permanently in the environment variable settings, or specified in a batch file.

To set it temporarily at a command prompt or in a batch file, just enter

set path="C:\Program Files\WinRAR\";%path%

To set it permanently in the Windows path, use start–>Control Panel–>System–>Advanced system settings–>Advanced Tab–>Environment Variables–>System Variables–>Path–>Edit. Add the path ;C:\Program Files\WinRAR; to the end (don’t forget the single semicolons at the beginning and end). Hit OK three times.

Using WinRAR in Batch Files:

Two batch file examples are provided. The first compresses all files in a folder, with the option to compress all files into a single archive or to compress them individually. The second batch file decompresses all .rar files from a folder and places the extracted files into another directory. Be sure to change the extension(s) to .bat before using either file.

compress_rar.bat

uncompress_rar.bat

 

Share

22. October 2011 · 1 comment · Categories: Windows · Tags:

Hard links, Junction Points and Symbolic links are linking mechanisms used to refer to other files, directories, or volumes. Generally, a Hard link is a file that acts like a representation of another file on the same drive without actually duplicating that file. A Junction Point (or directory hard link) is a type of hard link that acts like a representation of a directory, a partition or another volume. A Symbolic link (or soft link) is a file similar to a shortcut in that it points to a filename or directory name, but it’s handled at the system level rather than at the application level. Often confused with shortcuts as well as with each other, Hard links, Symbolic links and Junction Points are not the same; although like shortcuts, deleting either links or junction points usually won’t delete the target and vice versa (see exceptions below). Of the three, Symbolic links are more similar to shortcuts than either Hard links or Junction Points and are generally more flexible, easier to use and safer. When given a choice, most professionals recommend using Symbolic links.

Although Symbolic linking has been around since 1978 and since standardized in UNIX and UNIX-like Operating Systems, it’s been present in Windows only since Vista. Symbolic Linking was introduced in Vista to replace Hard Links and Junction Points and to enhance compatibility with UNIX and UNIX-like systems. Hard Linking for files and support for directory Hard Links (Junction Points) have been present in Windows since Win2K – often using different implementations in each succeeding Windows version. Because the development of linking in Windows has been relatively recent and ongoing, a number of different linking methods have been included, supported, or made available as third-party add-ons depending on the Windows version. Currently, Hard Links, Junction Points and Symbolic Links in Windows are only supported for the NTFS file system. Note that Windows does not support Hard Links or Junction Points to directories on remote shares; however, Symbolic Links can point to remote files and directories on SMB network paths.

Hard links, Junction Points, Symbolic links, and Shortcuts are further contrasted below:

Hard link (Linking for individual files):

  • A file that acts like a representation of a target file
  • Has the same size as the target without duplicating it (doesn’t use any space)
  • Interpreted at the operating system level (SW apps can act upon the target through the link)
  • Deleting the hard link does not remove the target file
  • If the target is deleted, its content is still available through the hard link
  • Changing the hard link contents changes the target’s contents*
  • Must reside on the same partition as the target file
  • Compatible with Win2k and above in Windows

* Some text editors save changed text to a new file and delete the original file, which can break the link. This behavior can be changed in some editors by forcing a save over the original file instead. See discussion at Jameser’s Tech Tips here for more information.

Junction Point (Directory Hard Link):

  • A file that acts like a representation of a target directory, partition or volume on the same system
  • Has the same size as the target without duplicating it (doesn’t use any space)
  • Interpreted at the operating system level – transparent to SW programs and users
  • Deleting the junction point does not remove the target*
  • If the target is deleted, its content is still available through the junction point
  • Changing the junction point contents changes the target’s contents
  • Can reside on partitions or volumes separate from the target on the same system
  • Compatible with Win2k and above in Windows

*A junction point should never be removed in Win2k, Win2003 and WinXP with Explorer, the del or del /s commands, or with any utility that recursively walks directories since these will delete the target directory and all its subdirectories. Instead, use the rmdir command, the linkd utility, or fsutil (if using WinXP or above) or a third party tool to remove the junction point without affecting the target. In Vista/Win7, it’s safe to delete junction points with Explorer or with the rdir and del commands.

Symbolic link (Soft Link):

  • A file containing text interpreted by the operating system as a path to a file or directory
  • Has a file size of zero
  • Interpreted at the operating system level – transparent to SW programs and users
  • Deleting the Symbolic link does not remove the target
  • If the target is moved, renamed or deleted, the link points to a non-existing file or directory
  • Points to, rather than represents, the target using relative paths
  • Can reside on partitions or volumes separate from the target or on remote SMB network paths
  • Compatible with UNIX and UNIX-like systems and with Vista and above in Windows

Shortcut:

  • A file interpreted by the Windows shell or other apps that understand them as paths to a file or directory
  • File size corresponds to the binary information it contains
  • Treated as ordinary files by the operating system and by SW programs that don’t understand them
  • Deleting the shortcut does not remove the target
  • Maintains references to target even if the target is moved or renamed, but is useless if the target is deleted
  • Points to, rather than represents, the target
  • Can reside on partitions or volumes separate from the target on the same System
  • Compatible with all Windows versions

Windows Applications for Creating Links:

A number of applications are available for creating links in Windows including those bundled with the operating system and third-party tools. Some of the more well known are summarized below.

Utilities included with Windows:

  • fsutil – a command line tool included with WinXP and above. It can only create hard links for files – it doesn’t create directory hard links (junction points) or symbolic links. Further information about this tool is available at Commandwindows.com.

usage: FSUTIL hardlink create new_filename existing_filename

  • mklink – a command line tool included with Vista and Server 2008 and above. The most current link creation tool included with Windows. It creates Hard Links, Symbolic Links and Junction Points.

usage: mklink [[/d] | [/h] | [/j]] <NameofLink> <Target>

/d – Creates a Symbolic link for a directory. If no flag used, creates a symbolic link for a file (default)

/h – Creates a hard link

/j – Creates a junction point

<NameofLink> – The name for the Symbolic link being created

<Target> – The relative or absolute path of the target

/? – Help

Third-party Tools:

Command line tools:

  • junction.exe – by Sysinternals  - Creates Junction Points and includes additional commands for displaying and deleting them. Runs on Windows XP and higher and Windows Server 2003 and higher.

display reparse point* info usage: junction.exe [-s] [-q] <file or directory>

-q – Don’t print error messages (quiet)

-s – Recurse subdirectories

create usage: junction.exe <junction directory> <junction target>

delete usage: junction.exe -d <junction directory>

*Reparse points are redirections in the Windows file system using user-defined data in tags to identify and process files.

  • linkd.exe – Part of the Windows 2003 Resource Kit  - Creates and deletes junction points. (For Win2k, WinXP and Windows Server 2003)

create usage: linkd newdirname existingdirname

delete usage: linkd newdirname /D

Context Menu/Shell tools:

  • NTFS Linkelsdoerfer.name – Creates Hard Links and Hunction Points using drag and drop with the right mouse button. Works with Win2K (NTFS ver 5 or greater) and above.

GUI-based tools:

  • Junction Link MagicRekenwonder Software – GUI-based application to create, list, and remove Junction Points. Also lists Symbolic Links and Mount Points. Works with Win2K, XP, 2003, Vista, Windows Server 2008 and Windows 7.

Tutorials on creating and using Hard Links, Soft Links and Junction Points:

Daniel Einspanjer’s journal - How To Use Hardlinks And Junctions In Windows

The Developer’s TidbitWindows File Junctions, Symbolic Links and Hard Links

How-to GeekUsing Symlinks in Windows Vista

Jameser’s Tech TipsTip #37: Creating NTFS Hard Links in Windows XP

Jeff Wouters’s BlogMaking soft and symbolic links in Windows

Maxi-Pedia – Mklink in Windows

TechRepublicVista’s symbolic links feature saves navigating time

 

for more information.

Share

This guide demonstrates how to automate repetitive compression tasks using the CLI (command-line interface) or batch files with two popular free archiving tools, the command-line utility for IZArc (freeware) and the command-line version of 7-Zip (open-source). Batch files are used because they are fairly easy to create, popular and can operate on several versions of Windows and DOS. Although batch files (*.bat) are covered in this guide, other scripting languages may also be used.

You may be thinking, “why would anyone want to automate a compression task in the first place”? Previously, I had a similar attitude until I had a need to compress, copy, rename, and move files from the same folder regularly, so I sought out a way to automate this process to save time and reduce errors.

This guide covers the use of two free archiving utilities, the command-line add-on utility for IZArc and the command-line version for 7-Zip. These two utilities were chosen because they are free, work well, and have a CLI built-in or available as a separate add-on (for IZArc). Using either of these utilities to automate your archiving needs is fairly easy to do, costs nothing and saves time. Although this guide is designed to enable the user to accomplish this task quickly and easily, it is assumed that the reader is already familiar with creating and using batch files and has some experience with using compression utilities. WinXP was used to verify the information in this guide.

Step 1. Download one of the following (32 bit versions were used for this guide)

The versions used for this guide were: 7-Zip Command Line Version 4.65, IZArc Version 4.1, and IZArc Command line Add-On Version 1.1.

Note: This guide uses the Command Line Version of 7-Zip (7za.exe), which is a stand-alone version of 7-Zip that supports only the 7z, cab, zip, gzip, bzip2, Z and the tar formats. The full version of 7-Zip (installable and portable versions) contains a command line executable (7z.exe) that is more full-featured and explained in the 7-Zip help files. The IZArc command-line utility is an add-on to IZArc and supports the zip, jar, bh, cab, and the lha formats.

Step 2. Install the utility and set the path variable

For 7-Zip:
Extract the 7-Zip file to a location of your choice, for example C:\7zip. For convenience and to make the 7-Zip commands available to the batch file, manually set the path variable for the chosen location, e.g., C:\7zip. To set the path, right click My Computer–>Properties–>Advanced Tab–>Environment Variables. In the System Variables Area, select path, then click the Edit button. Add “;C:\7zip;” (or your folder location) to the end of the line (don’t include the quotes and don’t forget the semicolon at the start and end). Hit OK three times.

For IZArc:
First, install the IZArc utility and then install the IZArc Command-line Add-on to their default locations (C:\Program Files\IZArc) by double clicking them. The IZArc Command-line Add-on will install the Command-line executables izarcc and izarce to the IZArc directory in C:\Program Files\IZArc. The Path Variable is not set automatically during the installation, so it must be done manually. To set the Path, right click My Computer–>Properties–>Advanced Tab–>Environment Variables. In the System Variables Area, select path, then click the Edit button. Add “;C:\Program Files\IZArc” to the end of the line (don’t include the quotes but don’t leave out the semicolon at the start). Hit OK three times.

Step 3. Test the installation

Open a CMD window by hitting the Start button, enter “cmd” and press “OK”. A command-line window will open. Type one of the following into the command prompt window:

For 7-Zip:
Type “7za” and then press ENTER. If installed correctly, you should see the usage commands for 7-zip as shown in the top left thumbnail (click the picture to enlarge):

For IZArc:
Type “izarcc” and then press ENTER. If installed correctly, you should see the usage commands for IZArc as shown in the top right thumbnail (click the pictures to enlarge and view content):

Note: For IZArc, the command-line executables are izarcc for the compression function and izarce for the extraction function. If you didn’t get one of the screens shown above, then check that the Path Variables are present and correct for the 7-Zip or IZArc installation.

Step 4. Create the batch file

There are many ways to create batch files: from the simple to the complex. For the sake of simplicity and to focus on quickly demonstrating the use of these two compression utilities, this guide uses batch file examples compresses all files from a folder into a single zip archive with a unique date suffix, moves the zip archive to another folder, and then deletes the original files. The thumbnails above show the batch files for 7-Zip (bottom left) and IZArc (bottom right). Click to enlarge them to see the contents. In the batch files, the folder “testzip” contains the files to zip and the folder “testmove” is where the archive is to be moved.

Depending on which utility you wish to use, click one of the following links to open the batch file text in a new window where you can cut and paste it into your text editor or download it: testizarc.txt (IZArc) or test7zip.txt (7-Zip). Note: if you use either one of these batch files be sure to change the extension to .bat, create the “testzip” and “testmove” folders on your C: drive, and copy some files you want to zip to the “testzip” folder. Of course you can change either batch file for your own needs using a text editor to change the zip folder (testzip) and move folder (testmove) to folders of your choice.

An explanation of the 7za command-line in the 7-Zip batch file (test7zip.txt):
7za a -tzip “C:\testmove\xxxx_%TODAY%.zip” “C:\testzip\*.*” -mx5

  • “7za” = (required) this starts the 7-Zip command-line executable.
  • “a” = (required) command to add files to the archive.
  • “-tzip” = (optional) switch to set the type of archive; in this case, it’s a zip file (optional unless using another compression format).
  • “C:\testmove\xxxx_%TODAY%.zip” =(required) the name of the archive to create.["testmove" is the folder where the archive is to be created. "xxxx" is the name of the archive. "%TODAY%" is today's date and time variable added to the zip archive name to insure a unique archive name is created each time the batch file is executed. The parentheses are optional unless there are spaces in the file path. Finally, ".zip" is the type of archive to be created.] 
  • “C:\testzip*.*” = (required) the files to be archived. In this case, it’s all the files in the folder “testzip” (this includes any subfolders regardless of the “-r” switch setting, see use below).
  • “-mx5″ = (optional) switch that sets the compression method. In this case, it’s zip mode, compression level 5 (level 5 is normal compression, which is the default level).

When using the zip format and normal compression, the optional switches can be eliminated and the command shortened as shown below:

7za a “C:\testmove\xxxx_%TODAY%.zip” “C:\testzip\*.*”

Concerning subfolders: Officially, the “r” switch is used for searching and unless included, recurse subdirectories is disabled. However, because of a glich in the program, the zip command always includes subfolders regardless of “r” switch setting in situations where all files in a folder are to be archived using the wildcards “*.*”. For this reason, the “r” switch is not used in any of the 7-Zip example commands in this guide. See 7-Zip forum topic or 7-Zip FAQ’s – Why doesn’t -r switch work as expected? for further information.

An explanation of the IZArc command-line in the IZArc batch file (testizarc.txt):
izarcc -a -cx “C\:testmove\xxxx_%TODAY%.zip” “C:\testzip\*.*”

  • “izarcc” = starts the izarcc compression executable.
  • “-a” = adds files to the archive.
  • “-cx” = sets the compression level to maximum.
  • “C:\testmove\xxxx_%TODAY%.zip” = the name of the archive to create.["testmove" is the folder where the archive is to be created. "xxxx" is the name of the archive. "%TODAY%" is today's date and time variable added to the zip archive name to insure that a unique archive name is created each time the batch file is executed. The parentheses are optional unless there are spaces in the file path. Finally, ".zip" is the type of archive to be created.]
  • “C:\testzip\*.*” = the files to be archived. In this case, it’s all the files in the folder ” testzip” (this doesn’t include any subfolders unless the “-r” command is added, see usage below).

Additionally the izarcc switches “-r” (include subfolders) and “-p” (store relative pathnames) may be used:
izarcc -a -r -p -cx “C:\testmove\xxxx_%TODAY%.zip” “C:\testzip\*.*”

“-r” = recurse into subfolders (include subfolders)

“-p” = store relative pathnames (maintains directory structure)

Currently, the IZArc help file for the IZArc program, IZArc.chm, contains limited information for the izarcc (compress) or izarce (extract) command-line functions; however, the command-line Add-on utility does include a text manual (manual.txt) that explains the commands and includes some examples for their use.

Summary

It’s fairly easy to automate archiving tasks using a simple batch file using either of the two free utilities covered in this guide. For basic needs, the command-line utility for IZArc or the 7-Zip command-line version (7za.exe) should suffice. For more complex requirements, the full version of 7-Zip (7z.exe) contains additional features; allowing users to perform more sophisticated archiving tasks from the command-line.

For more information and examples for using the 7-Zip command-line, also see the following:

Dot Net Perl’s article, 7-Zip Command-line examples

Codejacked’s article, ZIP Up Files from the Command Line

The official 7-Zip Manual, 7ZIP manual & documentation


Share

If you ever accidentally formatted an external drive on a PC with several plugged-in USB devices, you probably remember seeing them listed in the formatting tool’s selection box. The  (C:) and (D:) drives may be listed, which are usually the boot drive and the backup drive. If an (E:) is displayed, it’s probably the optical drive. However, distinguishing between the other drives or devices may not be as apparent, especially when information beyond the drive letter assignment isn’t shown. This is particularly true for drives or other USB  devices that don’t have identifying information on them. Under these conditions, it’s easy to become careless or impatient and select the wrong drive, especially if its’ letter assignment appears to be correct. If you are unlucky, you may notice a flashing light on the device confirming that the wrong drive is being formatted.

mystica_USB_Flash_Drive (public domain clip art www.clker.com)

I did this recently when in a hurry and chose the wrong drive letter.  Instead of formatting the flash drive, the external 250GB backup drive was formatting. Panicking, I immediately realized the error and stopped the formatting process, but it was too late. When I checked the hard drive, it couldn’t be accessed. The drive letter was still visible in Windows, but no files or folders were displayed and nothing could be written to or read from the drive.

Fearful of losing the data, I tried several file recovery tools with no success until using TestDisk. TestDisk is a free (open source) data recovery tool available from CGSecurity. With TestDisk, I was able to successfully restore the drive’s partition and it worked normally.

Luckily, all of the data was still intact. That’s because during a high-level format (quick format), only the external drive’s partition table information or boot sector are erased. According to Windows Help Central, even a full format can be recovered pretty easily as long as the original files are not overwritten with new data. That’s because the spaces containing file information on the disk drive are not really wiped clean, but instead, the areas where files are stored are marked as available for new data. As long as no new data is written to the drive, chances are excellent that the data is 100% recoverable using simple freeware data recovery tools. Anyway, if you hosed the partition table or MBR (Master Boot Record) during the format process, the drive isn’t likely to be writable anyway.

TestDisk is a utility that can be used to restore corrupted and missing MBRs, partition tables, and data. TestDisk works on most operating systems including Windows, Linux, BSD, SunOS, and Mac OSX and it’s included with many Linux LiveCD distros such as PartedMagic and many others. It’s a very useful and sophisticated tool in the hands of experienced users. Unfortunately, because data recovery can be a complex issue, inexperienced users may find it “user unfriendly”. However, documentation for the program is thorough and one can find many examples, technical notes, and step by step instructions in the documentation section on TestDisk’s Wiki. Also, the TestDisk Step by Step guide contains plenty of screen-shots which greatly helps in the data recovery process.

The following screen-shots show what to expect after starting TestDisk for recovering a deleted partition (note: the screens below show the results for a working hard drive that doesn’t have any problems).


Above is the first screen. For most cases,  select the default option – Create a new log file.


Next, select your media device with the problem. In the screen above, the main hard drive is selected.


Select Intel if using a Windows or Linux machine.


In most cases “analyse” should be selected.

FYI, “Advanced” provides options to restore the boot sector or to images for partitions.

analyse
Analyses displays a preliminary list of the current partitions for the drive selected. Next, select Quick Search to continue searching for additional partitions.

quicksearch
“Yes” should be selected for most situations.

selectpartition
TestDisk displays the structure analysis results and displays the partitions. Healthy partitions will be highlighted in green. Here, you can use the up/down arrow keys to select a partition to further analyze or recover, then hit ENTER. 

Pressing “P” will list the files in the partition, which can provide some assurance that the data is still intact. Do not use the left/right keys – they are used to change the partition’s characteristics!

deepersearch

If the partition you want to restore is listed, select “Write” and then Enter to restore it and that should be it. If TestDisk didn’t find your partition, you have the option to perform a deeper search by selecting “Deeper Search”. In the screenshot above, the results show that no problems were found (the screenshots are for a working hard drive).

For more information, the TestDisk Wiki’s Step by Step instructions explain the recovery procedure well enough so that even novices shouldn’t have a problem.

—————————————————————————————————————————————————————————

Other TestDisk how-tos:

The How-to-Geek explains recovering partitions using TestDisk using an Ubuntu LiveCD (ver 9.10):

Recover Data Like a Forensics Expert Using an Ubuntu Live CD

A MakeTechEasier how-to for data and partition recovery using TestDisk:

How to Recover Data and Partitions for Free with TestDisk

Other Data Recovery Utilities

If the drive is still accessible or if only a few files are missing, you may want to try one of the easier-to-use file recovery programs listed below before trying TestDisk. Some that I’ve successfully used in the past to recover lost data are:

Recover Files

Recuva

Restoration

PhotoRec

Install or Recover MBR or Boot Sector

If you know the problem is a damaged or missing MBR (Master Boot Record), a number of tools can be used to repair or restore it – including TestDisk.

Boot Sector Recovery:

Sourceforge article for recovering a Boot sector using TestDisk:

Boot Problems:Boot Sector (follow the steps in the either case section as shown below)

  • 1st screen: select No Log and then press ENTER
  • 2nd screen: select the drive to restore and select Proceed
  • 3rd screen: select Intel
  • 4th screen: select Advanced
  • 5th screen: select the partition and select Boot
  • 6th screen: select Rebuild BS
  • 7th screen: type “Y” to confirm

If using the Grub bootloader, make sure to update grub.

MBR Recovery:

Write a new MBR with TestDisk:
  1. Start TestDisk
  2. Create a new logfile
  3. Select a media
  4. Select partition table type (Intel, Mac, Sun, etc)
  5. MBR code (this writes a new MBR)

Below are are other tools that can be used to install, repair or restore the MBR. Note that some of the tools listed require that a backup of the MBR was previously saved:

Bootice (restores a saved MBR only)

How to fix MBR in Windows XP and Vista

MBR Tools, Disk Repair and Disk Recovery Freeware

5 Free Tools to Backup and Restore Master Boot Record (MBR)

MBR, Partition Table and Boot Record Tools

Summary

If the easier-to-use tools above don’t work for whatever reason, give TestDisk a try to recover the partition and/or MBR, but follow the directions carefully. Although fairly simple, it’s still possible to mess things up so thoroughly that the only way to recover the data would be to send the drive to a very expensive professional data recovery service.

Remember, if you accidentally format or erase files from your external USB drive, don’t panic. Get to work on recovering your data because chances are that it can be accomplished more easily than you think.

 

Share

Bad Behavior has blocked 373 access attempts in the last 7 days.