+ Post New Thread
Results 1 to 15 of 15
Scripts Thread, VBS - wshell.run based on file contents? in Coding and Web Development; Afternoon Folks, My second query of the day regarding VBS; Is it at all possible to exec a command within ...
  1. #1
    ahuxham's Avatar
    Join Date
    Apr 2008
    Posts
    1,122
    Thank Post
    76
    Thanked 138 Times in 109 Posts
    Rep Power
    30

    VBS - wshell.run based on file contents?

    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:

    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
    From there I would like to exec the following snippet for every line in the document:

    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
    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:

    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

  2. #2
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,999
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106
    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 02:48 PM.

  3. Thanks to ChrisH from:

    ahuxham (16th November 2009)

  4. #3
    ahuxham's Avatar
    Join Date
    Apr 2008
    Posts
    1,122
    Thank Post
    76
    Thanked 138 Times in 109 Posts
    Rep Power
    30
    Quote Originally Posted by ChrisH View Post
    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
    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)
    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
    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.
    Last edited by ahuxham; 16th November 2009 at 03:13 PM.

  5. #4
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,999
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106
    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.

    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
    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"
    Last edited by ChrisH; 16th November 2009 at 03:23 PM.

  6. Thanks to ChrisH from:

    ahuxham (16th November 2009)

  7. #5
    ahuxham's Avatar
    Join Date
    Apr 2008
    Posts
    1,122
    Thank Post
    76
    Thanked 138 Times in 109 Posts
    Rep Power
    30
    Quote Originally Posted by ChrisH View Post
    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.

    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
    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:

    Code:
    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

  8. #6

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,154
    Thank Post
    114
    Thanked 527 Times in 450 Posts
    Blog Entries
    2
    Rep Power
    123
    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:
    Code:
    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:
    Code:
    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)

  9. #7
    ahuxham's Avatar
    Join Date
    Apr 2008
    Posts
    1,122
    Thank Post
    76
    Thanked 138 Times in 109 Posts
    Rep Power
    30
    I'll change to /c, thanks for that.

    The command is
    Code:
    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

    Code:
    prncmd.exe Y:\%stringname%.pdf -CY:\Resources\Macro.txt -i
    The 0 at the end of the line is VBS for hiding the Window.

  10. #8

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,154
    Thank Post
    114
    Thanked 527 Times in 450 Posts
    Blog Entries
    2
    Rep Power
    123
    OK; think we're close :-)
    Code:
    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!

  11. Thanks to srochford from:

    ahuxham (17th November 2009)

  12. #9
    ahuxham's Avatar
    Join Date
    Apr 2008
    Posts
    1,122
    Thank Post
    76
    Thanked 138 Times in 109 Posts
    Rep Power
    30
    Quote Originally Posted by srochford View Post
    OK; think we're close :-)
    Code:
    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?

  13. #10
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,999
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106
    Use chr(34) instead of double quotes it will be easier to figure out.
    Last edited by ChrisH; 17th November 2009 at 12:52 PM. Reason: chr(34) instead of chr(32)

  14. Thanks to ChrisH from:

    ahuxham (17th November 2009)

  15. #11
    apeo's Avatar
    Join Date
    Sep 2005
    Location
    Lost
    Posts
    1,612
    Thank Post
    95
    Thanked 115 Times in 111 Posts
    Rep Power
    41
    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:

    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"
    so it should come out:

    Code:
    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?

  16. Thanks to apeo from:

    ahuxham (17th November 2009)

  17. #12
    ahuxham's Avatar
    Join Date
    Apr 2008
    Posts
    1,122
    Thank Post
    76
    Thanked 138 Times in 109 Posts
    Rep Power
    30
    Quote Originally Posted by apeo View Post
    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:

    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"
    so it should come out:

    Code:
    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.

  18. #13
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,999
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106
    Quote Originally Posted by ahuxham View Post
    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

  19. #14
    apeo's Avatar
    Join Date
    Sep 2005
    Location
    Lost
    Posts
    1,612
    Thank Post
    95
    Thanked 115 Times in 111 Posts
    Rep Power
    41
    Er yeah that my excuse..

  20. #15
    ahuxham's Avatar
    Join Date
    Apr 2008
    Posts
    1,122
    Thank Post
    76
    Thanked 138 Times in 109 Posts
    Rep Power
    30
    I'm sure you were Chris/Apeo

SHARE:
+ Post New Thread

Similar Threads

  1. vbs complex run commands
    By djones in forum Scripts
    Replies: 19
    Last Post: 18th December 2008, 10:55 PM
  2. PHP file lists contents
    By blackcat in forum Web Development
    Replies: 1
    Last Post: 19th January 2008, 09:09 PM
  3. Replies: 4
    Last Post: 7th March 2007, 03:37 PM
  4. Replies: 1
    Last Post: 21st January 2007, 02:51 PM
  5. Deploy the contents of a CAB file through GPO?
    By tosca925 in forum Windows
    Replies: 0
    Last Post: 16th October 2005, 12:46 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •