Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem converting batch file into VBA script 1

Status
Not open for further replies.

rob_nye

MIS
Dec 14, 2016
2
GB
Hello,

I have been working on calling a batch file from with a Access database report - using VBA

I completed this successfully, but now want to convert the script to run within the VBA code itself (this would help avoid the possibility of the batch file getting deleted)

Batch script - called from VB code with

Call shell(Environ$("COMSPEC") & " /c c:\Access\Rob\split_File.bat BTN", vbNormalFocus) ' run script to split the TT-NONSTG file into separate files

Code:
@echo off
cd c:\Access\Rob\TTFILS\%1

del TT-NONSTG-*.bak TT-NONSTG.old
ren TT-NONSTG-*.txt TT-NONSTG-*.bak
	

for /f "tokens=1-16 delims=," %%d in ('type "TT-NONSTG.txt"') do (
>>"TT-NONSTG-%%g.txt" echo(%%d,%%e,%%f,%%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n,%%o,%%p
)

move /y TT-NONSTG.txt TT-NONSTG.old

Ive worked out most of the commands for VB apart from the splitting of the files

VB code
Code:
Sub SplitFiles()
Dim DelVal As Variant
' delete old .bak files
'
ChDir "c:\Access\Rob\TTFILS\BTN"         'Change folder to desired folder
DelVal = Dir("*.bak")       'Get first file in folder
Do While Len(Nz(RetVal)) > 0   'Delete until no more files
  Kill PATHTOTTFILEbtn & DelVal
  RetVal = Dir()            'Get next file to rename
Loop

' delete old .old file
'
Kill PATHTOTTFILEbtn & "TT-NONSTG.old"

' rename old .txt files as .bak
'
Dim RetVal As Variant
RetVal = Dir("*.txt")       'Get first file in folder
Do While Len(Nz(RetVal)) > 0   'Rename until no more files
  Name RetVal As Left(RetVal, Len(RetVal) - 3) & "bak"  'Rename
  RetVal = Dir()            'Get next file to rename
Loop

' split the original file
'
for /f "tokens=1-16 delims=," %%d in ('type "TT-NONSTG.txt"') do (
>>"TT-NONSTG-%%g.txt" echo(%%d,%%e,%%f,%%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n,%%o,%%p
)

' rename non split file to .old
'
Name PATHTOTTFILEbtn & "TT-NONSTG.txt" As PATHTOTTFILEbtn & "TT-NONSTG.old"

End Sub

but im stumped as to how to code this :(

 
Off-hand, I don't think you can do file splitting directly in VBA. However, you could have a look at the FileSystemObject to see.

Why can't you create the batch file in the code, then run the batch file, then remove it after completed?

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
kjv1611 - your a star

I have been grappling with trying to rewrite the .bat commands into VB script code with a lot of agro.

30 mins after seeing your update, I now have the code creating the .bat file and running it successfully - then deletes the .bat files - so problems at the users end

many thanks
 
Awesome! Glad it worked out.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top