Is it possible to create a command that would create a folder and multiple children files with the same name and different extensions?
For example I would open the command and type in a name of "desktop-header"
the result would be the following being created
desktop-header (folder)
desktop-header.js
desktop-header.php
_desktop-header.sass
I've been following BEM and end up needing to create all the blocks manually.
I'm not even sure what to search to see if there is an answer to this.
thanks!
Yes, you can write arbitrary python code in the a command. Just select Tools > Developer > New Plugin... and paste the following code. This will create a folder, which contains the 3 files, relatively to the current view.
import os
import sublime
import sublime_plugin
class CreateBemFilesCommand(sublime_plugin.WindowCommand):
def run(self):
window = self.window
view_path = window.active_view().file_name()
if not view_path:
sublime.error_message("Save your file first.")
return
base_folder_path, _ = os.path.split(view_path)
def on_done(name):
folder_path = os.path.join(base_folder_path, name)
# add your file names
file_names = [
name + ".js",
name + ".php",
"_" + name + ".sass"
]
file_paths = [
os.path.join(folder_path, file_name)
for file_name in file_names
]
# create the folder
os.makedirs(folder_path, exist_ok=True)
# create the files
for file_path in file_paths:
with open(file_path, "a"):
pass
# open the files in Sublime Text
window.open_file(file_path)
window.show_input_panel(
"Input the folder name", "", on_done, None, None)
afterwards create a keybinding like this:
{
"keys": ["alt+shift+b"],
"command": "create_bem_files",
},
Related
In the webstorm I can do this using live-template using groovy to create custom template.
eg:
capitalize(camelCase(groovyScript( "def splitList = _1.split('/');def size = splitList.size();if (size >= 2) {for (int i = 0; i < size-2; i++) {splitList = Arrays.copyOfRange(splitList, 1, splitList.length);}};return splitList.join('_');", fileRelativePath() )));
This custom variable will create a string which is based on current directory and current filename and extentions.
If a directory like this
root
|-app
|-components
|MyComponent.tsx
|-server
MyComponent.tsx use this template will create a string which value is ComponentsMyComponentTsx
The reason create this string is i can command+o quick open by name which is found in the react-dev-tool.
I just copy the name and open de editor, command+o open the file. soconvenient
I want to know how can i do this in vscode
I am trying to find the file type in order to read the file based on its type.
Input come in different file formats such as CVS, excel and orc etc..,
for example input =>"D:\\resources\\core_dataset.csv"
I am expecting output => csv
You could achieve this as follows:
import java.nio.file.Paths
val path = "/home/gmc/exists.csv"
val fileName = Paths.get(path).getFileName // Convert the path string to a Path object and get the "base name" from that path.
val extension = fileName.toString.split("\\.").last // Split the "base name" on a . and take the last element - which is the extension.
// The above produces:
extension: String = csv
I'm trying to read some files from my Scala project, and if I use: java.io.File(".").getCanonicalPath() I find that my current directory is far away from them (exactly where I have installed Scala Eclipse). So how can I change the current directory to the root of my project, or get the path to my project? I really don't want to have an absolute path to my input files.
val PATH = raw"E:\lang\scala\progfun\src\examples\"
def printFileContents(filename: String) {
try {
println("\n" + PATH + filename)
io.Source.fromFile(PATH + filename).getLines.foreach(println)
} catch {
case _:Throwable => println("filename " + filename + " not found")
}
}
val filenames = List("random.txt", "a.txt", "b.txt", "c.txt")
filenames foreach printFileContents
Add your files to src/main/resources/<packageName> where <packageName> is your class package.
Change the line val PATH = getClass.getResource("").getPath
new File(".").getCanonicalPath
will give you the base-path you need
Another workaround is to put the path you need in an user environmental variable, and call it with sys.env (returns exception if failure) or System.getenv (returns null if failure), for example val PATH = sys.env("ScalaProjectPath") but the problem is that if you move the project you have to update the variable, which I didn't want.
I have the below code which allows the user to drag and drop a folder to get the folder path. I then take this folder path and use it to pass through to a command line application in Windows using Popen. This all works fine except if there are spaces in the folder path, which then fails. I am currently getting round this by using win32api.GetShortPathName(folder_list) which shortens them to DOS 8.3 spec, but I would like to use the full absolute path. I know the command line application takes paths with spaces as I also use a batch file with drag and drop which works with spaces in paths. I have tried inserting escapes, etc, still no luck. How can I get this to work properly with full folder paths with spaces?
class SubmissionPane(wx.Panel):
def __init__(self, parent, queue_control):
wx.Panel.__init__(self, parent, -1)
self.parent = parent
self.queue_control = queue_control
self.selected_folder = None
self.txtTitle = wx.TextCtrl(self, pos=(125, 70), size=(215, 25), style= wx.SUNKEN_BORDER, value="Enter Series Title Here")
self.txtTitle.Show(False)
self.drop_target = MyFileDropTarget(self)
self.SetDropTarget(self.drop_target)
def SetSubmissionFolders(self, folder_list):
"""Called by the FileDropTarget when files are dropped"""
print "Setting submission folders", folder_list
self.tc_files.SetValue(','.join(folder_list))
self.selected_folders = folder_list
class MyFileDropTarget(wx.FileDropTarget):
""""""
def __init__(self, window):
wx.FileDropTarget.__init__(self)
print "Creating a drop file target..."
self.window = window
def OnDropFiles(self, x, y, filenames):
self.window.SetSubmissionFolders(filenames)
I then submit this to Popen like this:
command1 = commandLineApplication + folder_list
process = Popen(command1, shell=True, stdin=PIPE)
You probably just need to put quotes around each of the file paths. That usually works. The win32api.GetShortPathName is a neat trick though.
Here's one way to do it:
n = ['"%s"' % x for x in folderlist]
Then do the
','.join(folder_list)
You mentioned in your code.
I have some .txt output files that I would like to import in excel.
Ideally I want to use Sheet1 as an index; where I will write a title, the name of the sheet and the path and name for the .txt file. The Macro should create a newSheet named as the cell and in that sheet import the corresponding .txt file.
I been trying to record a macros and to changing the VBA code, but so far just failures.
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Documents and Settings\UserXP\My Documents\My Dropbox\CIPPEC\1-MIRA\Procesamiento base\OEDE\Tablas\trim\q_employ_pcia_23.txt" _
, Destination:=Range("$A$1")).Name = "q_employ_pcia_23"
Any idea?
Thanks
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Documents and Settings\UserXP\My Documents\My Dropbox\CIPPEC\1-MIRA\Procesamiento base\OEDE\Tablas\trim\q_employ_pcia_23.txt" _
, Destination:=Range("$A$1"))
.Name = "q_employ_pcia_23"
.Refresh BackgroundQuery:=False
End With
ActiveSheet.Name = "q_employ_pcia_23"
works to get the import into a new sheet and you want to name the new sheet also.