Solved

writing a batch file

Posted on 2000-02-28
13
503 Views
Last Modified: 2012-08-14
hello,
having trouble opening a file for output
and writing batch file commands, because
of character text formatting problems.
( i think it always adds a carraige
return or line feed to the last line )

how do i get around this so no extra
text formatting characters are added?

thanks
0
Comment
Question by:mwam
  • 3
  • 3
  • 2
  • +4
13 Comments
 
LVL 2

Expert Comment

by:jkpcs
Comment Utility
Could you post your existing code so we can check it out?
0
 
LVL 1

Expert Comment

by:Ask_ED
Comment Utility
Hi,

If you are using the Print statement then it will always append a vbCRLF to the string being added to the file.

With the following code you can preformat a string to the file contents you desire and then write it to the file. Note you may have to delete the previous file with a "KILL" command before calling this routine.

<PRE>
Public Function WriteFile(pstrFileName As String, pstrData As String) As Boolean
    On Error GoTo WRITEFILE_ERR
    Dim intFile As Integer

    intFile = FreeFile

    Open pstrFileName For Binary Access Write As #intFile
    Put #intFile, 1, pstrData

WRITEFILE_ERR:
    If Err.Number <> 0 Then
        WriteFile = True  ' An error condition
        Resume WRITEFILE_ERR1
    End If
WRITEFILE_ERR1:
    On Error Resume Next

    Close intFile
End Function
</PRE>

Hope this helps
ED
0
 
LVL 5

Expert Comment

by:KDivad
Comment Utility
I think that adding a ; to the end of the print line will stop the carriage return.

Print #1, "This"
Print #1, "That"

--Start File--
This
That

---End File---

Print #1, "This"
Print #1, "That";

--Start File--
This
That
---End File---

Hope this helps!
0
 
LVL 3

Expert Comment

by:vikiing
Comment Utility
In fact, batch files NEED <CR><LF> terminators to recognize each separate line of commands.

Each time you execute the PRINT command, it adds a line separator (the combination of <CR> and <LF>, in that order), EXCEPT when that PRINT command ends with a semicolon.

KDivad's comment is right, but if you try to read a so-generated text file (as a batch command file is) with some older Basic programs, the last line, as it has *NO* line terminator, won't be properly read, because the last "t" (of "That") is not followed by a line terminator; as <EOF> is only there, program will fail to recognise the line, and perhaps:
a) a premature EOF condition may be detected
b) the program hangs at INPUT instruction
0
 
LVL 2

Expert Comment

by:damienm
Comment Utility
I think using filesystemobject are much better than open for output etc.

Command1_Click()
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile(App.Path & "\test.txt", True)

a.WriteLine "Testing"
a.WriteLine "123"
a.close
end sub

There is more code for append, extract etc.
0
 
LVL 3

Expert Comment

by:vikiing
Comment Utility
>>I think using filesystemobject are much better than open for output etc

¿Could you tell me please why? Just only for to satisfy my curiosity...
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:mwam
Comment Utility
while ed's answer may work...kdavid's
answer is much better. damienm's answer
didn't have any effect. now i'm confused, tho, cause the lack of the
final <CR><LF> wasn't the answer to my
batch file woes. here's what i'm trying
to do: make a bat file that runs dos
ftp and reads from a text file. here's
the code:
Private Sub Command2_Click()
On Error Resume Next

Dim wrap As String
Kill "c:\windows\temp\ftpbatch.bat"
Kill "c:\windows\temp\ftpget.txt"

wrap = Chr(13) & Chr(10)
scriptline = "open " & ftpsite & " " & ftpport & wrap & username & wrap & password & wrap & "binary" & wrap & "lcd temp"

Open "c:\windows\temp\ftpget.txt" For Output As #1
Print #1, scriptline
Print #1, "dir * dir.txt";
Close #1

Open "c:\windows\temp\ftpbatch.bat" For Output As #1
Print #1, "ftp -s:c:\windows\temp\ftpget.txt";
Close #1
Debug.Print scriptline

End Sub

not sure how that will work with the small input text box here on this webpage. the idea is to make a one line
textfile called "ftpget.txt" and a have
the batch file read from that. the batch file works, actually, but gives errors: "parameter value not in allowed
range"
"bad command or filename"
it also does the "dir * dir.txt" command
twice. the batch file actually works,
would just like to understand why it's acting funny and would like to clean it
up.

thanks, mike
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
mwam.. Your VB code is just fine.. (although you really should consider Option Explicit declarations.. <wink>).
Both the Batch file and the Scriptfile are being written fine.. <smile>.

The problem you are having is the actual script you are passing to FTP.
Right now, it reads..

open ftpsite ftpport
username
password
binary
lcd temp
dir * dir.txt

and even without knowing what DOS FTP Script should look like.. this looks very fishy. Positional line delineation for username and password parameters? ... I don't think so !!!

Anyhow.. let me go and research what the script file contents should be. In the meantime, I suggest that you MANUALLY create a script and BAT file, and test it.. after you get something that works.. then adjust your code accordingly.. <smile>.

Once again, there is NOTHING wrong with your program.
0
 
LVL 14

Accepted Solution

by:
wsh2 earned 50 total points
Comment Utility
<drum roll>.. TADA.. This BABY works !!!! The only change I made was a cosmetic one (using vbCrLf rather than wrap).

The FTP.EXE Tool is VERY sensitive.. one error in your script and KABOOM.. invalid commands out the Kazoo !!! Commands are case sensitive (lower), and you MUST relationally follow any server request with the CORRECT response (my apologies for my earlier username, password comment.. <sheesh>).. or your whole stinking script abends. Anyhow.. copy and paste the following.. and then you can change it as you see fit.. <smile>.

Oh.. one other note.. The MS knowledge base has one or two reports on FTP.EXE.. whether they apply to you or not who knows?.. But, you may want to check then out. Type in FTP.EXE as your search.

<--------- Code ------------>

On Error Resume Next

Dim strFtpSite As String
Dim strFtpPort As String
Dim strPassword As String
Dim strScriptLine As String
Dim strUserName As String

strFtpPort = "21"
strFtpSite = "ftp.sec.gov"
strUserName = "anonymous"
strPassword = "me@test.com"

Kill "c:\windows\temp\ftpbatch.bat"
Kill "c:\windows\temp\ftpget.txt"

strScriptLine = "open " & strFtpSite & " " & strFtpPort & vbCrLf _
    & strUserName & vbCrLf _
    & strPassword & vbCrLf _
    & "binary" & vbCrLf _
    & "lcd c:\windows\temp"

Open "c:\windows\temp\ftpget.txt" For Output As #1
Print #1, strScriptLine
Print #1, "dir * dir.txt";
Close #1

Open "c:\windows\temp\ftpbatch.bat" For Output As #1
Print #1, "ftp -s:c:\windows\temp\ftpget.txt";
Close #1

Debug.Print strScriptLine

Shell "c:\windows\temp\ftpbatch.bat", vbNormalFocus

0
 
LVL 2

Expert Comment

by:damienm
Comment Utility
To Viking

I think it is better because it does not need chr(13) chr(10) etc.

I think it reads code better also, sometimes when I am trying to read only 1 line from a text file using batch code it reads the whole document instead of just 1 line at a time.
0
 
LVL 3

Expert Comment

by:vikiing
Comment Utility
>>I think it is better because it does not need chr(13) chr(10) etc.

Note that when you work with Print (even with Write) statements, you disregard of the use of CHR(13/10) (althoug both instructions add the proper line terminator, even when you don't specify it [but Write has NO WAY to disable it]).

Now, provided that WriteLine method acomplishes THE SAME job (to add a line with its own line terminator), ¿where is the difference?

0
 

Author Comment

by:mwam
Comment Utility
well,
used wsh2's code...got exactly the
same results. yes it works, but
getting a couple error msgs and
mysteriously preforms the:
"dir * dir.txt" command twice.
closing the question...thanks
to all who commented :)

mike w
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
mwan.. On the dir * dir.txt problem, are you sure the server isn't just echoing the command back? If it isn't, as that is the last line you put into the buffer, it doesn't surprise me that FTP.EXE could send it twice (perhaps as a result of inactivity). Don't forget to add FTP.EXE "close", "disconnect" and/or "bye" statements to the end of your script as appropriate, so that you cleanly exit the session.

Another thought.. add a kill on the dir.txt file in the beginning of the procedure. When the FTP.EXE bat finishes, you can then existence test for the dir.txt file to see if the BAT completed.

As stated earlier.. FTP.EXE is VERY finnicky.. the smallest mistake.. and you are off to never never land. Your efforts may be better served by doing a WIN FTP application.. where you can intercept FTP server responses and take appropriate action.. <smile>.

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

728 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

9 Experts available now in Live!

Get 1:1 Help Now