Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a folder in a specific directory each time a button is pressed,
eg :


Sample on Sun 01.12.2014 At 09.13 AM

so,I think the code can be boiled down to into three basic parts:

1- The Name (sample) should be taken from a text Box
2- the part "on Sun" and 01.12.2014 should be in a different string i suppose?
3- the part "At 09.13 AM" is also a different part i think.



i have researched the code in batch for a while and it kinda looks like this:
the batch code for this operation is :
[

echo off
mode con cols=50 lines=25
title Create Folder for backup
CLS
set /p userinp= ^> Enter Name of Folder to take backup:
for /f "tokens=1-3 delims=:/" %%i in ("%DATE%") do set DATESTAMP=%%i.%%j.%%k
for /f "tokens=1-4 delims=:/" %%i in ('time /t') do set TIMESTAMP=%%i.%%j.%%k

md "%userinp% on %DATESTAMP% At %TIMESTAMP%"
echo %userinp% on %DATESTAMP% At %TIMESTAMP%
CLS
EXIT

]
am new to this area of coding so please help me.
Thanks in Advance!
Posted
Updated 21-May-21 23:29pm
Comments
Karthik_Mahalingam 11-Jan-14 23:27pm    
what is your issue ??
Suaidh 11-Jan-14 23:30pm    
i want the code which can be used on the button click event ..this should basically take the current date and time along with the contents of a particular textBox and create a directory in a specific folder!
Karthik_Mahalingam 11-Jan-14 23:42pm    
check my solution
BillWoodruff 12-Jan-14 2:28am    
Just curious: is it a requirement that the folder names are as "readable" as your "sample" ?

You need a couple different pieces:

Directory.CreateDirectory[^] to create the directory.

DateTime.ToString(string)[^] to create a string for the time.

And

DateTime Custom Format Strings[^] to generate a custom format.

So your code would look something like this:

C#
public void CreateDirectory(string prefix)
{
    string dirName = prefix + " on " + DateTime.Now.ToString("ddd MM.dd.yyyy 'At' HH:mm tt");

    //Improved version of the above:
    string dirName = string.Format("{0} on {1:ddd MM.dd.yyy 'At' HH:mm tt}", prefix, DateTime.Now);

    Directory.CreateDirectory(dirName);
}


[Edit notes] The second version is preferred (delete the first line), anything you can do to avoid string concatenation is better, although the performance hit in this case is negligible. [/Edit]

Which you would call with your textbox prefix.

NOTE: I did not check to see if any of the characters are reserved, if they are you will get an exception when trying to create the directory saying it has invalid characters.
 
Share this answer
 
v3
Comments
Suaidh 11-Jan-14 23:44pm    
so how do i implant it to a button event ...? and also an exception is thrown each time its executed.BTW does this :"CreateDirectory();" work in button click event?
and how do i set it to create the folder on current application directory?
Ron Beyer 11-Jan-14 23:48pm    
You can use the contents of the function I posted in your button, just replace prefix with textbox1.Text (or whatever your textbox name is). To set the current directory you need to append the application directory to the front, like:

string dirName = string.Format("{0}\\{1} on {2:ddd MM.dd.yyy 'At' HH:mm tt}", Environment.CurrentDirectory, prefix, DateTime.Now);
Try this code..


C#
private void button1_Click(object sender, EventArgs e)
       {
           //Sample on Sun 01.12.2014 At 09.13 AM
           string locationToCreateFolder = "D://";
           string folderName = "";
           string date = DateTime.Now.ToString("ddd MM.dd.yyyy");
           string time = DateTime.Now.ToString("HH.mm tt");
           string format = "{0} on {1} At {2}";
           folderName = string.Format(format, textBox1.Text, date, time);
           Directory.CreateDirectory(locationToCreateFolder + folderName);
           MessageBox.Show("done");
       }
 
Share this answer
 
v2
Comments
Suaidh 11-Jan-14 23:49pm    
You're the BEST Thanks a bunch...you saved me a years worth of internet...
Karthik_Mahalingam 12-Jan-14 0:05am    
welcome dude :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900