Why does ftp not work with concatenated strings? - vba

I have been scratching my head for hours on this one. I have a VBA macro that generates a text file containing a list of ftp commands that I will use with "ftp -s:". So far so good. However ftp does something very weird and splits a concatenated string an place it at the beginning of the line. I will give an example to better illustrate this. The following is the structure of the text file:
myUserName
myPassword
get myFile.zip
close
The characters in bold indicate where the concatenation occurred using Print #1, "get " & fileName & ".zip"
So when I run ftp -s:Commands.txt myftp.me.com i am able to login fine, except when ftp gets to third line with the get command. The ".zip" is split and placed in the beginning of the command line and basically executes .zip get myFile
I do not see this when I manually type the commands into a text file.
Any ideas on what might be happening? Thanks in advance.

I do not know, why this should not work, but what you may try, is to add quotes " " around the file names. normally this should work without quotes if you have no spaces in the filename, but I would give it a try.
Print #1, "get """ & fileName & ".zip"""

Related

Running .exe directly & through Shell VBA returning different results

When I click directly in the .exe file "PrintUsers.exe", the output is correct.
But when I do that through VBA using Shell the result is different. It tries to find the text file in another directory. Why? See figure:
SOLUTION: I am now using: GetModuleFileName(NULL, szEXEPath, 2048) instead of GetCurrentDir(buff, FILENAME_MAX);
It appears that PrintUsers.exe expects to find the file doNotEdit.txt in the current directory.
The best solution is to change that program to look for the file in the same directory as the program itself is located but, if that is not possible, get Excel to change the current directory before running the program, i.e. insert
ChDir ActiveWorkbook.Path
prior to invoking Shell.
Also, as Yahya Hussein mentioned in a comment, spaces inside paths can cause issues. There aren't any in your specific situation but, to ensure you don't have problems in future, consider using something like
myFile = """" & ActiveWorkbook.Path & "\PrintUsers.exe"""
ChDir ActiveWorkbook.Path
Shell myFile, vbNormalFocus
SOLUTION: I am now using: GetModuleFileName(NULL, szEXEPath, 2048) instead of GetCurrentDir(buff, FILENAME_MAX);

Writing out a quoted variable

I"m struggling with some code that writes out a VBS file, for a particular line that writes a file path which holds a variable. Struggling with getting the double-quotes right and placing variable correctly.
This line currently works, but only if "C:\tmp" dir is already created:
Print #PayLoadFile, "HTTPDownload ""http://host.example.net/test1.exe"", ""C:\tmp"" "
Instead of C:\tmp, I would like to write to user profile directory. But I can't get the part to write out correctly in VBS file. I'm trying this:
Print #PayLoadFile, "HTTPDownload ""http://host.example.net/test1.exe"", ""Replace(myFile)"" "
The 'myFile' variable holds a string of the user profile directory ("c:\Users\John Doe")
It should print out to the vbs file as:
HTTPDownload "http://host.example.net/test1.exe", "C:\Users\John Doe"
But instead it looks like this:
HTTPDownload "http://host.example.net/test1.exe", "Replace(myFile)"
You need to concatenate the value of myFile into the string:
Print #PayLoadFile, "HTTPDownload ""http://host.example.net/test1.exe"", """ & myFile & """"

How to concatenate files by calling Windows Shell with VBA

I am trying to write some VBA in Excel that will concatenate .csv files by executing the copy and more commands in the Windows Shell.
This code successfully creates an empty file concat_target.txt (into which the others will be concatenated):
Shell ("cmd.exe copy /b NUL " & """\\path\to\network\folder\concat_target.txt""")
However, this code does not have the intended effect of saving file_1.txt as file_1.concat_temp (skipping the header):
Shell ("cmd.exe more +1 " & """\\path\to\network\folder\file_1.txt""" & " > " & """\\path\to\network\folder\file_1.concat_temp""")
In fact, every variation of the call to more that I have tried just pops up a console (that stays open) whereas the call to copy does not. The UNC network path has cursed spaces in it.
UPDATE:
It turns out that adding the /c flag to the more command does the trick (although it seems to have no effect on the call to copy). So this code is having the desired effect:
Shell ("cmd.exe /c more +1 " & """\\path\to\network\folder\file_1.txt""" & " > " & """\\path\to\network\folder\file_1.concat_temp""")
I will post the full solution for posterity once it is finished.
I don´t know why you are using 'more', but I would use other methods:
Shell ("cmd.exe /c type ""\\path\to\network\folder\file_1.txt"" >> ""\\path\to\network\folder\file_1.concat_temp""")
Notes:
redirecting with '>' will overwrite the file, losing previous contents
redirecting with '>>' will append the file (concatenate)
no need to separate every element in your string with " & " if they are all text (but I guess you already knew this)
no need to create an empty file first.
UPDATE:
OP was using 'more' to exclude the .csv table headers, as I hadn´t understood before.
So the 'type' command won´t work in this case.

Is a .txt file created in VB different than one I'd randomly create?

So, basically, without having any Visual Studio experience, I wanted to improve my animation workflow. So, I wanted to use stuff found in http://www.aseprite.org/cli/ to do that.
So, I made a little program to get all .ase files in a folder and make a text file containing the lines needed for the batch file to convert the said .ase files to either .gif or png files. I'm stupid, so I manually convert the text file to a .bat file.
But the .txt files generated from the program do not work when converted to .bat, but if I copy the lines to another .txt file I create in windows, and convert it to .bat, it works.
So the question is - do I corrupt the .txt file in Visual Studio or something?
If there's a problem with the code, I'll share it, but I'm reluctant, cause I don't want to have a formatting mistake nightmare.
Oh, and yeah, I know this isn't a smart way to go about.
EDIT: Ah, right, the error of the batch file, which, by the way, flashed by extremely quickly and closes the command line prompt: '--batch' is not recognized as an internal or external command, operable program or batch file.
The code in .bat is like:
#set ASEPRITE="C:\Program Files\Aseprite\aseprite.exe"
%ASEPRITE% --batch animation.ase --scale 2 --save-as animation-x2.gif
%ASEPRITE% --batch animation.ase --scale 4 --save-as animation-x4.gif
And basically in VB I create the txt file and text like this:
' Dim fs As FileStream = File.Create(Label1.Text + "\Text.txt")
fs.Close()
For Each foundFile As String In My.Computer.FileSystem.GetFiles(Label1.Text)
Dim check As String = System.IO.Path.GetExtension(foundFile)
If check = ".ase" Then
ListBox1.Items.Add(Dir(foundFile))
str = Dir(foundFile)
str = str.Replace(".ase", fType)
My.Computer.FileSystem.WriteAllText(Label1.Text + "\Text.txt",
"%ASEPRITE% --batch " + Dir(foundFile) + " --scale " + fSize + " --save-as " + str + vbCrLf,
True)
End If
Next
'
You can try to write the file with a specific encoding:
File.WriteAllText(path, text, Encoding.UTF8) ' or UTF or ASCII
It could also be that the UTF8 encoder uses a Byte Order Mark which can't be read:
File.WriteAllText(path, text, new UTF8Encoding(false))
The other thing to look for is end of line characters. Is it \r\n or just \n?
You can use a file comparer to check the difference between the files, and it should become clear what the difference is.

FileSystem.WriteAllText adds non-printable characters

Here are two methods for writing text to a file in VB.Net 2012. The first one prepends the same three non-printable characters to each file: . The second one works as expected and does not add the three characters. objDataReader is an OleDB datareader.
Any idea why?
Greg
My.Computer.FileSystem.WriteAllText(lblLocation.Text & "\" &
objDataReader("MessageControlId").ToString & ".txt", objDataReader("MsgContents").ToString, False)
Using outfile As New StreamWriter(lblLocation.Text & "\" & objDataReader("MessageControlId").ToString & ".txt")
outfile.Write(objDataReader("MsgContents").ToString)
End Using
Thanks. I found the entry below I after Googled BOM, in case anyone wants a more detailed explanation. While the BOM was not visible in a text editor it did cause problems when I passed the file to our HL7 interface engine.
Greg
Write text files without Byte Order Mark (BOM)?