Hi,
I have very little knowledge when it comes to vbscript so I was wondering if someone out there could possibley help me with this issue. Please bear with we as explaining the issue will be quite lengthy
When we create a product design in our system is is always 6 numeric digits in length i.e. 123456, then when we place production orders the order number is in the format of
Product design number then a "/" then the cycle then another "/" then a 0 or 1. the cycle part can be either 1 or 2 digits i.e. A thru to Z and then it continues as AA,AB and so on, if the cycle is a single digit then it is preceded with a space therefore the production number end up in one of 2 formats
123456/ A/0
123456/AA/0
but the production order is always 11 digits in length we then create a PDF containing information required by production which is placed on a network drive, the filename of the PDF is constructed as follows.
"productionOrder" & PO Number & ".pdf" therefore a finished filename would look like so
productionOrder123456/ A/0.pdf or productionOrder123456/AA/0.pdf then because having the forward slash in could create problems they are changed to be an underscore therefore the filename changes to
productionOrder123456_ A_0.pdf or productionOrder123456_AA_0.pdf. We then had an issue that when the filename was created the space in a single alpha cycle was omitted thus creating a PDF with the following filename
productionOrder123456_A_0.pdf
we now had 2 different length filnames if it was a single cycle then the filename length was 29 characters, if it was a double cycle the filename was 30 characters and it was important that the filename was always 30 characters so a person who is no longer with the company wrote some vbscript to change the first single unscore to be a double underscore if the filename was only 29 characters using the following code
It now appears that for some reason the space is not being ommitted when the file name is produced therefore the filename is now 30 characters long but in a single cycle contains a space but we need it to be an underscore.
Therefore I ahve been trying to find some code that would remove the space before the other code is run so taht we get the correct format, I have manage dto find some and change to to do what we need but am unsure how stable it would be and also I have no idea how to incorporate it into the other code so that it can be run together as a scheduled task which would run every 30 minutes. the code for removing the space is as follows
I am really sorry for the length of the thread but felt it neccessary to try and explain fully the dilema we face at the moment seeing that we can place in the region of 700 orders per day
Regards
Paul
I have very little knowledge when it comes to vbscript so I was wondering if someone out there could possibley help me with this issue. Please bear with we as explaining the issue will be quite lengthy
When we create a product design in our system is is always 6 numeric digits in length i.e. 123456, then when we place production orders the order number is in the format of
Product design number then a "/" then the cycle then another "/" then a 0 or 1. the cycle part can be either 1 or 2 digits i.e. A thru to Z and then it continues as AA,AB and so on, if the cycle is a single digit then it is preceded with a space therefore the production number end up in one of 2 formats
123456/ A/0
123456/AA/0
but the production order is always 11 digits in length we then create a PDF containing information required by production which is placed on a network drive, the filename of the PDF is constructed as follows.
"productionOrder" & PO Number & ".pdf" therefore a finished filename would look like so
productionOrder123456/ A/0.pdf or productionOrder123456/AA/0.pdf then because having the forward slash in could create problems they are changed to be an underscore therefore the filename changes to
productionOrder123456_ A_0.pdf or productionOrder123456_AA_0.pdf. We then had an issue that when the filename was created the space in a single alpha cycle was omitted thus creating a PDF with the following filename
productionOrder123456_A_0.pdf
we now had 2 different length filnames if it was a single cycle then the filename length was 29 characters, if it was a double cycle the filename was 30 characters and it was important that the filename was always 30 characters so a person who is no longer with the company wrote some vbscript to change the first single unscore to be a double underscore if the filename was only 29 characters using the following code
Code:
'SFL Rename Files
' Please change the path below.....
Dim sInputFolder
Dim oFs: Set oFs = CreateObject("Scripting.FileSystemObject")
Main()
Set oFs = Nothing
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Main()
GetArguments()
RenameFiles(sInputFolder)
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub GetArguments()
'Dim oArgs: Set oArgs = WScript.Arguments
' If oArgs.Count <> 1 Then
' WScript.Echo(INVALID_ARG_ERR)
' WScript.Quit(INVALID_ARG_RET)
' End If
'##################Change HERE#######################
sInputFolder = "D:\Users\POliver\Desktop\JobCards2"
'####################################################
' Set oArgs = Nothing
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub RenameFiles(ByVal psFolderPath)
Dim iCnt
Dim oFolder
Dim oFile
If oFs.FolderExists(psFolderPath) Then
Set oFolder = oFs.GetFolder(psFolderPath)
For Each oFile In oFolder.Files
'### *** ### CHANGE THIS CODE TO CHECK FOR FILES THAT NEED TO BE RENAMED
If Not InStr(7, oFile.Name,"__",1) > 0 and len(oFile.Name) = 29 Then
'WScript.Echo(oFile.Path)
RenameFile(oFile.Path)
End If
Next
Else
WScript.Echo(Replace(FOLDER_MISSING_ERR, "$FolderName", psFolderPath))
WScript.Quit(FOLDER_MISSING_RET)
End If
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub RenameFile(ByVal psFileName)
Dim aTemp
Dim sFilePath, _
sFileName, _
sFileName_Target
aTemp = Split(psFileName, "\")
sFileName = aTemp(UBound(aTemp))
'### *** ### CHANGE THIS CODE TO END UP WITH THE TARGET FILENAME THAT YOU WANT
'WScript.Echo(sFileName)
'WScript.Echo(mid(sFileName,7,1))
sFileName_Target = mid(sFileName,1,21)&Replace(mid(sFileName,22,1), "_", "__")&mid(sFileName,23,7)
'WScript.Echo(sFileName_Target)
sFilePath = Left(psFileName, Len(psFileName) - Len(sFileName))
On Error Resume Next
If oFs.FileExists(psFileName) Then
If oFs.FileExists(sFilePath & sFileName_Target) Then
oFs.DeleteFile(sFilePath & sFileName_Target)
End If
oFs.MoveFile psFileName, sFilePath & sFileName_Target
Else
WScript.Echo(Replace(FILE_MISSING_ERR, "$FileName", psFileName))
WScript.Quit(FILE_MISSING_RET)
End If
On Error Goto 0
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const INVALID_ARG_RET = 1
Const INVALID_ARG_ERR = "You must provide a path of the files you wish to rename."
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const FILE_MISSING_RET = 51
Const FILE_MISSING_ERR = "File Does Not Exist: $FileName"
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const FOLDER_MISSING_RET = 2
Const FOLDER_MISSING_ERR = "Folder Does Not Exist: $FolderPath"
It now appears that for some reason the space is not being ommitted when the file name is produced therefore the filename is now 30 characters long but in a single cycle contains a space but we need it to be an underscore.
Therefore I ahve been trying to find some code that would remove the space before the other code is run so taht we get the correct format, I have manage dto find some and change to to do what we need but am unsure how stable it would be and also I have no idea how to incorporate it into the other code so that it can be run together as a scheduled task which would run every 30 minutes. the code for removing the space is as follows
Code:
Option Explicit
Dim StdIn: Set StdIn = WScript.StdIn
Dim StdOut: Set StdOut = WScript.StdOut
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
'Dim FilesRenamed: FilesRenamed = 0
Main
Sub Main
Dim FolderPath
FolderPath = "D:\Users\POliver\Desktop\JobCards2"
Dim CurrentFolder
Set CurrentFolder = fso.GetFolder(FolderPath)
ProcessFolder CurrentFolder
'StdOut.WriteLine FilesRenamed & " Files renamed."
End Sub
Sub ProcessFolder (ByVal Folder)
Dim Files: Set Files = Folder.Files
Dim File
For Each File In Files
If InStr(1,File.Name," ") > 0 Then
File.Move Replace(File.Path," ","")
'FilesRenamed = FilesRenamed + 1
End If
Next
End Sub
Set fso = Nothing
I am really sorry for the length of the thread but felt it neccessary to try and explain fully the dilema we face at the moment seeing that we can place in the region of 700 orders per day
Regards
Paul