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.
Related
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);
Upload file via FTP from Excel VBA was very helpfull, I'm using the code to synchronize a local folder to my FTP server.
Call Shell( _
CurrentProject.Path & "\WinSCP.com /log=" & CurrentProject.Path & "\ftp.log /command " & _
"""open ftp://user:pass#ftp.server.com/"" " & _
"""synchronize local " & localfolder & " /www/remotefolder/wines -filemask=""*.png"" " & _
"""exit""")
I'm trying to issue an exit command at the end, but the code gives me a
Too many parameters for command 'synchronize'.
The line in the log tells me
synchronize local C:\localfolder\wines /www/remotefolder/wines -filemask=*.png exit
The exit is in the same line as the synchronize one, when I use the put script this doesn't happen. What can I do to prevent this?
You are missing closing quote after the synchronize command. And the quotes around -filemask value have to be doubled. Or actually, you do not need them, as the value does not contain spaces.
This will do:
"""synchronize local " & localfolder & " /www/remotefolder/wines -filemask=*.png""" & _
See https://winscp.net/eng/docs/commandline#syntax
The symbol you have at the beginning of —hostkey and —rawsettings is not a simple hyphen-minus (-), but em-dash (—).
Please use hyphen-minus (-) — what is the dash that you find on the standard English [and other] keyboards.
So actually you have the very same problem as in WinSCP forum post you referred to.
Or even easier, have WinSCP GUI generate a script template for you.
Also see FAQ Why are some WinSCP scripting commands specified in a batch file not executed/failing?
Other questions with the same error message, but different problem:
WinSCP command line - Too many parameters for command 'open' when using -rawtransfersettings switch
WinSCP "Too many parameters for command 'open'." error
FTP "Too many parameters for command 'synchronize'" with WinSCP
PROBLEM IS SOLVED ALREADY
this should be fairly simple, but I can't figure out what's wrong.
I have a cmd file in V:\something\XYZ.cmd, which takes 1 parameter.
When I execute it manually, e.g. Windows-Explorer and double-click the cmd, I get my result.
Now I have a XLSM file on my Desktop and a macro should invoke this cmd instead.
Problem is, when executed that way, I get some "file-not-found errors" in the cmd itself.
So how could I simulate the manual execution of the cmd.
There must be some path related problem...
This is how I execute from VBA:
Call Shell("cmd.exe /c " & "V:\something\XYZ.cmd" & " " & someParameter, vbNormalFocus)
I tried to put a
ChDir "V:\something\"
right before the call, but that doesn't change anything...
Where's the problem?
thank you, I just found the error myself:
There was a %root% used inside the script, this was the error. Changed it to absolute path, now it works
so I'm trying to get a very simple shell command to execute a batch file that's created, but for some reason the shell command doesn't do anything.
The batch file gets created and when I double click on the batch file it executes the script just fine, but if I try to call the shell command in VBA it appears that it tries to open a command window, but immediately closes it after, not producing any errors. This code used to work fine before I reformatted this laptop AND this code works on another system running Server 2012 R2 (I'm running windows 10)
Dim SedjaShell
f = "C:\Temp\_SedjaScript.bat"
Open f For Output As #1
Print #1, Chr(34) & SedjaDir & Chr(34) & " merge --files " & Page1 & " " & Page2 & " " & TOS & " " & Disclosure & " --output " & OutputDir
Print #1, "Pause"
Close #1
SedjaShell = Shell("C:\Temp\_SedjaScript.bat", 1)
Is there something I might be missing?
EDIT
Adding the contents of _SedjaScript.Bat
"C:\Users\User\Desktop\DealTracker\MergeFiles\sejda-console-3.0.4\bin\sejda-console.bat" merge --files C:\Temp\ContractSummary.pdf C:\Temp\Page2.pdf C:\Temp\TOS.pdf C:\Temp\Disclosure.pdf --output "C:\Users\User\Desktop\DealTracker\Completed Contracts\Company-Name-Inc-07-19-2017-Contract.pdf"
Pause
Thanks to Andre I was able to get the script to work inside VBA's Shell command.
By setting the PATH for my JRE to JAVA_HOME I was able to successfully launch the script.
setx JAVA_HOME "C:\Program Files (x86)\Java\jre1.8.0_141"
setx PATH "%PATH%;%JAVA_HOME%\bin";
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"""