PDA

View Full Version : [SOLVED:] Move Files to SubFolders



Jill
08-18-2017, 07:39 AM
I've exhausted my efforts to solve this on my own. I'm fairly new to scripting and I'm hoping someone with more experience can point me in the right direction.

I have a script that creates thousands of images in one folder. At the end of the script I'd like to sort the images into subfolders. The script successfully creates the subfolder and I can move the file if i use the exact path. But I need something that will work on variable filenames.

When i save the file i use this script with success...
"-SaveRenderWindowAs ""C:\Rendermation\renders\" & folderIdx.name & "_AccentGem" & ".png" & """"
The folderIdx.name successfully uses a name that changes per file.

When I move the file into its subfolder...
This script works fine (defining the exact path)
Set fs = CreateObject("Scripting.FileSystemObject")
fs.MoveFile "C:\Rendermation\Renders\perspective_Ring_Solitaire_Marquise_6X3mm.3dm.png", "C:\Rendermation\Renders\Ring_Solitaire_Marquise_6X3mm.3dm\"


However, if i replace this line, it fails...

fs.MoveFile "C:\Rendermation\Renders\perspective_" & folderIdx.name, "C:\Rendermation\Renders\" & folderIdx.name & "\"


This is the error.
"Microsoft VBScript runtime error"
"Object required:'folderIdx'"


Any ideas?

Jill

Bob Phillips
08-18-2017, 08:51 AM
Surely, folderIdx.name refers to perspective not to Ring_Solitaire_Marquise_6X3mm.3dm.png

Jill
08-18-2017, 10:09 AM
folderIdx.name is this portion
Ring_Solitaire_Marquise_6X3mm.3dm

I add the perspective when i save the file.

This is the portion that creates the folder where I want to move the files.

For Each folderIdx In files

'create folder to save the renders
If Not folderexists(strFolder & "\renders\" & folderidx.name) Then
mkdir strFolder & "\renders\" & folderidx.name
End If

Next


Im attaching a screencap of the directory from which I want to move the files. You can see the folder that has been created.

Jill
08-18-2017, 10:14 AM
I did notice i was missing the .png from the source.

Set fs = CreateObject("Scripting.FileSystemObject")


fs.MoveFile "C:\Rendermation\Renders\perspective_" & folderIdx.name & ".png", "C:\Rendermation\Renders\" & folderIdx.name & "\"

But i still get the same error...

"Microsoft VBScript runtime error"
"Object required:'folderIdx'"

Jill
08-22-2017, 10:25 AM
I'm still spinning my wheels.

I will try to explain the issue again in detail.

I have a script that runs
For Each folderIdx In files

These files are located in the following directory
C:\Rendermation\


At first there is only one file Ill call "Solitaire"

A subfolder with this name is created programatically
C:\Rendermation\render\solitaire
This subfolder is created by using the following code

For Each folderIdx In files

If Not folderexists(strFolder & "\renders\" & folderidx.name) Then
mkdir strFolder & "\renders\" & folderidx.name
End If


Next




The script then opens all files in that initial directory (C:\Rendermation\) and saves them with a prefix.
bluestone_solitaire
perspective_solitaire
side_solitaire
solitaire (this is my original file)
throughfinger_solitaire
topangle_solitaire

The script then creates images based on that prefix that are saved in a subfolder
C:\Rendermation\renders

At the end of this process there are 18 files for each prefix. So if i have one original file (Solitaire) then the result is 108 image files. However if I drop 5 original files then I get 30 prefix files and 540 images.

So i added code to sort through the files. And I finally got it to work on some of the files.

This is example of one of the blocks of code for one of the prefixes.

If fileexists(strFolder & "\renders\bluestone_" & folderIdx.name & ".png") Then

fs.MoveFile "C:\Rendermation\Renders\bluestone_" & folderIdx.name & ".png", "C:\Rendermation\Renders\" & folderIdx.name & "\"

If fileexists(strFolder & "\renders\bluestone_" & folderIdx.name & "_Metal1.png") Then
fs.MoveFile "C:\Rendermation\Renders\bluestone_" & folderIdx.name & "_Metal1.png", "C:\Rendermation\Renders\" & folderIdx.name & "\"
End If

If fileexists(strFolder & "\renders\bluestone_" & folderIdx.name & "_Metal2.png") Then
fs.MoveFile "C:\Rendermation\Renders\bluestone_" & folderIdx.name & "_Metal2.png", "C:\Rendermation\Renders\" & folderIdx.name & "\"
End If

If fileexists(strFolder & "\renders\bluestone_" & folderIdx.name & "_CenterGem.png") Then
fs.MoveFile "C:\Rendermation\Renders\bluestone_" & folderIdx.name & "_CenterGem.png", "C:\Rendermation\Renders\" & folderIdx.name & "\"
End If

If fileexists(strFolder & "\renders\bluestone_" & folderIdx.name & "_SideGem.png") Then
fs.MoveFile "C:\Rendermation\Renders\bluestone_" & folderIdx.name & "_SideGem.png", "C:\Rendermation\Renders\" & folderIdx.name & "\"
End If

If fileexists(strFolder & "\renders\bluestone_" & folderIdx.name & "_AccentGem.png") Then
fs.MoveFile "C:\Rendermation\Renders\bluestone_" & folderIdx.name & "_AccentGem.png", "C:\Rendermation\Renders\" & folderIdx.name & "\"
End If

End If



The problem .... this code does not work until the script creates the images for the original file (Solitaire) in the list below. So in theis example when it completes Solitaire, it successfully moves all the files into their respective subfolder
C:\Rendermation\render\solitaire

However any prefix files that fall alphabetically after Solitaire never get sorted.


bluestone_solitaire
perspective_solitaire
side_solitaire
solitaire (this is my original file)
throughfinger_solitaire doesnt get sorted
topangle_solitaire doesnt get sorted


I believe the problem is the folderidx.name doesnt initiate until after creating the images for that file. I have tried so many things to solve this and I am totally lost.

My approach to do this as one script may be all wrong. I've also tried to separate it into two scripts, one that creates the images and one that sorts the files, but I have not been able to figure out how to create something that sorts the files based on the name of the subfolder within the directory. I'm including a screenshot of the directory in case anyone has ideas.

Thanks in advance to anyone who has ideas.
Jill

Jill
08-24-2017, 07:38 AM
This was solved by mdmackillop (the genius!) in the thread below. Thanks so much for all the help.

Jill

http://www.vbaexpress.com/forum/showthread.php?60484-Move-File-to-Folder-if-filename-matches-foldername