Overview
This is a button/script which turns lists of files and folders into real (empty) files and folders.
For example, someone may post to the forum with a list of filenames they wish to rename in a particular way, and you may then want to create files with those names to use when testing a solution. You can copy the names to the clipboard and then click this button to create files with the same names.
If there is text in the clipboard:
- The clipboard text is a list of empty files and folders to create.
- Put one name or path on each line.
- Put a \ at the end of a line if you want it to be a folder instead of a file.
(You only need to explicitly create empty folders. If you specify something below a folder that doesn't exist, its parent folder(s) will be created automatically.) - If the paths are just names or relative paths, they will be created relative to the current directory (i.e. the source folder tab).
- If the paths are absolute then they'll be created as specified, ignoring the current directory.
If there are files in the clipboard:
- Files will be created in the current directory with the same names as the ones in the clipboard.
- Nested directory structures can't be created in this case, since the script doesn't know where to take the relative paths from. Absolute paths don't make sense either, since they'd just point to the files you copied, which already exist.
In both cases, UAC is supported, so you can create things below protected folders if needed.
Installation:
- Download Paste_File_List.dcf (9.23 KB) and go to the folder you downloaded it to.
- Select Settings -> Customize Toolbars.
- Drag Paste_File_List.dcf to where you want it on your toolbars or menus
- Click OK in the Customize window.
History:
- v2.2 10/Apr/2016 - Fixed always creating files, never folders.
- v2.1 23/Aug/2015 - Fixed pasting into special paths like Desktop, and added support for aliases in the file lists.
- v2.0 11/Apr/2014 - Converted to Opus 11 script. Enhanced to work with sub-folders, absolute paths and UAC.
- v1.0 18/Dec/2013 - Original version.
The script itself:
If you just want to use the script, the Paste_File_List.dcf download above is probably easier.
The script code is reproduced here so that people looking for scripting techniques on the forum can browse the script code without having to download the .dcf file.
@script vbscript
option explicit
'
' Paste File List
' Written by Leo Davidson
' v2.2 10/Apr/2016 - Fixed always creating files, never folders.
' v2.1 23/Aug/2015 - Fixed pasting into special paths like Desktop, and added support for aliases in the file lists.
' v2.0 11/Apr/2014 - Converted to Opus 11 script. Enhanced to work with sub-folders, absolute paths and UAC.
' v1.0 18/Dec/2013 - Original version.
'
' This script lets you turn lists of files and folders into real (empty) files and folders.
'
' An example of where this is useful is when someone gives a list of files they want to rename in a
' particular way, and you want to create files with those names to use when testing your solution.
'
' If there is TEXT in the clipboard:
' The clipboard text as a list of empty files and folders to create.
' Put a name or path on each line.
' Put a \ at the end of a line if you want it to be a folder instead of a file.
' (You only need to explicitly create empty folders. If you specify something below a folder that
' doesn't exist, its parent folder(s) will be created automatically.)
' If the paths are just names or relative paths, they will be created relative to the current
' directory (i.e. the source folder tab).
' If the paths are absolute then they'll be created as specified, ignoring the current directory.
'
' If there are FILES in the clipboard:
' Files will be created in the current directory with the same names as the ones in the clipboard.
' Nested directory structures can't be created in this case, since the script doesn't know where
' to take the relative paths from. Absolute paths don't make sense either, since they already exist.
'
' In both cases, UAC is supported, so you can create things below protected folders if needed.
'
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
Function OnClick(ByRef ClickData)
Dim strClip
Dim objClipFiles
Dim strClipArray
Dim strPath
Dim x
If DOpus.GetClipFormat() = "text" Then
strClip = DOpus.GetClip("text")
strClipArray = Split(strClip, Chr(13) & Chr(10))
ElseIf DOpus.GetClipFormat() = "files" Then
' Convert the list of files into a suitable format.
' We could just call DOpus.GetClip("text") here instead, then remove the path information,
' but doing it that way would lose the knowledge of which things are dirs (the script would
' have to check using the paths) as well as the (occasionally useful) display_name handling.
Set objClipFiles = DOpus.GetClip("files")
strClipArray = Empty
For each x in objClipFiles
If x.display_name <> "" Then
strClip = x.display_name
Else
strClip = x.name
End If
If x.is_dir Then
strClip = strClip & "\"
End If
If IsEmpty(strClipArray) Then
ReDim strClipArray(0)
Else
ReDim Preserve strClipArray(UBound(strClipArray)+1)
End If
strClipArray(UBound(strClipArray)) = strClip
Next
Else
ClickData.Func.Dlg.Request "Clipboard does not contain files or text.", "OK"
Exit Function
End If
For each x in strClipArray
strClip = Replace( Trim(x), """", "") ' Remove quotes, if any
If strClip <> "" Then
If (Mid(strClip,2,1) = ":" or Left(strClip,2) = "\\") Then
strPath = strClip
Else
strPath = fs.BuildPath( ClickData.Func.SourceTab.Path, strClip )
End If
strPath = DOpus.FSUtil.Resolve(strPath)
If ((not fs.FileExists(strPath)) and (not fs.FolderExists(strPath))) Then
' Creating the files and folders using Opus commands, rather than the fs object, means we gain
' UAC support, work better with Opus's File and Undo logs, and easier recursive directory creation.
If Right(strClip,1) = "\" Then
ClickData.Func.Command.RunCommand "CreateFolder READAUTO=no NOSEL NAME=""" & strPath & """"
Else
If (not fs.FolderExists(fs.GetParentFolderName(strPath))) Then
ClickData.Func.Command.RunCommand "CreateFolder READAUTO=no NOSEL NAME=""" & fs.GetParentFolderName(strPath) & """"
End If
'
' We use a trick here, adding a . to the end of the filename (which will not become part of the resultant name), to ensure
' that if the filename has no extension it does not get a .TXT extension added by the 'FileType NEW=.txt' command.
'
' TODO: Instead of creating empty files using the new .TXT handler, we could use the appropriate handler for the real
' extension, when there is one. The problem is that if there isn't a handler then the command will fail, so that
' might need a way to either detect which new-file handlers are available or to tell the command to fallback on the
' .TXT handler when the specified one is missing. (Or, better, fallback on creating an empty file, since it's possible
' for the .TXT handler itself to be missing on systems where the registry/filetypes have been messed up.)
'
ClickData.Func.Command.RunCommand "FileType NEW=.txt NEWNAME=""norename:" & fs.GetFileName(strPath) & "."" PATH=""" & fs.GetParentFolderName(strPath) & """"
End If
End If
End If
Next
End Function