CHALLENGE..have fun

Your flash disk with important school files was infected by an LHS virus. You have successfully scanned and removed the virus from all folders in the flash disk. However, you have noticed that one defect was not fixed by the Antivirus program. The virus converted all your Text (“[filename].txt”) files to an unknown LHS format (“[filename].lhs”). When you inspected one of the files, you discovered that the filename and contents were not modified. The virus just changed the extension from “.txt” to “.lhs”. Changing back the extension to “.txt” fixes the issue.

A quick search of the flash disk indicates that you have more than 10,000 text files spread over more than 100 folders. It is going to be impractical to rename all the files manually.

Project Objectives:
The flash disk contents are in a folder named “flash-disk”. Create a program that can visit each folder and its subfolders in the “flash-disk” and rename all “.lhs” files to “.txt”. Once done, display how many folders were visited and how many affected files were fixed by your program.
By "Challenge" you mean your school project? Are you asking for help?

You made this thread back in... September. And then again October. And you never replied to that original thread.
http://www.cplusplus.com/forum/general/262861/
http://www.cplusplus.com/forum/general/264707/
Last edited on
Yes it was my school project though the submission time is over and I just want to know how to do that challenge. If you can help please help.
Use the command line for this kind of work:
find flash-disk -name "*.lhs" -exec sh -c 'mv "$1" "${1%.lhs}.txt"' _ {} \;

In C++ it's pretty easy too:

Iterate the directory entries using std::recursive_directory_iterator. For each directory entry, if it represents a file, look at its path and check its extension. If it's .lhs, use std::filesystem::path::replace_extension to change it, then rename the file.
https://en.cppreference.com/w/cpp/filesystem/recursive_directory_iterator
https://en.cppreference.com/w/cpp/algorithm/for_each
https://en.cppreference.com/w/cpp/filesystem/path/extension
https://en.cppreference.com/w/cpp/filesystem/path/replace_extension
https://en.cppreference.com/w/cpp/filesystem/rename
Last edited on
Topic archived. No new replies allowed.