writing a batch file

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?

Who is Participating?

Improve company productivity with a Business Account.Sign Up

wsh2Connect With a Mentor Commented:
<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

Could you post your existing code so we can check it out?

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.

Public Function WriteFile(pstrFileName As String, pstrData As String) As Boolean
    Dim intFile As Integer

    intFile = FreeFile

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

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

    Close intFile
End Function

Hope this helps
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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--

---End File---

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

--Start File--
---End File---

Hope this helps!
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
I think using filesystemobject are much better than open for output etc.

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

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

There is more code for append, extract etc.
>>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...
mwamAuthor Commented:
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
"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

thanks, mike
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
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.
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.
>>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?

mwamAuthor Commented:
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
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>.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.