Shell Window not Closing

Posted on 2009-12-18
Last Modified: 2012-05-08
Hi Experts,

I'm using "Shell" in VBA to merge multiple CSV Files together but the CMD window isn't closing.
it does the job though and my macro then continues to Import the Merged file.

And how would i make it close automatically?
Sub Test()
Dim comStr as String
comStr = "CMD /k Copy " & Chr(34) & strDir & "\*.csv" & Chr(34) & " " & Chr(34) & "C:\all.txt" & Chr(34)
Call Shell(comStr)
End Sub

Open in new window

Question by:bromy2004
    LVL 81

    Assisted Solution

    by:zorvek (Kevin Jones)

    comStr = "CMD /k Copy " & Chr(34) & strDir & "\*.csv" & Chr(34) & " " & Chr(34) & "C:\all.txt" & Chr(34)


    comStr = "CMD /C Copy " & Chr(34) & strDir & "\*.csv" & Chr(34) & " " & Chr(34) & "C:\all.txt" & Chr(34)

    LVL 4

    Accepted Solution


    Zorvek response is right but I think a little explanation would have been welcome:

    The two involved CMD.exe switches here are:

    /C      Carries out the command specified by string and then terminates
    /K      Carries out the command specified by string but remains

    So using /K, it is the normal behavior of the prompt to remain open after the command as been carried out.

    Hope this will clarify the point.
    LVL 10

    Author Closing Comment

    I've given Zivko the majority because he was clear with his explanation.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
    This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
    This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
    This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now