hexagon logo

Create folder script

It used to be here.
  • Ah. Forget the important basics. I apologize the item I posted was not a full script. Just a section to demonstrate that you could do it.

    Sub Main()
    Dim objFSO, objShell
    Dim PCDApp, PCDPartProgram, PCDCommands, PCDCommand, retval
    Dim objFile, objFolder As Object
    
    'Assign PCDMIS Commands, And pulls In the variables
    Set PCDApp               = CreateObject("PCDLRN.Application")
    Set PCDPartProgram = PCDApp.ActivePartProgram
    Set PCDCommands    = PCDPartProgram.Commands
    


    ^ Needs to be at the top of your script.

      strDirectory  = PCDPartProgram.Path & "\" & "Job Log" 
    strFileName = joname.StringValue 
    [B]strPath[/B]         = strDirectory & "/" & strFileName & ".txt"
    


    ^ This is what I use for the my script If you want to build off it. StrPath is the directory you wish to make your folder in. In my example I used multiple variables from my program. You will have to chop it up to make it work.

    TO_FILE=ON,APPEND="[B]Z:\Everyone\ChadJ\2014programs\\"+J1.INPUT+"\JOB# "+J1.INPUT+" TIME "+TIME.PDF[/B]
    


    ^If this is your directory, you could use an assignment in its place and pull the directory in.

    If objFSO.FileExists(strPath) Then
      Set objFile   = objFSO.OpenTextFile(strPath)
    Else
      Set objFile   = objFSO.CreateTextFile(strPath)
        Set objFile = objFSO.OpenTextFile(strPath)
    End If
    
    


    ^Like I said this is an example using a text file creation. You will have to construct one to make a folder.

    It's more than just copy and paste, you will have to tweak it to fit your purposes.
  • Yeah, I haven't programed since the 80's and it was Basic and Dbase, so I'm pretty green with it. I'll play with it some today and let you know. Thanks for the help.
  • Ok, I think I'm almost there.

    ASSIGN/OOT=GETPROGRAMINFO("NUMOOT")
                ASSIGN/TIME=GETPROGRAMINFO("TIME")
    J1         =COMMENT/INPUT,YES,FULL SCREEN=NO,
                'Job Number:'
                ASSIGN/JOB=J1.INPUT
                ASSIGN/PATH="Z:\Everyone\ChadJ\2014programs\\"+J1.INPUT+"\JOB# "+J1.INPUT+" TIME "+TIME.PDF
                ASSIGN/PATH2="Z:\Everyone\ChadJ\2014programs\\"+J1.INPUT
                IF/OOT==0
    CS1          =SCRIPT/FILENAME= C:\USERS\CJACQUAY\DESKTOP\MAKEFOLDER3.BAS
                  FUNCTION/Main,SHOW=YES,,
                  STARTSCRIPT/
                  ENDSCRIPT/
                  PRINT/REPORT,EXEC MODE=END,$
                    TO_FILE=ON,APPEND=PATH,AUTO OPEN REPORT=OFF,$
                    TO_PRINTER=OFF,COPIES=1,$
                    TO_DMIS_REPORT=OFF,FILE_OPTION=INDEX,FILENAME=,$
                    REPORT_THEORETICALS=NONE,REPORT_FEATURE_WITH_DIMENSIONS=NO,$
                    TO_EXCEL_OUTPUT=OFF,
                    PREVIOUS_RUNS=DELETE_INSTANCES
                END_IF/
    


    Sub Main()
    
    Dim objFSO, objShell
    Dim PCDApp, PCDPartProgram, PCDCommands, PCDCommand, retval
    Dim objFile, objFolder As Object
    
    Set PCDApp=CreateObject("PCDLRN.Application")
    Set PCDPartProgram=PCDApp.ActivePartProgram
    Set PCDCommands=PCDPartProgram.Commands
    
    Set Jobnumber=PCDPartProgram.GetVariableValue("JOB")
    Set Pathname=PCDPartProgram.GetVariableValue("PATH2")
    
    strFolderName=Jobnumber.StringValue
    strPath=Pathname.StringValue
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    If objFSO.FileExists(strPath) Then
      'Set objFile=objFSO.OpenFolder(strPath)
    Else
      Set objFile=objFSO.CreateFolder(strPath)
      'Set objFile=objFSO.OpenFolder(strPath)
    End If
    
    End Sub
    


    I commented out the OpenFolder lines because I'm not sure that's what should be there. When I run the program it works perfect if there wasn't already a folder there, but if there is it errors out on line 22. strFolderName is probably not needed.
  • Nevermind, I figured it out.

    Sub Main()
    
    Dim objFSO, objShell
    Dim PCDApp, PCDPartProgram, PCDCommands, PCDCommand, retval
    Dim objFile, objFolder As Object
    
    Set PCDApp=CreateObject("PCDLRN.Application")
    Set PCDPartProgram=PCDApp.ActivePartProgram
    Set PCDCommands=PCDPartProgram.Commands
    
    Set Pathname=PCDPartProgram.GetVariableValue("PATH2")
    
    strPath=Pathname.StringValue
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    If objFSO.FolderExists(strPath) Then
      'Nothing
    Else
      Set objFile=objFSO.CreateFolder(strPath)
    End If
    
    End Sub
    
  • That looks about right. Good job.

    Thank you for posting your solution.
  • First off, my compliments to chadjac... when it comes to Basic scripting, I'm not worthy to kiss your shoes. That being said...

    I "borrowed" this (by which I mean I shamelessly cut and pasted it) and it worked BEAUTIFULLY. I've been beating my head against the wall and taking pretty much any suggestions I could get in regard to another post utilizing another method that uses a much more complicated method to do what this script does, to no avail. I copied this in, adapted it to my file paths, and ran it, and it did exactly what I wanted it to do. But then I got greedy... I inserted the exact same script into a different program, tried to run it, and got a "OLE Automation method exception" on Line 20, which is the "Set objFile=objFSO.CreateFolder (strPath).
    Then I went back to the program that I had originally used it in where it had worked perfectly, and got the same error message.

    WTF?
  • Are you sure you copied it exactly, normally i get that message when i try to create something that already exists or did not dim everything out correctly at the beginning.
    Also Chadjac made use of a script posted by vpt.se, I would thank him.

  • This was from Chadjac's post-

    Sub Main()
    
    Dim objFSO, objShell
    Dim PCDApp, PCDPartProgram, PCDCommands, PCDCommand, retval
    Dim objFile, objFolder As Object
    
    Set PCDApp=CreateObject("PCDLRN.Application")
    Set PCDPartProgram=PCDApp.ActivePartProgram
    Set PCDCommands=PCDPartProgram.Commands
    
    Set Pathname=PCDPartProgram.GetVariableValue("PATH2")
    
    strPath=Pathname.StringValue
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    If objFSO.FolderExists(strPath) Then
      'Nothing
    Else
      Set objFile=objFSO.CreateFolder(strPath)
    End If
    
    End Sub
    


    and this is my script
    Sub Main()
    
    Dim objFSO, objShell
    Dim PCDApp, PCDPartProgram, PCDCommands, PCDCommand, retval
    Dim objFile, objFolder As Object
    
    Set PCDApp=CreateObject("PCDLRN.Application")
    Set PCDPartProgram=PCDApp.ActivePartProgram
    Set PCDCommands=PCDPartProgram.Commands
    
    Set Pathname=PCDPartProgram.GetVariableValue("PATH2")
    
    strPath=Pathname.StringValue
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    If objFSO.FolderExists(strPath) Then
      'Nothing
    Else
      Set objFile=objFSO.CreateFolder(strPath)
    End If
    
    End Sub
    


    If there's a difference, I'm not seeing it...

    This is the code from my PCDMIS program-

                ASSIGN/PATH="Q:\\Quality\\In Process Inspection Plans\\11127-MACHINE\\CMM REPORTS\\"+JN.INPUT+"\ "+PROG_NAME+"_"+V2+".PDF"
                ASSIGN/PATH2="Q:\\Quality\\In Process Inspection Plans\\11127-MACHINE\\CMM REPORTS\\"+JN.INPUT
    PRINT      =LABEL/
                PRINT/REPORT,EXEC MODE=END,$
                  TO_FILE=ON,APPEND=PATH,AUTO OPEN REPORT=OFF,$
                  TO_PRINTER=OFF,COPIES=1,$
                  TO_DMIS_REPORT=OFF,FILE_OPTION=INDEX,FILENAME=,$
                  REPORT_THEORETICALS=NONE,REPORT_FEATURE_WITH_DIMENSIONS=NO,$
                  TO_EXCEL_OUTPUT=OFF,
                  PREVIOUS_RUNS=DELETE_INSTANCES
    


    It worked perfectly the first few times I ran it. It created the folder from the Job number and put the report in the folder exactly as I was needing- I had to add a refresh script because the report it was saving merely had the report header and nothing else, but the refresh script resolved that. It wasn't until I closed the first program out and opened another program and added the script to the second program that it suddenly refused to work. And then when I went back to the first program and tried to run it again, suddenly I started getting the same error message. And I changed absolutely NOTHING other than the program it was running in.
  • What exactly is device Q:? Network share, USB disk, local disk? It am wondering if it is that disk that is causing your weird issues (working, not working).