First of all you need a custom action. If you need to hide a button on a list view or item view - use CommandUI.Ribbon as location. If you need to hide a button in a new form, edit form or view form - use NewFormToolbar, EditFormToolbar or DisplayFormToolbar as location. Full list of default ribbon locations here and customization locations here).
<CustomAction
Id="DMS.Lists.HideButtonCustomAction"
Location="CommandUI.Ribbon"
RegistrationType="List"
RegistrationId="1005590"
ControlAssembly="$SharePoint.Project.AssemblyFullName$"
ControlClass="DMS.Lists.HideButtonCustomActionCodeBehind">
</CustomAction>
Add a class which inherits the "Control" class. Then you handle the OnLoad event of the custom action you created:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Visible = false;
if (Microsoft.SharePoint.SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
{
SPRibbon ribbon = SPRibbon.GetCurrent(this.Page);
// check if current URL has a path of specific folder, etc.
// Try using:
// SPContext.Current.File.Url or HttpContext.Current.Request.Url
HideCustomActions(ribbon);
}
}
You hide ribbon buttons by using this code:
ribbon.TrimById(buttonToTrim);
You can get the IDs of the Ribbon buttons using IE developer tool (F12):

In a nutshell it works like this: you add a custom action to a location where you want to run some code behind. Every time you access that location (open a new form, go to list settings etc.) that custom action execute ControlClass methods (OnLoad, OnRender etc.). This is the place where you want to do all your checks and hide you buttons.
In case you want to disable the button based on conditions, you still follow these instructions and after hiding the button, you add it again, except you don't specify any command. Since no command will be associated with it - it will be grayed out.
Take a look at this pseudo code:
var web = SPContext.Current.Web;
var list = web.Lists.TryGetList("RibbonButtonList");
if (list != null)
{
var userAction = list.UserCustomActions.Add();
userAction.Location = "CommandUI.Ribbon";
userAction.ImageUrl = "/_layouts/images/image.png";
userAction.CommandUIExtension = @"<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition Location=""Ribbon.List.Settings.Controls._children"">
<Button
Id=""{5557CC45-324E-41bb-9E88-A535DBF1BF6B}""
Alt=""Programmatically Added Button""
Sequence=""5""
Command=""""
Image32by32=""/_layouts/images/menulistsettings.gif""
Image16by16=""/_layouts/images/itdcl.gif""
LabelText=""Programmatically Added Button""
TemplateAlias=""o1"" />
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
</CommandUIHandlers>
</CommandUIExtension>";
userAction.Update();
}
Hope this helps.
EnabledScript
option. You can try to identify in which folder you are currently in and disable the button. You can refer this question which details out "How to find the current folder through JS COM?" Not sure if this will work or not as I haven't tried it myself.