GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Paste empty file list

scripting
button
vbscript

#1

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

Add file name list to collection from clipboard
Add cover art to selection
Create folder or empty file with name from clipboard
Find Duplicates but only for partial file names (command)
Is it possible to copy only file names, not contents?
Can't figure out how to make a couple buttons for SelectEx & ClipboardEx
Create new 'empty' versions of files
#2

Updated script in the root post with the following change:

  • v2.1 23/Aug/2015 - Fixed pasting into special paths like Desktop, and added support for aliases in the file lists.

#3

I’ve just installed this script button on DO12.0.4 to use it for creating a set of default folders.

I created a text file with the following contents:

Documents\ Downloads\ Music\ OneDrive\ Pictures\ Videos\

I then copied the contents of the file to the clipboard. When I press the ‘Paste File List’ button, I get files with the names above and not folders.

I tried using ‘/’ instead of ‘’ at the end of the line but that made no difference - every time, files are created and not the desired folders.

I even tried using a different text editor, NotePad (I normally use Edit+) but that made no difference either.

Am I doing something wrong or is the script up to no good?


#4

Never mind the above (although I would be interested in why it didn’t work).

I have used the native CreateFolder command with the NAME option which works fine with a list of required folders.


#5

You don’t even need to do that. Just click the New Folder button and there is a UI for it:



#6

Script updated in root post:

  • v2.2 10/Apr/2016 - Fixed always creating files, never folders

(Changed Right(strPath,1) to Right(strClip,1).)


#7

Thanks for the reply Leo.

I know I could have used the New Folder Button and filled in the required folders but I wanted something that did it all with a single click as in this case, it will always be the same set of folders. Whenever I do a clean install of Windows, I move the folders listed from the C:\ drive to the D:\ drive and it saves time if those folders already exist in the destination location.

I see you have updated your script to fix the lack of folder creation. I’ll take another look at that as well.

Thanks again.


#8

How can I copy the current selected folder recursively to the clipboard so that I can use this script? in one button?

Currently copying the name only copies only for the selected item not all the items below it. So it doesn’t recreate the entire structure.


#9

You probably want this instead (Recursive Version is 2nd in the 1st post):