I have an excel workbook in which there is data from multiple text files.
The text files are created in a .bat script.
I would like to refresh the workbook in the script right after creating the files.
Does anyone know a command that can do that?
Thanks!
For command line refresh (scheduled by Windows task scheduler) I ended up using this open source application: https://github.com/alapolloni/ExcelRefresh. Note that Excel needs to be installed on client for this to work.
You can specify what to update, for example "macros", "pivot tables", "Queries" or simply "all".
Together with this nice tool "WasFile" from https://www.horstmuc.de/wbat32.htm you can check the age to avoid unnecessary excel-refreshes.
For example, a batch file:
set file1="c:\Tmp\refreshMe.xlsx"
ECHO.
echo ----- %File1% -----------------------------
C:\Test\WasFile.exe %file1% modified not after today-1
if %errorlevel%==0 (echo TRUE: Older than
Goto Next1
)
if %errorlevel%==1 (
echo FALSE: Not older
goto Skip1
)
if %errorlevel%==255 (
Echo ERROR
goto Error
)
:Next1
Echo ### Update START ....
C:\test\ExcelRefresh.exe -d -a -f %file1%
Echo ### Update END ....
echo.
I don't think excel has CLI, but you can use VBS - there is example of importing data from excel spreadsheet using vbs: http://www.gregthatcher.com/Papers/VBScript/ExcelExtractScript.aspx but it's possible to do this in reverse direction too.
Related
Usually I get an excel spreadsheet with dozens of filenames, for which I then need to go and search individually.
Spreadhseet
Is there a way that I could simply:
Select All filenames in e.g. row A of Excel,
then Search for all these files on "This Mac"
then Copy all found files into the New Folder on the Desktop
So far I've tried the first part of searching and this is what i get :a)
Automator with Variable. But the problem is, it only searches for 1 file from selection
b)
Automator with Shell Script (Copy to Clipboard > Open Finder > CMD+F (to highlight Search dialog) > CMD+V). It opens a new Finder window, but it doesn't paste the clipboard into search dialog
c) /usr/bin/pbcopy
on run {input, parameters}
tell application "System Events"
keystroke "f" using {command down}
keystroke "v" using {command down}
end tell
return input
end run`
End result, is same as option b). I was planning to run this in Automator as a 'Service', which I could later assign to Keyboard Shortcut.
I am pretty sure there should be a simple shell option for this - any advice would be much appreciated.
I made a bash script that does what you want. You would basically select a bunch of filenames in Excel, or any other app, and copy them to the clipboard with ⌘C. After that you need to run the script and it will take items from the clipboard and search for TIFF or JPEG images that match that name and copy them to a directory on your Desktop called Selected Files:
#!/bin/bash
# Get contents of clipboard into bash array
files=( $(pbpaste) )
# Create output directory - no checks for already existing or already containing files
OUTDIR="$HOME/Desktop/Selected Files"
mkdir -p "$OUTDIR"
# Iterate through fetching files
for ((i=0;i<${#files[#]};i++)) ; do
name=${files[i]}
result=$( mdfind "kMDItemDisplayName == \"${name}.*\" && (kMDItemKind==\"TIFF image\" || kMDItemKind==\"JPEG image\")" )
if [ -f "$result" ]; then
echo $name: $result
cp "$result" "$OUTDIR"
else
echo ERROR: Searched for: $name, found $result
fi
done
I am not sure of your level of familiarity with bash, so you may be able to ignore the following...
Make a new directory for your own scripts:
mkdir -p $HOME/scripts
Save the above script in that directory with filename:
$HOME/scripts/gather
Make the script executable by typing this into Terminal:
chmod +x $HOME/scripts/gather
Edit your login profile ($HOME/.profile) and add your $HOME/scripts directory to your PATH:
export PATH="$PATH":$HOME/scripts
Then start a new Terminal and you can use any script that you have saved in $HOME/scripts without needing to specify the full path to it, e.g.:
gather
Following information kindly contributed by #user3439894 in comments section, as I am out of my depth on this aspect...
To use a keyboard shortcut, you'd have to create an Automator "Service workflow" with a "Run Shell Script" action, which you can assign a keyboard shortcut to under: System Preferences > Keyboard > Shortcuts > Services
Question Summary:
Can I start Excel file Installer.xlsm from Windows batch script in safemode, without providing EXCEL.EXE installation path?
Details
I have a windows batch script which downloads the latest versions of a family of Excel Add-ins from a remote server, places them in a directory (C:\appname\AddIns) and calls the Excel file Installer.xlsm.
Upon loading, Installer.xlsm executes a VBA macro, which uninstalls older versions of the add-ins and installs their newer version.
Currently I start Installer.xlsm using the command:
start "Launching installer file" /wait "<Path to file>\Installer.xlsm"
What's great about it is that it uses Windows' file association to open Excel, and I don't have to provide the EXCEL.EXE installation path (multiple users with different machine images and MS Office versions).
Now I'd like to load Installer.xlsm in safemode, to make sure that no add-ins are loaded and no other code is run while Installer.xlsm tries to work with the add-ins.
I know I can use "<PathToExcel>excel" /safemode "<PathToXls>Installer.xlsm" as described in this answer, but this method doesn't use Windows' file association and requires that I provide a path.
We have users with various machine images, using different versions of MS Office, so I do not want to get into hardcoding all possible Excel installation locations.
Can I do something of the following form:
start "Launching installer file" /wait "<Path to file>\Installer.xlsm /safemode"
I tried different possible combinations without success. How would you do it?
First I suggest to read the Microsoft documentation page Application Registration. It explains how the installer of an application or an application suite like Microsoft Office should register the installed application(s) so that the executable(s) of the application(s) can be found by other applications.
Recommended is creating under registry key
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
a subkey with name of the executable file like excel.exe with default string value being name of the executable with full path and optionally adding one more string value with name Path containing just the path to the executable. The Path string can but most not exist and it can but must not end with a backslash.
The command START uses also this key to find an application as explained in answer on Where is “START” searching for executables?
The installers of the various versions of Microsoft Office register excel.exe key under this key too.
So the easiest method on Windows Vista and later Windows versions to get installation location of Microsoft Excel is:
#echo off
for /F "skip=1 tokens=2*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe" /ve 2^>nul') do set "ExcelApp=%%~B"
echo ExcelApp=%ExcelApp%
pause
But on Windows XP the output of reg.exe is different and requires for that reason this batch code:
#echo off
for /F "skip=3 tokens=3*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe" /ve 2^>nul') do set "ExcelApp=%%~B"
echo ExcelApp=%ExcelApp%
pause
The different outputs are explained in answer on Read words separated by space & string value also contains space in a batch script in batch code written to get string value of a default string of a registry key containing spaces.
And it is good coding practice to add extra code which handles an error case like registry key does not exist at all because Microsoft Excel is not installed at all.
But is it possible with batch code to do what command START respectively the Windows shell function ShellExecuteEx does on using in a command prompt window the command line?
start "Launching installer file" "C:\Path to file\Installer.xlsm"
Yes, it is possible as the commented batch code below demonstrates.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem First query default string value of HKEY_CLASSES_ROOT\.xlsm from registry.
call :GetDefaultRegValue "HKCR\.xlsm"
rem Is there no key HKEY_CLASSES_ROOT\.xlsm or was the default string empty?
if not defined RegValue goto GetFromAppPaths
SET RegValue
rem Get the shell command line used for opening a *.xlsm file.
call :GetDefaultRegValue "HKCR\%RegValue%\shell\open\command"
rem Could the command line not read successfully from Windows registry?
if not defined RegValue goto GetFromAppPaths
SET RegValue
rem The command line contains as first string usually enclosed in double
rem quotes EXCEL.EXE with full path enclosed in double quotes. And there
rem can be even more arguments on the command line which are not needed
rem here. The command line below is used to get just first string of
rem the command line which should be EXCEL.EXE with full path.
for %%I in (%RegValue%) do set "RegValue=%%~I" & goto CheckExcelExistence
rem It is not good when both registry queries above fail. This means
rem either Microsoft Excel is not installed at all or a version of
rem Excel is installed which does not support *.xlsm files like Excel
rem of MS Office 2003, MS Office 2000 or MS Office 97.
rem However, perhaps just *.xlsm is not correct registered and therefore
rem get full path to excel.exe from application registration key.
:GetFromAppPaths
call :GetDefaultRegValue "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe"
if defined RegValue goto CheckExcelExistence
echo Failed to determine installation location of Microsoft Excel.
echo/
endlocal
pause
goto :EOF
:CheckExcelExistence
SET RegValue
rem Remove surrounding double quotes if the Excel executable file name
rem read from Windows registry is still enclosed in double quotes.
set "RegValue=%RegValue:"=%"
if exist "%RegValue%" goto :RunInstall
echo Registered "%RegValue%" does not exist.
echo/
endlocal
pause
goto :EOF
:RunInstall
SET RegValue
ECHO start "Launching installer file" /wait "%RegValue%" "%~dp0Installer.xlsm" /safemode
endlocal
goto :EOF
rem This subroutine queries from Windows registry the default string value of
rem the key passed to the subroutine as first and only parameter and assigns
rem this value to environment variable RegValue. Environment variable RegValue
rem is deleted and therefore is not defined after subroutine exits on failure
rem to get the registry value or when the default value is an empty string.
rem This subroutine works for Windows XP and all later versions of Windows.
:GetDefaultRegValue
set "TypeToken=2"
:Reg3Run
for /F "skip=1 tokens=%TypeToken%*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "%~1" /ve 2^>nul') do (
if "%%A" == "REG_SZ" (
if not "%%~B" == "" (
set "RegValue=%%B"
goto :EOF
)
) else if "%%A" == "NAME>" (
set "TypeToken=3"
goto Reg3Run
)
)
set "RegValue="
goto :EOF
This batch code is just a demonstration. It does not start Excel when really found. Instead it just outputs the command line which would start Excel because of ECHO left of start ... in block below label RunInstall.
Further this batch code contains 4 lines with just SET RegValue. Those 4 lines output just the string value queried successfully from Windows registry and stored in environment variable RegValue. Those 4 commands help to understand what happens on execution of the batch file. Those four command lines should be deleted finally from batch file and also the single ECHO written in upper case.
Note: It is quite easy to test what happens if an expected registry key does not exist or its default value is an empty string. Just insert a single character like # before last double quote on a line starting with call :GetDefaultRegValue and the modified registry key is not found anymore.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
reg /?
reg query /?
rem /?
setlocal /?
start /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded reg.exe command line with using a separate command process started in background.
I start Excel from the command line and my add-on does some work. when it's done, I want to return some info to the caller. At least 0/1 for success failure or better also an optional error-message.
By caller I mean the command or process that started Excel. e.g. in a Windows command script I could call excel like this:
Excel.exe SomeWorkbook.xlsx /p C:\Somedir /e
when you call an executable in Windows, it can return an numeric code or set an error.
in a script you could check the result like this:
if %errorlevel% neq 0 (
echo some error occurred...
)
MessageBoxes, etc. are no option, because this whole task should be triggered by another application automatically without any user-interaction.
How can we do that?
You could use the status bar:
Application.StatusBar = “your message here”
As far as I know, the message box requires a button to be clicked: macro will wait...
I ended up using text files: i.e when the add-on finished correctly, it will create an empty file OK.txt and when an error occurred it will create a file named ERR.txt that contains the error-message.
Now it's easy for the calling script to check the result:
OK.txt exists: everything is fine - delete OK.txt
no file exists: a fatal error has happened: show a general error message
ERR.txt exists: an error occured: maybe display the error text (contents of the text-file) to the user, delete ERR.txt
I'm trying to figure out how to add content/code to the end of a .js file that already has code in it using ssh command.
ie....
touch ./ap/includes/ckeditor/ckeditor.js
Maintain current code
echo "add custom end code only"> ./ap/includes/ckeditor/ckeditor.js
sshcommand is used to connect to another server.
What you can do append text to the end of a file is to echo "something" >> /your/file.
So based on your code:
touch ./ap/includes/ckeditor/ckeditor.js
Maintain current code
echo "add custom end code only" >> ./ap/includes/ckeditor/ckeditor.js
^
|_ changed this
By the way, the touch part is unnecessary. When echoing inside the file, the date of the file will be updated. And if file does not exist, it will be automatically created with echo.
I know the title sounds crazy. Anyway, here is my scenario:
I need to create about 500 text files for 500 different files. Each text file will contain the information seen in my example below. Is there an easy way to put this into a single batch file without copy and pasting something 500+ times?
Example of what I am trying to do....
echo ^<filename 1^> >> filename1.txt
echo. >> filename1.txt
echo. >> filename1.txt
echo No OCR Found >> filename1.txt
Using random numbers for the files...
#echo off
set loop=0
:loop
set num=%random%
if exist filename%num%.txt (
echo ^<filename %num%^>
echo.
echo.
echo No OCR Found
) > filename%num%.txt else (
goto loop
)
set /a num+=1
if %loop%==500 goto end
goto loop
:end
NOTE:
The maximum amount of files is 32767.
To change the amount of files made, change the number in the last if statement (E.g: To make it create 80 files you would change if %loop%==500 goto end to if %loop%==80 goto end).