Apple Script: when moving a file into a folder, is it possible to move duplicates inside that folder? - directory

I have an apple script which moves all files within a folder into a new folder according to the first 6 characters of the filenames.
If the folder already exists, the files get moved into that existing folder instead.
If the file already exists in the new/existing folder, the file to be moved into the folder gets a "copy" extension to the filename – which is not in the script, it's a system function ;-)
My question:
Is it possible to move all existing (duplicate) files into a subfolder (named "_alt" for example), before the other files get moved within the folder (to prevent the "copy"-extension)? I would like to keep both files, the original files and the newly moved files.
Thanks.
This is my existing script:
set mgFilesFolder to (choose folder with prompt "Choose folder…")
(* get the files of the mgFilesFolder folder *)
tell application "Finder" to set fileList to files of mgFilesFolder
(* iterate over every file *)
repeat with i from 1 to number of items in fileList
set this_item to item i of fileList
set this_file_Name to name of this_item as string
set thisFileCode to characters 1 thru 6 of this_file_Name as text
log thisFileCode
tell application "Finder"
try
set nf to make new folder at mgFilesFolder with properties {name:thisFileCode}
end try
end tell
end repeat
set sourceFolder to mgFilesFolder
set destinationFolder to mgFilesFolder
tell application "Finder"
set the_files to (get files of mgFilesFolder)
repeat with this_file in the_files
set the_name to name of this_file
set the_short_name to items 1 thru 6 of the_name as string
if exists folder the_short_name of mgFilesFolder then move this_file to folder the_short_name of mgFilesFolder
end repeat
end tell

Related

how to write a function dir_info(directory) which returns 3 values

(1) Number of files inside the directory (recursively)
(2) Number of directories inside the directory (recursively)
def dir_info(directory):
nfiles = how many files inside directory?
ndirs = how many sub directories inside directory?
return nfiles, ndirs

Moving files between folders when folder names match partially (in R or VBA)

I'm trying to solve the following problem
I have 9 folders titled PROS_2010 to PROS_2019. Each of them has about 500 subfolders with names structured as follows e.g. PROS_201001211_FIRM NAME_number. Each subfolder has a variety of pdf files with different names.
I have created in VBA another folder called sample with about 400 subfolders, each of which is named a specific FIRM NAME. For this I used the following code:
Sub MakeFolders()
Dim Rng As Range
Dim maxRows, maxCols, r, c As Integer
Set Rng = Selection
maxRows = Rng.Rows.Count
maxCols = Rng.Columns.Count
For c = 1 To maxCols
r = 1
Do While r <= maxRows
If Len(Dir(ActiveWorkbook.Path & "\" & Rng(r, c), vbDirectory)) = 0 Then
MkDir (ActiveWorkbook.Path & "\" & Rng(r, c))
On Error Resume Next
End If
r = r + 1
Loop
Next c
End Sub
I now want to move all the pdf files that are in the original subfolders PROS_201001211_FIRM NAME_number to the folders titled FIRM NAME only.
Basically, each original subfolder contains a report about a firm for a specific year (2010 to 2019) and I want to get all the firm reports for all years in a single folder titled FIRM NAME
To make it easier I already have an excel file that basically has the complete list of subfolders that looks like this:
Data structure: Company name is the name of the folder in which I want to move the files that are currently in "attachment folder". attachment1 is the pdf file name (which always changes so ideally the code would pluck all the files in attachment folder and move them to the file with company name
Thanks in advance,
Simon
OK
So thanks to the help of a mate I found it is super easy to solve this problem using the "command" command in windows
Basically create a text file (in notepad) that has the following structure
move "original pdf file directory" "new pdf file location\"
...
Repeat the structure for each file (which requires some basic excel string manipulations)
Then save the .txt file as a .cmd file and open it.
Done

premake5 add generated files to vstudio project

I have overridden the onProject function for the vs2012 action which generates some cpp files and then tries to include them in the project
--cant override the generateProject directly
--so have to override at the action level
premake.override( premake.action._list.vs2012, 'onProject', function(base, prj)
if premake.project.iscpp(prj) then
--generate files
--print( "Generating extra files ...")
local extraFiles = mine.getExtraFiles(prj)
for _,file in ipairs( extraFiles ) do
p.generate( file, nil, mine.generateExtraFile )
mine.addFileToSources(file)
end
end
--Generate regular stuff
base(prj)
end)
function mine.getExtraFiles(prj)
local extraFiles = {}
--works out what files to generate and add relevant info to table
return extraFiles
end
--this function is passed as a callback to premake.generate
function mine.generateExtraFile(extraFile)
--write contents of file
end
This is the function that attempts to add each generated file to the project
function mine.addFileToSources(extraFile)
local prj = extraFile.prj
local cfg = extraFile.cfg
local groups = premake.vstudio.vc2010.categorizeSources(prj)
local compiledFiles = groups.ClCompile or {}
--create a new file config for generated file
local filename = path.join(extraFile.location, extraFile.filename)
local fcfg = premake.fileconfig.new( filename, prj)
premake.fileconfig.addconfig(fcfg, cfg)
--add the config to the project's sources
table.insert(compiledFiles, fcfg)
compiledFiles[filename] = fcfg
--add to the projects source tree
--this bit is copied from premake.project.getsourcetree
-- The tree represents the logical source code tree to be displayed
-- in the IDE, not the physical organization of the file system. So
-- virtual paths are used when adding nodes.
-- If the project script specifies a virtual path for a file, disable
-- the logic that could trim out empty root nodes from that path. If
-- the script writer wants an empty root node they should get it.
local flags
if fcfg.vpath ~= fcfg.relpath then
flags = { trim = false }
end
-- Virtual paths can overlap, potentially putting files with the same
-- name in the same folder, even though they have different paths on
-- the underlying filesystem. The tree.add() call won't overwrite
-- existing nodes, so provide the extra logic here. Start by getting
-- the parent folder node, creating it if necessary.
local tr = premake.project.getsourcetree(prj)
local parent = premake.tree.add(tr, path.getdirectory(fcfg.vpath), flags)
local node = premake.tree.insert(parent, premake.tree.new(path.getname(fcfg.vpath)))
-- Pass through value fetches to the file configuration
setmetatable(node, { __index = fcfg })
end
For the most part - this all works:
The files are generated correctly and to correct location
The files are also included in the vcxproj file correctly
My problem is that the vcxproj.filters file is not being generated.
When I run premake I get this error:
Generating myproject.vcxproj.filters...Error: [string "src/actions/vstudio/vs2010_vcxproj_filters...."]:82: attempt to index field 'parent' (a nil value)
which corresponds to the function premake.vstudio.vc2010.filterGroup(prj, groups, group)
I get that the new fcfg I created needs to have a parent but I can't work out where or what I should be adding it to.
Can anyone help?
EDIT 1
I've got things working by adding this line to the end of function mine.addFileToSources(extraFile)
fcfg.parent = parent
This gives the file config a parent node so everything works out but I feel kinda dirty doing this so I'll look at following Citron's advice
EDIT 2
Overriding the bakefiles was much cleaner and neater. It wasn't as straightforward as Citron's code since I needed the information from the baked files in order to carry out my file generation but I am now confident that my code is correct and will possibly work with other exporters than vstudio too.
Here's my new code:
premake.override( premake.oven, 'bakeFiles', function(base, prj)
--bake the files as normal
local bakedFiles = base(prj)
if premake.project.iscpp(prj) then
--gather information about what files to generate and how
local extraFiles = mine.getExtraFiles(prj, bakedFiles)
for _,file in ipairs( extraFiles ) do
--do the generation
premake.generate( file, file.extension, mine.generateExtraFile )
--add the new files
local filename = premake.filename(file, file.extension)
table.insert(file.cfg.files, filename)
-- This should be the first time I've seen this file, start a new
-- file configuration for it. Track both by key for quick lookups
-- and indexed for ordered iteration.
assert( bakedFiles[filename] == nil )
local fcfg = premake.fileconfig.new(filename, file.prj)
bakedFiles[filename] = fcfg
table.insert(bakedFiles, fcfg)
premake.fileconfig.addconfig( bakedFiles[filename], file.cfg)
end
--sort the baked files again - since we have added to them
table.sort(bakedFiles, function(a,b)
return a.vpath < b.vpath
end)
end
return bakedFiles
end)
I don't know what the problem is with your code (a bit too much to read, and not enough time :p) but if you just want to add some generated files to your project tree, I would advise you to override premake.oven.bakeFiles instead.
This is what I used to add files generated by Qt in my addon. See premake.extensions.qt.customBakeFiles on https://github.com/dcourtois/premake-qt/blob/master/qt.lua
Basically in the bakeFiles override, you can just browse your projects, and insert files in the list easily. Then, if those added files need some custom configuration, you can then override premake.fileconfig.addconfig. See premake.extensions.qt.customAddFileConfig in the aforementioned addon.
In this addconfig override, you'll have access to the files and you will be able to modify their configuration object: you can add custom build rules, special options, etc.
It's not a direct answer to your specific question, but I hope it will help you achieve what you need.

Finding Test set and Sub folder from a parent folder in QC Test Lab

I am trying to extract sub folders (not all children folders) along with Test sets(not all children test sets, only next test set) from a parent folder in QC test Lab.
Let suppose There are 2 sub folders and 1 test set in a Parent folder in QC:
-Parent Folder1
- Sub folder1
- Sub folder2
- Test Set
Using the below code i am able to extract the subfolders only not test sets:
Code:
Set TsetMgr = UserForm9.QCConnection.TestSetTreeManager 'object required
Set Root1p = TsetMgr.Root 'connect to Root
Set SubNodesRoot = Root1p.SubNodes() 'Using SubNodes method to extract SubNodes from QC
For rp = 1 To SubNodesRoot.Count 'Total SubNodes available
c1p = SubNodesRoot.Item(rp)' Finding SubNodes
MsgBox(c1p) ' SubFolder1 and Subfolder2 values are displayed.
Next
My only problem now is, i am not able to extract Test Sets along with subfolders from a parent folder.
Can anyone please help me fix this. Thanks.
I don't think there is a way to get both folders and test sets with calling one function. But you can get the folders with the method you described. And you can get the immediate child-test-sets of a TestSetFolder via the TestSetFactory of this folder:
test_set_factory = lab_folder.TestSetFactory
test_sets = test_set_factory.NewList("")
test_sets.each do |test_set|
puts "Test Set: #{test_set.Name}"
end
That should print you the names of all TestSets in the lab_folder. I think you cannot have TestSets directly in your root folder, so you can use the TestSetFactory only on sub folders.

Extracting File Extensions from AppleScript Paths

I'm writing an Apple Script that ultimately will mark commercials for all iTunes exports from EyeTV. But I'm running into a simple problem with AppleScript paths, which the EyeTV app returns as recording locations. Here's the context:
set recordingID to 370404006
set myid to recordingID as integer
tell application "EyeTV"
set eyetvr_file to get the location of recording id myid as alias
end tell
return eyetvr_file
alias "Macintosh HD2:Documents:EyeTV Archive:South Park.eyetv:000000001613eaa6.eyetvr"
Now I need to extract the containing path and the file prefix 000000001613eaa6 (using this question) so I can look for the corresponding commercial markings in the file 000000001613eaa6.edl. Here's the hiccup:
tell application "Finder"
set eyetv_path to container of eyetvr_file
set root_name to name of eyetvr_file
set fext to name extension of eyetvr_file
end tell
The results are,
eyetv_path: document file "South Park.eyetv" of folder "EyeTV Archive" of folder "Documents" of disk "Macintosh HD2" of application "Finder"
root_name: "000000001613eaa6.eyetvr"
fext: ""
fext should be ".eyetvr", not the empty string. How do I correctly extract ".eyetvr" from either eyetvr_file or root_name? I've tried a bunch of hack like
set fext to name extension of (eyetvr_file as document)
But this gives error like,
error "Can’t make alias \"Macintosh HD2:Documents:EyeTV Archive:South Park.eyetv:000000001613eaa6.eyetvr\" into type document." number -1700 from alias "Macintosh HD2:Documents:EyeTV Archive:South Park.eyetv:000000001613eaa6.eyetvr" to document
Finder doesn't recognize all filename extensions. You could just use text item delimiters to remove the last part though.
do shell script "touch /tmp/some.file.eyetvr"
set text item delimiters to "."
tell application "Finder"
set n to name of file (POSIX file "/tmp/some.file.eyetvr")
if n contains "." then set n to (text items 1 thru -2 of n) as text
n -- some.file
end tell
Mini-comments apparently do not allow posting code, so here are my edits to Lauri Ranta's nice solution:
do shell script "touch /tmp/some.file.eyetvr"
set delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
tell application "Finder"
set n to name of file (POSIX file "/tmp/some.file.eyetvr")
if n contains "." then set n to (text items 1 thru -2 of n) as text
n -- some.file
end tell
set AppleScript's text item delimiters to delims
I'll post a link to the EyeTV-commercial-marking-script-for-iOS when I've finished that project.

Resources