Solved

writing a batch file

Posted on 2000-02-28
13
508 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
ID: 2567519
Could you post your existing code so we can check it out?
0
 
LVL 1

Expert Comment

by:Ask_ED
ID: 2567543
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
ID: 2568141
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:vikiing
ID: 2568484
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
ID: 2569247
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
ID: 2570289
>>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
 

Author Comment

by:mwam
ID: 2571302
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
ID: 2571513
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
ID: 2571666
<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
ID: 2572059
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
ID: 2572324
>>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
ID: 2572474
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
ID: 2572862
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 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