INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

problem converting batch file into VBA script

problem converting batch file into VBA script

(OP)
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 :(

RE: problem converting batch file into VBA script

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

RE: problem converting batch file into VBA script

(OP)
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

RE: problem converting batch file into VBA script

Awesome! Glad it worked out.

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close