ahuxham (16th November 2009)
Afternoon Folks,
My second query of the day regarding VBS;
Is it at all possible to exec a command within VBS, based on lines within a text document.
At present I have a portion of script to index the current folder:
From there I would like to exec the following snippet for every line in the document:Code:Set write = CreateObject("Scripting.FileSystemObject") sFolder = "Y:\" Set newfile = write.CreateTextFile(sFolder&"\Resources\pdf_list.txt", True) Set folder = wrIte.GetFolder(sFolder) Set files = folder.files For each folderIdx In files newfile.writeline(folderIdx.Name) Next newfile.Close
Is there any means to get the second snippet to run the said command on each line of the file. The above command is a third part PDF renaming solution designed to rename files based on content, I.e. Invoice number at a specified location, and a trimming mechanism to only attain the actual number, as previous these are named 00*****.pdf as the invoice number.Code:Set cmd= wscript.createobject("WScript.shell") cmd.run "cmd /K cd c:\Program Files\Program\ & command.exe Y:\file.pdf -CY:\Resources\macro.txt -i", 0
pdf_list.txt:
Code:AB invoice (13112009 214705).PDF AC invoice (3112009 214704).PDF BU invoice (13112009 214706).PDF DP invoice (13112009 214716).PDF FI invoice (13112009 214718).PDF
You need to look up the file handling routines. You can open the text file and read it line by line into a variable and then do what you want with it.
Something like this:
Code:Const ForReading = 1 Set ObjFSO = CreateObject("Scripting.FileSystemObject") Set TextFile = ObjFSO.opentextfile("c:\pdflist.txt",forreading) do while TextFile.atendofstream <> true FileName=TextFile.readline loop
Last edited by ChrisH; 16th November 2009 at 03:48 PM.
ahuxham (16th November 2009)
What about this? Obviously "newfile" is mentioned earlier in the script as
Code:sFolder = "Y:\" Set newfile = write.CreateTextFile(sFolder&"\Resources\pdf_list.txt, True)Just need to figure out how to convert the "Y:\a.pdf" argument into the line information.Code:Set read = CreateObject("Scripting.FileSystemObject") Set prncmd= wscript.createobject("WScript.shell") strData = read.OpenTextFile(newfile,ForReading).ReadAll arrLines = Split(strData,vbCrLf) For Each strLine in arrLines prncmd.run "cmd /K cd c:\Program Files\A-PDF Rename\ & prncmd.exe Y:\A.pdf -CY:\Resources\macro.txt -i", 0 Next
Way beyond my intellect on VBS, might have to toy around with it for a while.
Last edited by ahuxham; 16th November 2009 at 04:13 PM.
My recommendation from the last post regarding concatenation is very relevant when using the run command. Build it up outside the run statement as a variable then use that.
Something like that will work. It probably wont work though because of the space in "program files" you can fix that with some single quotes inside or some Chr(32) or whatever it is egCode:Set read = CreateObject("Scripting.FileSystemObject") Set prncmd= wscript.createobject("WScript.shell") strData = read.OpenTextFile(newfile,ForReading).ReadAll arrLines = Split(strData,vbCrLf) For Each strLine in arrLines RunCommand = "cmd /K cd c:\Program Files\A-PDF Rename\" & prncmd.exe" Y:\" & strLine & " -CY:\Resources\macro.txt -i", 0 " prncmd.run RunCommand Next
"cmd /K cd 'c:\Program Files\A-PDF Rename\'" & prncmd.exe"
Last edited by ChrisH; 16th November 2009 at 04:23 PM.
ahuxham (16th November 2009)
Odd indeed, the functionability is all there now, however the RunCommand line isn't working as expected. Keeps prompted for an "Expected end of statement" at Char 70 which is the end of prncmd.exe
Currently have:
I have tested with wscript.echo strLine and it outputs perfectly, now just to attached to said commandCode:RunCommand = "cmd /K cd c:\Program Files\A-PDF Rename\" & prncmd.exe "Y:\ "& strLine &" -CY:\Resources\macro.txt -i", 0"![]()
I can't work out what a single, correct command should look like - can you post just one line as you would type it to get it to work!
Guessing at what it should be, I think you're trying to get:
If so, then the runcmd line needs to be:Code:cmd /K "c:\Program Files\A-PDF Rename\prncmd.exe" Y:\ -CY:\Resources\macro.txt -i, 0
Why cmd /k? I think you might be better with cmd /c (/k shells the command and leaves the command processor running - you'll end up with lots of command shells open!)Code:runCommand = "cmd /K ""c:\Program Files\A-PDF Rename\prncmd.exe"" Y:\ " & strLine & "-CY:\Resources\macro.txt -i, 0"
Not sure if it would be easier, but if you can get a list of the files in Excel (eg dir /b > c:\temp\files.txt and then open that file in Excel) then you can make a formula to add the necessary code to the file name; you then just copy and paste the list of commands into Notepad and save as a batch file. Working like this can be easier if it's just a one off (but is no use if you need to run this once a week etc)
I'll change to /c, thanks for that.
The command isAnd I'd like to process everything in the dir, the prnccmd doesn't accept mutliple instances, only single files, and this would have to be VBS.Code:prncmd.exe Y:\filename.pdf -CY:\Resources\Macro.txt -i
1) Only scripting language I roughly know and can cobble together
2) The files are generated daily on exports, and different times of day, so would be run twice daily to get everything imported, thus vbs.
Obviously as Chris was showing, I want
The 0 at the end of the line is VBS for hiding the Window.Code:prncmd.exe Y:\%stringname%.pdf -CY:\Resources\Macro.txt -i
OK; think we're close :-)
but it's way past my bedtime so I could have made a typo!Code:runCommand = "cmd /c ""c:\Program Files\A-PDF Rename\prncmd.exe"" Y:\" & strLine & " -CY:\Resources\macro.txt -i" prncmd.run runCommand,0
ahuxham (17th November 2009)
Work to some regards, however I've found a problem, and not sure how to fix it as all the """"""'s are confusing me.
The file name which is Y:\& strLine & should be "Y:\& strLine &". I'm getting file cannot be found, but the second I load cmd and add the quotes the command works in it's entirety.
Any ideas?
Use chr(34) instead of double quotes it will be easier to figure out.
Last edited by ChrisH; 17th November 2009 at 01:52 PM. Reason: chr(34) instead of chr(32)
ahuxham (17th November 2009)
Yeah this can get really confusing and you can use double or triple " to get it done but as chris says its probably easier to read if its something like this:
so it should come out:Code:"cmd /c " & chr(32) & "c:\Program Files\A-PDF Rename\prncmd.exe" & chr(32) & " " & chr(32) & "Y:\" & strLine & chr(32) & " -CY:\Resources\macro.txt -i"
ofcourse strLine is what ever the variable is.Code:cmd /c "c:\Program Files\A-PDF Rename\prncmd.exe" "Y:\strLine" -CY:\Resources\macro.txt -i[/
is that what you want?
ahuxham (17th November 2009)
Er yeah that my excuse..
I'm sure you were Chris/Apeo![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)