Click here to Skip to main content
15,885,870 members
Articles / Programming Languages / C++11

Extending boost::filesystem for Windows and Linux: Part 1

Rate me:
Please Sign up or sign in to vote.
4.93/5 (15 votes)
19 Mar 2013CPOL16 min read 31.2K   977   33  
Extending boost::filesystem for Windows and Linux.
 
						      Overview of options available by FSHelper console application
 


FSHelper is intended to be frontend for FileSystemController multifunctional class which extends and somewhat simplifies usage of boost::filesystem. It contains 
basic functionalities like Move, Copy, Rename and Delete with additional capabilities like sending files/folders to Recycle Bin (Windows) or Trash (Linux). It also provides FileSystemWatcher functionality which gives the opportunity to watch real-time changes in destination folder. I also implemented Search capability which allows you to search for files starting from destination folder based on criterias like part of the file name or path. Program is designed to be usable but it's main purpose is to provide basic insight to underlying functionalities of FileSystemController and supporting C++ classes. Here are some usage examples with brief explanations:

  

FSHelper -h

==> Provides the list of options available 

  

FSHelper --test_wildcard="C;\*.exe"

==> Lists files that match criteria defined with wildcard '*'. Supported wildcard combinations are *.* , x*.y , *.y , *x.y , *x*.y , x*.* where x and y represent the part of filename and extension.	Combinations of *.* and x*.* can be used for folders too unlike other combinations which are specific for files only.
 

FSHelper --watch_location="C:\Test_Folder"

==> Program starts listening for basic changes like deleting, creating, removing items and similar. If you want more detailed overview of changes you should use additional option --watch_full which will allow you to detect changes like attribute/permission changes on Windows or a lot of other stuff on Linux. Be aware that --watch_full produces a lot more output than watching for basic changes (especially on Linux). Typing 'p' pauses watching for changes and additional 'p' restores it. Typing 'q' in console will stop further watching on defined location.
  

FSHelper --pdel="C:\Folder_to_Delete_content\*.*"

==> Deletes item(s) on specified path. Be aware that this is a PERMANENT DELETION operation which won't	give you any additional warnings upon execution. I designed these options with speed and usability in mind so I guess that you know what you are doing once you try to use this functionality.
  

FSHelper --make_path="C:\path_1\part_2\part_3"

==> This will create folder hierarchy like defined in the path. If parts or the whole path already exist it will just create missing parts of the hierarchy or make no changes (if the complete path already exists. 
  
  

FSHelper --copy --from="C:\Test_Folder" --to="C:\Location_2\Test"
  
  
FSHelper --copy --from="C:\test\*.*;C:\test2\p*.txt" --to="D:\location_2"
  
  
FSHelper --copy --from="C:\test" --to="C:\test2\destination_folder" --merge
  
  
FSHelper --copy --from="C:\test" --to="C:\test2\destination_folder" --overwrite

*=> First example will copy "Test_folder" folder to "Location_2" with name "Test" if it doesn't exist there already. If there is a folder "C:\Location_2\Test" copy of "Test_Folder" will be made as "C:\Location_2\Test\Test_Folder". Operation will give an error report if something is wrong and restore the original state. 

*=> Second example uses wildcards to define multiple files in first source path but there is also a second path defined so the summary of them will be copied to destination defined in --to option. 

*=> Third example assumes that "C:\test2\destination_folder\Test" already exists. It will copy all files/folders that are missing in destination and skip all other items if present without errors. 

*=> Finally, if you want to overwrite complete content of "C:\test2\destination_folder\test" with content of "C:\test" location this is the way to do it. If, on the other hand you just want to overwrite .txt files in "C:\test2\destination_folder\test" that are already present you should use:
  
  

FSHelper --copy --from="C:\Test\*.txt" --to="C:\test2\destination_folder\test" --overwrite
  
  

==> options COPY, PDEL, WATCH_LOCATION and MOVE support defining multiple source destinations separated by ';':
  
  

FSHelper --move --from="C:\Test\*.*;C:\Test2\*.*" --to="C:\test2\destination_folder" --overwrite
  
  

==> MOVE operation should also be safe in terms that it won't change original states of source and destination if something goes wrong during the process. MOVE operation won't support --merge option like COPY operation does.
 Other options and behaviors are similar to COPY operation. 
  

FSHelper --rn --from="Test" --to="Test2"

==> Here "Test" defines CurrentPath/Test while --to option DOESN'T ACCEPT FULL PATH but just a NEW NAME. You could define full path in --from option unlike --to option which is specific behavior to this functionality only concerning that the same action can be done by using --move with defining full --to path.
  

FSHelper --list="C:\"

==> Lists all visible entries from defined location. If you want mode detailed descriptions of file system items which are listed use --v option. If you want to see all items including hidden ones use --all option like:
  
  

FSHelper --list="C:\" --v --all  
  

FSHelper --list_trash

==> Lists contents of Trash/Recycle Bin locations with recovery paths. All items are enumerated so you can use this number to identify which item to recover by using --from_trash option (Linux only).
  

FSHelper --to_trash="C:\test_folder"

==> Sends "test_folder" to Recycle Bin (or Trash on Linux)
  


./FSHelper --from_trash=3 --overwrite

==> Restores item from Trash (Linux only). Number which defines specific item can be obtained by using --list_trash option. Option --overwrite will restore specific Trash item replacing currently existing one assuming the item with the same name already exists.There is also a possibility to define different restoration path with option --restore_path like:



./FSHelper --from_trash=3 --restore_path="/home/user/New Location"
  


./FSHelper --size_trash

==> Gives summary of all item sizes in Trash location (Linux only)

  

FSHelper --search_loc="C:\" --search_string="abc"

==> Searches for the file/folder with "abc" in its name. If you want to find exact matches to defined file/folder name use --exact_match option. If you want to case sensitivity in your search use --case_sensitive option. Finally, if you are looking for part of the path you can use --search_path option. To summarize, this line will search for path which contains "test\test2" and will be case sensitive:
  
  

FSHelper --search_loc="C:\" --search_string="test/test2" --search_path --case_sensitive


==> Note that you can make search much faster by limiting the number of possible results like:

FSHelper --search_loc="C:\Test" --search_string="test_2" --result_count=3
  
  
  

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Serbia Serbia
Software developer with couple of years of experience mostly with .NET programming and MS SQL databases currently interested in expanding knowledge to C++ and other operating systems.

Comments and Discussions