VBScript (.vbs) Produces Errors When Executed From A 2nd .vbs - arrays

We have a vendor who sends us CSV index files to be used with our OnBase document import software. If the CSV file was generated using one of our institutional forms, OnBase ingests them w/o error as we have set up corresponding Document Types that match the 1st delimited value in the CSV file. However, if the CSV file was generated using a vendor form, the 1st delimited value is slightly different and creates OnBase indexing errors. Our vendor uses this CSV format for many of their clients and have indicated it cannot be customized to match our current OnBase Document Types (w/o $$$).
I was tasked with identifying a workaround and found another institution using VBScript to create CSV index files before OnBase processing. After some collaboration, I was able to code a similar approach using VBScript. However, since we already receive index files, our approach identifies how many CSV files are in a target folder, via a loop opens each one, identifies the 1st delimited value within the CSV file, compares that value via a switch case method, updates the 1st delimited CSV value based on the switch case method, then saves the file, until the loop ends (loop value = # of files in target folder - 1...to account for the vbs file). When I click on the vbs file, it works like a charm!
Here is where the trouble begins. If I try and run the vbs file from another vbs file, the script produces errors: File not found. I included some debugging code, and I can see it correctly counts the number of files in the target folder, etc. When I thought it wasn't finding the target folder, I accounted for that by harcoding the path during testing. Even when I am sure it is finding the correct target folder, I'm receiving the same File Not Found error. If I double-click the exact same vbs file, it runs without error and all CSV files are correctly updated.
Update #Ansgar Wiechers resolved the issue of finding and keeping the correct file path value. But, I'm still receiving a File Not Found error at line 163.
Line 163
Char 1
Error: File Not Found
Code: 800A0035
Source: Microsoft VBScript runtime error
On a side note, I've also noticed that my vbs script will only run one time before you have to copy/paste a new instance in the target folder before running it again. Which leads me to think my loop never ended, so I included a debugging MsgBox that displays after the loop has ended (or think it has ended) and right before the WScript.Quit command. These are the last two lines in the script and the message box successfully displays. I'll include code below from both my vbs files.
VBScript file #1 (asterisks masking personal info):
Dim CurrentDirectory
Set FSO = CreateObject("Scripting.FileSystemObject")
CurrentDirectory = "C:\Users\*******\Desktop\ProVerify\Testing\ERROR_FILES\"
FSO.CopyFile "C:\Users\*******\Desktop\ProVerify\Testing\Old Source Files\VBScript_PV_Form_conversion.vbs", CurrentDirectory
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "C:\Users\*******\Desktop\ProVerify\Testing\ERROR_FILES\VBScript_PV_Form_conversion.vbs"
WshShell.Popup "VBS file will be deleted in 10 seconds...", 10
FSO.DeleteFile "C:\Users\*******\Desktop\ProVerify\Testing\ERROR_FILES\VBScript_PV_Form_conversion.vbs"
WScript.Quit
VBScript file #2 (in target folder with CSV files; removed switch case code for this post to save space):
' Declare vars
Dim intDebug, sFile, strDirectory, numFiles, sLine, aLine
intDebug = 1
' Find path of folder where script file resides & Set Object vars
Set objFSO = CreateObject("Scripting.FileSystemObject")
strDirectory = objFSO.GetParentFolderName(WScript.ScriptFullName)
Set objFolder = objFSO.GetFolder(strDirectory)
' Count number of files in folder
numFiles = objFolder.Files.Count - 1
If intDebug = 1 Then
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Popup "File Count: " & numFiles, 2
WshShell.Popup "File Path: " & strDirectory, 2
Else
End If
If numFiles <= 1 Then
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Popup "No Files to Process", 2
WScript.Quit
Else
End If
'Loop through each file in folder
For Each folderIdx In objFolder.Files
Set oStream = folderIdx.OpenAsTextStream
'Read file and capture first delimeted value
sLine = oStream.ReadLine
oStream.Close
aLine = Split(sLine, ",")
' Compare delimeted value & update to OnBase DocType naming convention
Select Case aLine(0)
[*****case method here*****]
End Select
' Create replacement delimited value
sLine = ""
For i = LBound(aLine) To UBound(aLine)
sLine = sLine + aLine(i) + ","
Next
'Open file and replace updated delimeted value
Set oStream = objFSO.OpenTextFile(folderIdx.Name, 2)
oStream.WriteLine Left(sLine, Len(sLine)-1) ' Remove comma from updated delimeted value
oStream.Close
Next
'Reset Object vars
Set oStream = Nothing
Set objFSO = Nothing
Set objFolder = Nothing
If intDebug = 1 Then
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Popup "Conversion Complete", 2
Else
End If
WScript.Quit
In summary:
This code works fine (once) when I double-click; then a new instance needs to be copied/pasted into target folder before double-clicking will run the file again.
Code produces error when run via another vbs file, line 163, File Not Found.
I'm pretty sure this could be accomplished within OnBase, but our OnBase Sys Admin insists I am incorrect. The 51 OnBase User Guides referring to VBScipting over 1,500 times, tells me otherwise. That said, for now I've tasked with finding a solution outside of OnBase. Feel free to comment on this topic, too.

Errors like the one you describe are usually caused by incorrect assumptions about the working directory of a script.
Your folder structure apparently looks somewhat like this (names shortended for brevity):
C:\base\folder
├── ERROR_FILES
│   ├── a.csv
│   ├── b.csv
: :
│   └── second.vbs
└── first.vbs
When you double-click the script second.vbs the working directory is C:\base\folder\ERROR_FILES. However, when you double-click first.vbs the working directory is C:\base\folder. Invoking C:\base\folder\ERROR_FILES\second.vbs from there DOES NOT change the working directory for second.vbs.
Now let's take a look at the code in your second script:
strDirectory = objFSO.GetAbsolutePathName(".")
'***********FOR TESTING ONLY**********************
If strDirectory = "C:\Users\*******\Desktop\ProVerify\Testing" Then
strDirectory = objFSO.GetAbsolutePathName(".") & "\ERROR_FILES"
Else
End If
'***********FOR TESTING ONLY**********************
Set objFolder = objFSO.GetFolder(strDirectory)
...
For Each folderIdx In objFolder.Files
Set oStream = objFSO.OpenTextFile(folderIdx.Name, 1)
...
Next
The "FOR TESTING ONLY" section kind of tries to account for the working directory difference by appending "\ERROR_FILES" to strDirectory, which allows you to enumerate the content of that folder. However, running objFSO.OpenTextFile(folderIdx.Name, 1) still attempts to open each file from the current working directory, which is still C:\base\folder.
To avoid this issue run objFSO.OpenTextFile(folderIdx.Path) or, better yet, use the object's OpenAsTextStream method. Also, when your script is residing in the same folder as your data files it's better to get the directory from the script path rather than appending a particular subfolder to the working directory (which may not be what you think it is).
Change the above code fragment to something like this, and it will do what you want:
strDirectory = objFSO.GetParentFolderName(WScript.ScriptFullName)
Set objFolder = objFSO.GetFolder(strDirectory)
...
For Each folderIdx In objFolder.Files
Set oStream = folderIdx.OpenAsTextStream
...
Next
Use the same approach when you open the file for writing:
Set oStream = folderIdx.OpenAsTextStream(2)

Related

Print multiple copies of pdf from script file

I would like to silent print a PDF file multiple times. I don't really mind what implementation is used, but due to being in a corporate environment I cannot easily install unsupported software :(.
I am currently using the following VBscript but could switch to any other implementation:
TargetFolder = "<path to folder>"
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(TargetFolder)
Set colItems = objFolder.Items
For Each objItem In colItems
For i = 1 To 13
objItem.InvokeVerbEx ("Print")
Next
Next
This spools the job 13 times though. Is there a way to do this as a single job?
I also saw a suggestion for printing using adobe reader that looked like this:
AcroRd32.exe /t <file.pdf> <printer_name> <printer_driver> <printer_port>
But I couldn't find any reference material for passing the number of copies as a parameter.
I just found this questions which is essentially the same:
Programatically print multiple copies from command line
It appears that looping through and sending the file multiple times is the only solution without additional software.

get the files in folder between two timestamp

I have a folder 'script'. People check-in the files to this folder. Every time they check-in, I need to get the those files alone and execute them. For that, current timestamp needs to be saved in some log file. so that we can get the files that are modified after the last build by comparing the current time with last execution time(which is saved in log file). Let me explain that clearly.
Folder name -- script.
there three files in this folder -- a.sql, b.sql, c.sql
after few hours -- two new files are created. also b.sql is modified.
totally five files -- a.sql, b.sql, c.sql, d.sql, e.sql
now I need to execute only those two new files and one modified file.
it should be like below
b.sql
d.sql
e.sql
we need to compare the current time with last execution time and get the files that are modified/created between two timestamps.
Can someone tell me how to do it using vbscript or windows script?
Look at the docs and this demo code:
Option Explicit
Const Archive = 32
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim oFile
For Each oFile In goFS.GetFolder("..\data").Files
WScript.Echo oFile.Name, oFile.Attributes
If oFile.Attributes And Archive Then
WScript.Echo oFile.Name, "execute and clear archive bit"
oFile.Attributes = oFile.Attributes - Archive
End If
Next

AppleScript Move File(s) failing. What did I do wrong?

I'm working on my first AppleScript and have run into an error on the last command. Basically, the script is supposed to loop through all of the jpg's in a given folder ("/Volumes/Public2/Pictures/LINP/temp/") and move them all into sub-folders based on the date in the file name:
Ex. 000DC5D1A54E(Patio)_1_20130604084734_139988.jpg
Where 20130604 is the date (2013-06-04)
The part of my script that gets each of the file names and loops over them, creating the necessary sub-folders works great. No issues at all. It's when I try the actual "move" that I get a System Events error. So, to start, here's the complete script:
set workFolder to "/Volumes/Public2/Pictures/LINP/temp/"
log workFolder
set workFolder to (POSIX file workFolder) as alias
log workFolder
tell application "System Events"
--get files to work on
set filesToProcess to files of workFolder
--log filesToProcess
repeat with thisFile in filesToProcess
log thisFile
set {fileName, fileExt} to {name, name extension} of thisFile
log fileName
log fileExt
if (fileExt is in "(*jpg*)" and ((length of fileName) is greater than 10)) then
--get name of file without extension
set rootName to text 1 thru -((length of fileExt) + 2) of fileName
log rootName
--break apart the file name to get to the imortant stuff
set AppleScript's text item delimiters to "_"
--store the camera name
set cameraName to text item 1 of rootName
log cameraName
--store the full date
set photoDate to text item 3 of rootName
log photoDate
--cut out the time information
set photoDate to text 1 thru 8 of photoDate
log photoDate
--break out the year
set photoYear to text 1 thru 4 of photoDate
log photoYear
--break out the month
set photoMonth to text 5 thru 6 of photoDate
log photoMonth
--break out the day
set photoDay to text 7 thru 8 of photoDate
log photoDay
--combine parts into a new name of the form "YYYY-MM-DD"
set photoFolder to photoYear & "-" & photoMonth & "-" & photoDay
log photoFolder
--make sure a correctly named folder exists
set targetFolder to my checkForFolder({parentFolder:(workFolder as text), folderName:photoFolder}) as text
log "targetFolder: " & targetFolder
log thisFile
log "targetFolder Class: " & class of targetFolder
log "thisFile Class: " & class of thisFile
set finalTarget1 to (POSIX file targetFolder) as alias as string
log "finalTarget1: " & finalTarget1
log "finalTarget1 Class: " & class of finalTarget1
--Here's where the error pops up. I've tried both HFS and POSIX formats for the target folder, but the issue seems to be with the file
--Yes, the files definitely exist (I've confirmed this). I show exactly what the values are and error messages I'm getting at the end of this post
move thisFile to the folder finalTarget1
move thisFile to the folder targetFolder
end if
end repeat
end tell
to checkForFolder({parentFolder:fParent, folderName:fName})
--find or create a folder
tell application "System Events"
--set fName to POSIX file of fName as alias
--set fParent to POSIX file of fParent as alias
if not (exists POSIX path of (folder fName of folder fParent)) then
set output to POSIX path of (make new folder at end of folder fParent with properties {name:fName})
else
set output to (POSIX path of (folder fName of folder fParent))
end if
end tell
--returns a POSIX path
return output
end checkForFolder
After hours of poring over the Apple Forums, Stack Overflow and consulting the Google oracle, I'm aware that context matters, so I'm trying to provide as much information as possible. Here, I've printed (log) the contents and Class types of thisFile, targetFolder and finalTarget1:
(targetFolder: /Volumes/Public2/Pictures/LINP/temp/2013-06-04)
(targetFolder Class: text)
thisFile
(*file Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg*)
(thisFile Class: file)
(finalTarget1: Public2:Pictures:LINP:temp:2013-06-04:)
(finalTarget1 Class: text)
And here are the errors displayed by each of the two move attempts:
move thisFile to the folder finalTarget1
= move file "Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg" to folder "Public2:Pictures:LINP:temp:2013-06-04:"
error "System Events got an error: Can’t get file \"Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg\". " number -1728 from file "Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg"
move thisFile to the folder targetFolder
= move file "Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg" to folder "/Volumes/Public2/Pictures/LINP/temp/2013-06-04"
error "System Events got an error: Can’t get file \"Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg\". " number -1728 from file "Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg"
Finally, yes, Public2 is mounted and I know that it's accessible since I'm getting the full folder contents back AND creating each of the sub-folders appropriately. It's just this last "move" operation that is failing and I'm sure it's a product of my ignorance.
Can anyone help identify the cause and how I can fix it?
I took your script and quickly made a local version to test, with two jpegs in the temp folder. So far, this is what I came across.
Fix #1:
move thisFile to the folder finalTarget1
needs to be changed to
move thisFile to finalTarget1
but then there's is an error with
move thisFile to the folder targetFolder
because the script is attempting to move the file it already moved. Not sure if that line is in there for testing purposes. Maybe you can clarify. If I comment out the 2nd line it seems to work.

Find File based on partial file name and move file elsewhere in vbs

I'm looking to write a vbs script to do the following.
- There is a mapping file which contains an identifier followed by a directory name (e.g. 123456-Documents).
- For each line in the mapping file, there will be a document which begins with the identifier that I need to move elsewhere (e.g. 123456_otherStuff_Dec 12.xls).
- I then need to parse the full filename and take the middle string (otherStuff) to determine which sub directory it goes into.
So in the above example, the file "123456_otherStuff_Dec 12.xls" should end up in the directory C:.../current/Documents/otherStuff/123456_otherStuff_Dec 12.xls.
I'm new to vbs, but I know a bit of Java so have been able to make a start on this. This is what I have so far:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Documents and Settings\w133960\Desktop\test1\Text1.txt", ForReading)
Const ForReading = 1
Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close
For Each strLine in arrFileLines
WScript.Echo strLine
MyArray = Split(strLine, "-", -1, 1)
WScript.Echo MyArray(0)
WScript.Echo MyArray(1)
Next
This goes through the mapping file and will parse the identifer for me. The next part would be searching for the file beginning with the identifier, then parse it's filename and move it into the relevant directory based on that.
I could add another For Loop inside the current loop, then parse the filename and move the file within that loop - but I am worried about the efficieny of this as there are a few hundred files to move.
Can anyone advise on the cleanest and most efficient way of doing this? Any help would be much appreciated.
A plan:
A first loop to extract the Ids (and the folder names) from the
mapping file into a dictionary (key = id, value = folder name
(possibly)) [never duplicate the lines collection in a file into an
array in memory, if you don't need to access item i to process item
j (i <>j)]
A second loop over the files in the (one and only?) source directory. Split the oFile.Name on the correct! separator "_", not "-". If the Id is found in the dictionary, use the parts to build (FileSystemObject.BuildPath()) the new/target file spec. Check (FileSystemObject.FileExists()) whether the targext exists, if not, oFile.Move

Moving files using VBscript

I am trying to move a group of files with in a group of folders named recup_dir.1 through recup_dir.535 into a single folder so that all the files will be out of the folders and just in the single folder. I know I will need to use a loop to move the files and probably concatenation to go from recup_dir.1 to recup_dir.535 but I just am not that skilled in programming please help!! I just want it to automate the copying and moving of the files rather than do it manually.
Try the following (it assumes that you want to bring the files into the directory that you execute the script from):
Dim filesys, file
Set filesys = CreateObject("Scripting.FileSystemObject")
Dim i
For i = 0 to 535
Dim files
Set files = filesys.GetFolder("recup_dir." & i).Files
For Each file in files
filesys.MoveFile "recup_dir." & i & "\" & file.Name, ".\" & file.Name 'assuming you want all the files to be in the current directory
Next
Next
Of course, please make a backup of all of your folders and files before testing this script.

Resources