ahuxham Posted November 16, 2009 Posted November 16, 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: 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 From there I would like to exec the following snippet for every line in the document: 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 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. pdf_list.txt: 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
ChrisH Posted November 16, 2009 Posted November 16, 2009 (edited) 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: 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 Edited November 16, 2009 by ChrisH 1
ahuxham Posted November 16, 2009 Author Posted November 16, 2009 (edited) 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: 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 What about this? Obviously "newfile" is mentioned earlier in the script as sFolder = "Y:\" Set newfile = write.CreateTextFile(sFolder&"\Resources\pdf_list.txt, True) 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 Just need to figure out how to convert the "Y:\a.pdf" argument into the line information. Way beyond my intellect on VBS, might have to toy around with it for a while. Edited November 16, 2009 by ahuxham
ChrisH Posted November 16, 2009 Posted November 16, 2009 (edited) 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. 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 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 eg "cmd /K cd 'c:\Program Files\A-PDF Rename\'" & prncmd.exe" Edited November 16, 2009 by ChrisH 1
ahuxham Posted November 16, 2009 Author Posted November 16, 2009 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. 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 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 eg "cmd /K cd 'c:\Program Files\A-PDF Rename\'" & prncmd.exe" 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: RunCommand = "cmd /K cd c:\Program Files\A-PDF Rename\" & prncmd.exe "Y:\ "& strLine &" -CY:\Resources\macro.txt -i", 0" I have tested with wscript.echo strLine and it outputs perfectly, now just to attached to said command
srochford Posted November 16, 2009 Posted November 16, 2009 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: cmd /K "c:\Program Files\A-PDF Rename\prncmd.exe" Y:\ -CY:\Resources\macro.txt -i, 0 If so, then the runcmd line needs to be: runCommand = "cmd /K ""c:\Program Files\A-PDF Rename\prncmd.exe"" Y:\ " & strLine & "-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!) 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)
ahuxham Posted November 16, 2009 Author Posted November 16, 2009 I'll change to /c, thanks for that. The command is prncmd.exe Y:\filename.pdf -CY:\Resources\Macro.txt -i And 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. 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 prncmd.exe Y:\%stringname%.pdf -CY:\Resources\Macro.txt -i The 0 at the end of the line is VBS for hiding the Window.
srochford Posted November 16, 2009 Posted November 16, 2009 OK; think we're close :-) runCommand = "cmd /c ""c:\Program Files\A-PDF Rename\prncmd.exe"" Y:\" & strLine & " -CY:\Resources\macro.txt -i" prncmd.run runCommand,0 but it's way past my bedtime so I could have made a typo! 1
ahuxham Posted November 17, 2009 Author Posted November 17, 2009 OK; think we're close :-) runCommand = "cmd /c ""c:\Program Files\A-PDF Rename\prncmd.exe"" Y:\" & strLine & " -CY:\Resources\macro.txt -i" prncmd.run runCommand,0 but it's way past my bedtime so I could have made a typo! 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?
ChrisH Posted November 17, 2009 Posted November 17, 2009 (edited) Use chr(34) instead of double quotes it will be easier to figure out. Edited November 17, 2009 by ChrisH chr(34) instead of chr(32) 1
apeo Posted November 17, 2009 Posted November 17, 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: "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" so it should come out: cmd /c "c:\Program Files\A-PDF Rename\prncmd.exe" "Y:\strLine" -CY:\Resources\macro.txt -i[/ ofcourse strLine is what ever the variable is. is that what you want? 1
ahuxham Posted November 17, 2009 Author Posted November 17, 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: "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" so it should come out: cmd /c "c:\Program Files\A-PDF Rename\prncmd.exe" "Y:\strLine" -CY:\Resources\macro.txt -i[/ ofcourse strLine is what ever the variable is. is that what you want? Perfect thank you. However its 34 not 32, 32 is a space, but that's beside the point.
ChrisH Posted November 17, 2009 Posted November 17, 2009 Perfect thank you. However its 34 not 32, 32 is a space, but that's beside the point. I was just making sure you were awake
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now