hexagon logo

Create folder script

It used to be here.
  • Thanks for sharing. This has been an issue for me before. My solutions have been less elegant.


    Jan.
  • You are most welcome! Happy to have helped!
  • Back due to popular demand.

    ' Create folder and save with user input for PC-DMIS
    '
    ' When called, it asks the user for a folder to save in
    ' and creates the folder if it doesn't exist - the full path
    ' needs to be entered with drive letter and all.
    ' User is the asked for a filename to save as.
    '
    ' This path is then passed to PC-DMIS, which needs to have
    ' a variable called 'RESFOLDER'.
    '
    ' Add a print command to your partprogram and make sure
    ' the script and the RESFOLDER variable is placed before
    ' the print command.
    '
    ' Change the path in the print command to 'RESFOLDER'
    ' to save the report in your folder.
    '
    'CS1        =SCRIPT/FILENAME= C:\CREATEFOLDERINPUT.BAS
    '            FUNCTION/Main,SHOW=YES,,
    '            STARTSCRIPT/
    '            ENDSCRIPT/
    '            ASSIGN/RESFOLDER="c:\MYFOLDER\MYREPORT.pdf"
    '            PRINT/REPORT,EXEC MODE=END,$
    '              TO_FILE=ON,APPEND=RESFOLDER,$
    '              TO_PRINTER=OFF,$
    '              TO_DMIS_REPORT=OFF,FILE_OPTION=INDEX,FILENAME=,$
    '              REPORT_THEORETICALS=NONE,REPORT_FEATURE_WITH_DIMENSIONS=NO,$
    '              PREVIOUS_RUNS=DELETE_INSTANCES
    '
    ' vpt.se 2010
    '
    
    
    
    Sub Main()
    Dim objFSO, objFolder, objShell, strDirectory, InputFolder
    
    strDirectory = InputBox("Enter folder to save in: ", "Create folder")
    strFileName = InputBox("Enter filename to save as: ", "Enter filename")
    
    ' Create the File System Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    ' Note If..Exists. Then, Else ... End If construction
    If objFSO.FolderExists(strDirectory) Then
       Set objFolder = objFSO.GetFolder(strDirectory)
       'WScript.Echo strDirectory & " already created "
    Else
       If strDirectory <> "" Then 
          Set objFolder = objFSO.CreateFolder(strDirectory)
          'WScript.Echo "Just created " & strDirectory
       End If
    End If
    
    'If err.number = vbEmpty Then
    '   If strDirectory <> "" Then
    '      Set objShell = CreateObject("WScript.Shell")
    '      'objShell.run ("Explorer" &" " & strDirectory & "\" )
    '   End If
    'Else WScript.echo "VBScript Error: " & err.number
    'End If
    
    Dim PCDApp, PCDPartProgram, PCDCommands, PCDCommand, retval
    Dim cnt As Integer
    Dim found As Boolean
    Dim varname
    
    Set PCDApp = CreateObject("PCDLRN.Application")
    Set PCDPartProgram = PCDApp.ActivePartProgram
    Set PCDCommands = PCDPartProgram.Commands
    
    found = False
    varname = "RESFOLDER"
    strDirectory = strDirectory & "\" & strFileName & ".pdf"
    	
    For cnt = 1 To PCDCommands.Count
      Set PCDCommand = PCDCommands.Item(cnt)
      If ((PCDCommand.Type = 195) And (varname = PCDCommand.GetText(DEST_EXPR, 0))) Then
        retval=PCDCommand.PutText("""" & strDirectory & """", SRC_EXPR, 0)
        found = True
      End If
    Next
    
    If Not found Then
      Set PCDCommand = PCDCommands.Add(195, True) 
      PCDCommand.marked = True
      PCDCommand.PutText(varname, DEST_EXPR, 0) 
      PCDCommand.PutText("""" & strDirectory & """", SRC_EXPR, 0)
    End If
    
    End Sub
  • Sorry I know this is an old thread but I have a question about it.

    Is there a way to use this script to create the folder using a variable set it PCDMIS rather than asking for an additional operator input? Example, in PCDMIS I ask the operator to input the Job# with a Comment/Input, can that be used to create the folder?
  • Yes you can. One sec and will post.

    
    'Variables
    Set opname     = PCDPartProgram.GetVariableValue ("OPERATOR")
    Set joname      = PCDPartProgram.GetVariableValue ("JOB")
    Set sampname = PCDPartProgram.GetVariableValue ("SAMP")
    
    'Assignments per Variable Call-Ins
    Fname         = sampname.Stringvalue & " " &  opname.Stringvalue & " "  
    strDirectory  = PCDPartProgram.Path & "\" & "Job Log"
    strFileName = joname.StringValue
    strPath         = strDirectory & "/" & strFileName & ".txt"
    
    ' Create the File System Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    'Check For Text File And create If Necessary
    If objFSO.FileExists(strPath) Then
      Set objFile   = objFSO.OpenTextFile(strPath)
    Else
      Set objFile   = objFSO.CreateTextFile(strPath)
        Set objFile = objFSO.OpenTextFile(strPath)
    End If
    
    


    This is an example with a text file. But the folder commands are similar.
  • Not sure what I'm doing wrong, I commented out 'Set opname' and 'Set sampname' so I'm only using 'Set joname'. I changed my Comment.Input name to JOB, but I get a 'Type Mismatch' error on that line.
  • So are you trying to pull in the information from a variable, or from a comment/input? Or are you trying to use an inputbox in VB? All three of those is a little different in coding.
  • Then to use the method I posted. You will have to do something like this:

    JobInput     =COMMENT/INPUT,No,FULL SCREEN=NO,
                'Job #'
                ASSIGN/JOB=JobInput.INPUT
    


    With something like this in your script

    Set joname      = PCDPartProgram.GetVariableValue ("JOB")
    
  • I'm still getting the Type Mismatch error.

    Here's my code, maybe I'm missing something:

    ASSIGN/OOT=GETPROGRAMINFO("NUMOOT")
                ASSIGN/TIME=GETPROGRAMINFO("TIME")
    J1         =COMMENT/INPUT,YES,FULL SCREEN=NO,
                'Job Number:'
                ASSIGN/JOB=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="Z:\Everyone\ChadJ\2014programs\\"+J1.INPUT+"\JOB# "+J1.INPUT+" TIME "+TIME.PDF,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()
    
    Set joname=PCDPartProgram.GetVariableValue("JOB")
    
    Fname         = sampname.Stringvalue & " " &  opname.Stringvalue & " "  
    strDirectory  = PCDPartProgram.Path & "\" & "Job Log"
    strFileName = joname.StringValue
    strPath         = strDirectory & "/" & strFileName & ".txt"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    If objFSO.FileExists(strPath) Then
      Set objFile   = objFSO.OpenTextFile(strPath)
    Else
      Set objFile   = objFSO.CreateTextFile(strPath)
        Set objFile = objFSO.OpenTextFile(strPath)
    End If
    
    End Sub
    


    I know there's still some work to be done after Line 3 but that's where my error is currently.