Results 1 to 9 of 9

Thread: [RESOLVED] shell command help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2019
    Posts
    194

    Resolved [RESOLVED] shell command help

    works
    Code:
    Private Sub Command7_Click()
    shell "cmd /k ""cd C:\Users\leon\Desktop\newfolder&&test.bat"""
    End Sub

    does not work?
    Code:
    Private Sub Command1_Click()
    shell "cmd /k ""cd " & text1.text & """"
    End Sub
    Code:
    text1.text C:\Users\leon\Desktop\newfolder&&test.bat
    Last edited by doberman2002; Oct 5th, 2019 at 05:39 PM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: shell command help

    from the appearance of the code, you are trying to cd to a file rather than a folder, but i am not sure that is really what you want to do, if you want to just call the batch file then as you are supplying a full path you should not need to cd
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: shell command help

    Agree with west....appears you are attempting to change directory to a filename...

    The code you say works, I doubt it.

    The code you say does not work, should (if text1.text is a valid directory)

    If all you want to do is run your batch file (located in the directory as stated in your text1.text), then simply:

    Code:
    Private Sub Command1_Click()
        Text1.Text = "c:\Users\leon\desktop\newfolder\"  'i put this here to show what your text1.text MIGHT look like (not needed in this button click)
        Shell Text1.Text & "\test.bat"
    End Sub

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: shell command help

    The first code probably does work, because && is the command separator in DOS, so the line contains two commands, cd and executing the batch file.

    The second code may not be passing the command string properly, so the cmd processor isn't interpreting the "&&", i.e. it may be inside quotes so being prevented from parsing. I'll have to try it out to see what's what.

    p.s. I tried it out and it worked for me, in both cases. I'm not sure why it didn't work for you. I did use a simpler path though, but that shouldn't have made a difference.
    Code:
    'Textbox1 contained C:\c&&test.bat
    Option Explicit
    
    Private Sub Command1_Click()
      Shell "cmd /k ""cd " & Text1.Text & """"
    End Sub
    The batch file was C:\c\test.bat

    The contents of the batch file were
    Code:
    dir >d1.lst
    notepad d1.lst
    It gave me a list of the C:\c directory in the file, which verified the cd did take effect before the batch file ran.
    Last edited by passel; Oct 6th, 2019 at 09:50 AM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2019
    Posts
    194

    Re: shell command help

    Quote Originally Posted by passel View Post
    The first code probably does work, because && is the command separator in DOS, so the line contains two commands, cd and executing the batch file.

    The second code may not be passing the command string properly, so the cmd processor isn't interpreting the "&&", i.e. it may be inside quotes so being prevented from parsing. I'll have to try it out to see what's what.

    p.s. I tried it out and it worked for me, in both cases. I'm not sure why it didn't work for you. I did use a simpler path though, but that shouldn't have made a difference.
    Code:
    'Textbox1 contained C:\c&&test.bat
    Option Explicit
    
    Private Sub Command1_Click()
      Shell "cmd /k ""cd " & Text1.Text & """"
    End Sub
    The batch file was C:\c\test.bat

    The contents of the batch file were
    Code:
    dir >d1.lst
    notepad d1.lst
    It gave me a list of the C:\c directory in the file, which verified the cd did take effect before the batch file ran.
    after executing the bat i have inside .bat
    del name.bat
    exit
    so the cmd stays open i need to kill cmd window and kill the bat file because its not needed no more ?

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: shell command help

    WHAT, may I ask, are you trying to do...the ultimate goal?

  7. #7
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: shell command help

    For whatever purpose you are doing this, the Exit command should close the CMD window as long as it is the last thing run in your (many) batch files.

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: shell command help

    /k specifies to keep the command window open
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: shell command help

    If you don't want to leave the command window open, then use /c not /k. /k is specifically to keep the command window open after the command is executed. /c will execute the command and the window will close when the command completes.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

Posting Permissions

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



Click Here to Expand Forum to Full Width