• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[C++] Rename all files within folder

Joined
Jul 26, 2006
Messages
3,634
Reaction score
1,007
I've been struggling with this for a few hours now... Absolutely starting to hate C++ for its strictness with variable types in comparison to PHP ._.

I basically have this simple form where the user selects a folder and I want to replace all spaces in the filenames of the files in that folder to underscores. Sounds easy but I can not work it out. Selecting the folder goes perfectly fine, but in button2_Click I need to do the renaming and every single code snippet on earth uses char* or another Ducking type. I believe I get a string from the folder selection dialog but nothing works with that. Why the duck does it have to be all specific?!

Using Visual Studio 2010 Express, here's my code (Form1.h)

Code:
#pragma once

namespace SpacesToUnderscores2 {
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::FolderBrowserDialog^  folderBrowserDialog1;
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Button^  button2;
	private: System::Windows::Forms::Button^  button3;
			 bool folderSelected;
	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->folderBrowserDialog1 = (gcnew System::Windows::Forms::FolderBrowserDialog());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->button3 = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// folderBrowserDialog1
			// 
			this->folderBrowserDialog1->Description = L"Select the directory";
			this->folderBrowserDialog1->ShowNewFolderButton = false;
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(15, 54);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(89, 40);
			this->button1->TabIndex = 0;
			this->button1->Text = L"Select folder";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(12, 9);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(81, 13);
			this->label1->TabIndex = 1;
			this->label1->Text = L"Selected folder:";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
			this->label2->Location = System::Drawing::Point(15, 22);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(29, 15);
			this->label2->TabIndex = 2;
			this->label2->Text = L"N/A";
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(110, 54);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(89, 40);
			this->button2->TabIndex = 3;
			this->button2->Text = L"Spaces to underscores!";
			this->button2->UseVisualStyleBackColor = true;
			this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
			// 
			// button3
			// 
			this->button3->Location = System::Drawing::Point(219, 62);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(22, 25);
			this->button3->TabIndex = 4;
			this->button3->Text = L"\?";
			this->button3->UseVisualStyleBackColor = true;
			this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(253, 106);
			this->Controls->Add(this->button3);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->button1);
			this->Name = L"Form1";
			this->Text = L"SpacesToUnderscores";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
			 }

		// Bring up a dialog to open a file.
		void button1_Click(Object^ /*sender*/, System::EventArgs^ /*e*/)
		{
			System::Windows::Forms::DialogResult result = folderBrowserDialog1->ShowDialog();
			if(result == System::Windows::Forms::DialogResult::OK)
			{
				this->label2->Text = folderBrowserDialog1->SelectedPath;
				folderSelected = true;
			}
		}

		void button2_Click(Object^ /*sender*/, System::EventArgs^ /*e*/)
		{
			if(!folderSelected)
			{
				System::Windows::Forms::MessageBox::Show(this, "Select a folder first!", "Error", MessageBoxButtons::OK);
			}
			else
			{

			}
		}

		void button3_Click(Object^ /*sender*/, System::EventArgs^ /*e*/)
		{
			System::Windows::Forms::MessageBox::Show(this, "This application will replace the spaces in filenames of files in the selected folder with underscores.\n\n(c) Floris den Hartog, 2012", "Info", MessageBoxButtons::OK);
		}
	};
}
 
Joined
Apr 18, 2010
Messages
674
Reaction score
393
I typically lay off of Unicode and use MBCS (Multi Byte Character Set).

Code:
#ifdef UNICODE
#undef UNICODE
#endif
#ifdef _UNICODE
#undef _UNICODE
#endif
#ifndef MBCS
#define MBCS
#endif

Or just change it in your projects settings. Then the new settings will change the prepocessor definition of the function (say FindFirstFileW to FindFirstFileA) thus changing it from LPCTSTR to LPCSTR, which is compatible with the char pointer variable.
 
Last edited:
Joined
Jul 26, 2006
Messages
3,634
Reaction score
1,007
I've got some sort of solution now, just having the user select a single file. FindFirstFile was throwing an exception but I'mma try and figure it out.

EDIT:
Finished it! Worked out the exception and worked out some more bugs.
 
Last edited:
Joined
Mar 11, 2007
Messages
904
Reaction score
1,254
I typically lay off of Unicode and use MBCS (Multi Byte Character Set).

Code:
#ifdef UNICODE
#undef UNICODE
#endif
#ifdef _UNICODE
#undef _UNICODE
#endif
#ifndef MBCS
#define MBCS
#endif

Or just change it in your projects settings. Then the new settings will change the prepocessor definition of the function (say FindFirstFileW to FindFirstFileA) thus changing it from LPCTSTR to LPCSTR, which is compatible with the char pointer variable.


to properly explain what he means would take too long and you probably wouldn't understand it anyways since you complained about char* in regards to string arguments (small tip, strings in C++ are an encapsulated char* class). the LPCTSTR and LPCSTR are windows defines for types of unicode character sets (its how you can see characters of languages whose character sets are very large i.e. korean/chinese). Windows does this natively because its easier for windows to just take everything in as a wide char (so there is no need for type conversion within the API). there are simple solutions, one you could follow PenguinGuy's suggestion by using MBCS instead of unicode. Or you can just use windows macros like _T("Your text here"). using _T only works on string literals tho, not variables. you must recast the variable to LPCTSTR using C-style or your favorite C++ method.
 
Back
Top