Code to automate CMD steps in VBA

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

Manually, I can do:

Windows, launch CMD.exe (Start->Run cmd Enter)
[In DOS prompt] "cd /d D:\Serious\Work\Other People\Al\DataRead"
[DOS] "type *.csv > x.txt"

This creates concatenated text file of all csv files in folder.

If I run below VBA, I receive error: "The system cannot find the path specified."
Code:
Sub testme()

Dim s   As String: s = Range("Folder").Offset(, 1).Text

'1: vbNormalFocus
Call Shell("cmd.exe /k cd /d " & s, 1")

End Sub
Where Range("Folder").text contains a folder path ending with ""

Please someone help with code to automate these steps?

Ultimately, it will be used to concatenate CSV files in a DropBox folder, then write x.txt to Excel and analyse results.

TIA,
JackDanIce
 
Last edited:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
you have three parenthesis in the call, they have to be paired?
 
Upvote 0
Apologies, meant:
Code:
Sub testme()

Dim s   As String: s = Range("Folder").Offset(, 1).Text

s = "cmd.exe /k cd /d " & s

'1: vbNormalFocus
Call Shell(s & ", 1")

End Sub
 
Last edited:
Upvote 0
Hi Tetra201, thank you that works and changes the directory. Final part, how do I add "*.csv > x.txt" ?

Trying this errors "The directory name is invalid"
Rich (BB code):
Sub testme()

Dim s   As String: s = Range("Folder").Offset(, 1).Text
s = "cmd.exe /k cd /d " & s & "*.csv > x.txt"

'1: vbNormalFocus
Call Shell(s, 1)

End Sub
I'm trying to avoid using a .bat file or creating a temporary one to run 2 commands and then delete it, any suggestions please?
 
Last edited:
Upvote 0
Got it:
Rich (BB code):
Sub testme()

Dim s   As String: s = ThisWorkbook.Sheets("Main").Range("Folder").Offset(, 1).Text
s = "cmd.exe /k cd /d " & s & "& type *.csv > x.txt"

'1: vbNormalFocus
Call Shell(s, 1)

End Sub
Part in red was missing, thank you both @mole99 and @Tetra201!
 
Last edited:
Upvote 0
One more related question - how can I tell when the output file x.txt is complete?

I'm using an arbitary 5 second wait here but any other suggestions:
Rich (BB code):
Private Sub Create_txt_File(ByRef strFilePath As String)

    Dim s   As String
    s = "cmd.exe /k cd /d " & strFilePath & "& type *.csv > x.txt"
    
    '1: vbNormalFocus
    Shell s, 1
    
    Do
        Application.Wait Now + TimeSerial(0, 0, 5)
    Loop Until Len(Dir(strFilePath & "x.txt"))
    
End Sub
This isn't ideal because it tests only if the file exists not whether the operation is complete.

Later, I'll add code to kill the cmd window and x.txt file.
 
Last edited:
Upvote 0
Maybe
Code:
Private Sub Create_txt_File(ByRef strFilePath As String)

    Dim s   As String
    s = "cmd.exe /k cd /d " & strFilePath & "& type *.csv > x.txt [COLOR=#ff0000]& echo 0 > Done.txt[/COLOR]"
    
    '1: vbNormalFocus
    Shell s, 1
    
    Do
        Application.Wait Now + TimeSerial(0, 0, 5)
    Loop Until Len(Dir(strFilePath & "[COLOR=#ff0000]Done.txt[/COLOR]"))
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top