I want to spin through a config file in a loop assigning any options I find to a variable of the same name in my notebook.
So the code is shorter and I don't have a load of try and else steps. I initialize default options at the start and then if I find a config option with the same name it updates it
Config file
[file_options]
cfgfilename = 'newfilename.csv'
Notebook
import configparser
# default options
cfgfilename = 'dummy.csv'
otheroptions_etc = 'hi'
config = configparser.ConfigParser()
config.read({config file path here})
if config.has_section('file_options'):
for option in config.options('file_options'):
{something here to set cfgfilename}= config.get('file_options', option )
print (cfgfilename) # and so it comes out as newfilename.csv not dummy.csv
Thanks samKart this is how I solved it using a dictionary
# create dictionary with the dummy value pairs
dict_values = { 'filename' : 'dummy.csv',
'otheroptions_etc' : 'hi'}
# check it has a file options section
if config.has_section('file_options'):
# loop through the options in the section
for option in config.options('file_options'):
# if that option is in the dictionary..
if option in dict_values.keys():
# ... up date it
dict_values[option] = config.get('file_options', option )
# finally set all the variables to the updated dictionary values
filename= dict_values['filename']
otheroptions_etc= dict_values['otheroptions_etc']
Related
I'm using the logging facilities, as described in this other post:
OUTPUT TO VALUE("C:\Temp_Folder\logfile.txt").
...
PUT UNFORMATTED "Start : Check-zones" SKIP.
...
OUTPUT CLOSE.
This is working fine.
Now I would like to add the results of an OS-COMMAND in the output file.
I've already tried the following: (putting the results in a newly to be created file)
OS-COMMAND NO-WAIT VALUE("WMIC printer get name, deviceID >> C:\Temp_Folder\Printers.txt").
This is working fine.
So, I know the command is working fine. However, the following is not working:
OS-COMMAND NO-WAIT VALUE("WMIC printer get name, deviceID >> C:\Temp_Folder\LogFile.txt").
This is obvious, because C:\Temp_Folder\Logfile.txt is locked for writing by the progress application, so the shell, opened by OS-COMMAND can't write to that file.
In order to overcome this, I would like to catch the results of the OS-COMMAND's results.
How can I do this?
Unfortunately back in the dark ages when os-command was designed, it was considered useful to suppress all errors.
You can either start a batch file and let that do some “guaranteed” output error handling, see https://knowledgebase.progress.com/articles/Article/21039
Or (since you are on Windows) you can use the .Net system diagnostics process class (which you will want to wrap in your own method or function to simplify use):
DEFINE VARIABLE oProcess AS System.Diagnostics.Process NO-UNDO.
DEFINE VARIABLE lcstderr AS LONGCHAR NO-UNDO.
DEFINE VARIABLE lcstdout AS LONGCHAR NO-UNDO.
oProcess = NEW System.Diagnostics.Process().
oProcess:StartInfo:FileName = "wmic.exe".
oProcess:StartInfo:Arguments = "printer get name, deviceID".
oProcess:StartInfo:CreateNoWindow = TRUE.
oProcess:StartInfo:UseShellExecute = FALSE.
oProcess:StartInfo:RedirectStandardError = TRUE.
oProcess:StartInfo:RedirectStandardOutput = TRUE.
oProcess:Start().
lcstdout = oProcess:StandardOutput:ReadToEnd().
lcstderr = oProcess:StandardError:ReadToEnd().
oProcess:WaitForExit().
lcstdout = lcstdout + oProcess:StandardOutput:ReadToEnd().
lcstderr = lcstderr + oProcess:StandardError:ReadToEnd().
I am using filelist to generate a dataframe of each folder and the files contained in it. I would like to save each folder name and file name with an observation number x to be able to pull their names out later.
ssc install filelist
filelist
//Save each file's name and corresponding folder:
forvalues x = 1 / `=_N' { //for every row in the filelist dataframe shown
local file = filename[`x']
global folder_`x' dirname[`x'] //save the folder name as folder_i, i = 1, 2, ... _N
global file_`x' filename[`x'] //save the file name as file_i, i = 1, 2, ... _N
}
global filecount `=_N'
This runs smoothly, and if I was to run di $file_2, for instance, it would produce the given filename. The issue I have is that then when I try to use this and access these Globals later on, they appear to have saved the "filename[`x']" rather than the actual filename. For instance, if I run:
import excel "InterestRates.xlsx", sheet("US") firstrow
di $file_2
Then I get the error filename not found. I have tried changing up my `' and "" and {} in many different ways, and I still cannot seem to get this to reference the actual filename. Any help would be greatly appreciated!
There are two ways to assign local or global macros: with an equal sign, and without. If it is assigned without an equal sign, the content will be stored as it is. With an equal sign, the content will be evaluated first.
clear
input str8 filename
"file.dta"
end
global file_1 filename[1]
global file_2 = filename[1]
di "$file_1"
di "$file_2"
Result:
. di "$file_1"
filename[1]
. di "$file_2"
file.dta
Here is a slight alteration to Wouter's code which still gives me the error:
cd [MY_PATH_HERE]
ssc install filelist
filelist
//Save each file's name and corresponding folder:
forvalues x = 1 / `=_N' { //for every row in the filelist dataframe shown
local file = filename[`x']
global folder_`x' dirname[`x'] //save the folder name as folder_i, i = 1, 2, ... _N
global file_`x' filename[`x'] //save the file name as file_i, i = 1, 2, ... _N
}
export excel test.xlsx, firstrow(var)
import excel different.xlsx, firstrow clear
di $file_1
This then still produces the filename not found error as before. It works fine when telling Stata to import the same Excel (test.xlsx) you just saved, but if you import a different Excel, it causes issues. I don't understand why this is the case though if you are saving it as a global macro.
My AutoIt script worked as long as I used it via command line. There I could use $CmdLine[1] and pass a path as argument. Now I try to convert the script to a new method to avoid command line arguments.
You open an Explorer Window and select a file e.g C:\test.txt. After that you trigger the AutoIt function with CTRL+WIN+C. The script should look what file is selected in the active Explorer Window and retrieve the path C:\test.txt and assign it to $file variable.
This is my work-in-progress where I'm stuck.
Line 5 $CmdLine[1] needs to be changed to a secret function I don't know.
;Assign key combination "CTRL-WIN-C" to function "copyUNC"
HotKeySet("^#c", "CopyUNC")
;function to copy UNC path of selected Windows Explorer file/folder to clipboard
func CopyUNC()
$file = FileGetLongName($CmdLine[1]) ;THIS LINE NEEDS TO BE CHANGED
$drive = StringLeft($file, 2)
$UNCdrive = DriveMapGet($drive)
If $UNCdrive = "" Then
$UNCfile = $file
else
$UNCfile = $UNCdrive & StringTrimLeft($file, 2)
endif
ClipPut($UNCfile)
endfunc
;necessary loop so AutoIt script stays active and in Tray
While 1
Sleep(100)
WEnd
Q: How do I get the path of a selected file/folder from Windows Explorer into AutoIt v3.3.8.1?
Note #1: I don't want to use registry and right-click tricks to pass the argument
Note #2: If multiple files are selected just pass the first file. Don't overcomplicate things
CMDLINE[1] has nothing to do with what you want.
If you want to activate your script by hotkey AFTER manually selecting the file in Windows Explorer, you need to examine the Explorer window itself.
Here is the function to retrieve selected item in explorer
Func GetExplorerSelection()
Local $saveClip = ""
Local $filesFolders = ""
Local $handle = _WinAPI_GetForegroundWindow()
Local $className = _WinAPI_GetClassName($handle)
If $className = "ExploreWClass" Or $className = "CabinetWClass" Then
$saveClip = ClipGet()
Send("^c")
Sleep(50) ; give clipboard time to react
$filesFolders = ClipGet()
ClipPut($saveClip)
; test if $filesFolders contains #LF and split if so etc..
; code to call StringSplit() etc..
EndIf
EndFunc
maybe this
HotKeySet('{F8}','ccc')
Func ccc()
$ObjWindows = ObjCreate("Shell.Application").Windows()
For $i = 0 To $ObjWindows.Count -1
Local $w = $ObjWindows.Item($i)
Local $aSeLected = $w.Document.SelectedItems()
For $b = 0 To $aSeLected.Count -1
$x = $aSeLected.Item($b)
MsgBox(0,'',$x.Path&'_______'&$x.Name)
Next
Next
EndFunc
While 1
Sleep(100)
WEnd
I am currently using the following code to select a file and add its path to a text box.
Dim objDialog As Object
Set objDialog = Application.FileDialog(3)
With objDialog
.AllowMultiSelect = False
.Show
If .SelectedItems.Count = 0 Then
MsgBox "No file selected."
Else
Me.FileNameTextBox = Dir(.SelectedItems(1))
End If
End With
Set objDialog = Nothing
How do I make it so the entire file path is inserted, not just the file name?
.SelectedItems(n) already contains the full path and filename. If what you need is just to separate the name of the file from its path, instead of using the Dir function you could use something like this:
Me.FileNameTextBox = Mid$(.SelectedItems(1), InStrRev(.SelectedItems(1), "\") + 1)
Me.PathTextBox = Left$(.SelectedItems(1), InStrRev(.SelectedItems(1), "\"))
Hope this helps!
you need to remove the dir() part, EG....
Me.FileNameTextBox = .SelectedItems(1)
Hi there I'm currently trying to find a way to save 2 variables from my workspace into a file. I want the file name to be put together using the original and the current date.
I only want the max value from the variables so:
max(streaking)
and
max(tap_total)
The original file name is:
3_FM001_02_05460$BandP$64_24000_FWD_1x1_PRI_PRI_PRI_PRI_15_17_ActivePixelMeans.csv
The only portion of this original file name that I would like to use is:
3_FM001_02_05460$BandP$64_24000_FWD_1x1
These can be saved in a text file or spreadsheet, it doesnt matter.
An example of the new file name would be something like this:
3_FM001_02_05460$BandP$64_24000_FWD_1x1_7-26-2012
Also,
If something could be done in the file to display which variable is which, for example:
Streaking: 1.272 % this would come from the variable max(streaking)
Tap_Total: 2.252 % this would come from the varaible max(tap_total)
EDIT:
% Construct a questdlg with three options
choice = questdlg('Would you like to save?', ...
'Save Options', ...
'Yes','No','Cancel','Cancel');
% Handle response
switch choice
case 'Yes'
disp([choice ' processing.'])
save_option = 1;
case 'No'
disp([choice ' processing.'])
save_option = 0;
case 'Cancel'
disp('Canceled.')
save_option = 2;
end
file_to_get = evalin( 'base', 'file_to_get' );
streaking = evalin( 'base', 'streaking' );
tap_total = evalin( 'base', 'tap_total' );
if save_option == 0
elseif save_option == 1
max_streak = max(streaking);
max_tap = max(tap_total);
str_streak = mat2str(max_streak);
str_tap = mat2str(max_tap);
fname = file_to_get;
pruned_fname = regexprep(fname,'_PRI(\w*).(\w*)','');
new_fname = [pruned_fname '_' date '.csv'];
path1 = '\\pfile01thn\bbruffey$\My Documents\analysis data\Banding and Streaking Results';
fid = fopen([path1 new_fname], 'w');
fprintf(fid,['Max Banding: %s\nMax Streaking: %s'],str_tap,str_streak)
fclose(fid);
elseif save_option == 2
end
This would be a great place to use the regexprep command to prune the original filename down.
Example:
fname = '3_FM001_02_05460$BandP$64_24000_FWD_1x1_PRI_PRI_PRI_PRI_15_17_ActivePixelMeans.csv';
pruned_fname = regexprep(fname,'_PRI(\w*).(\w*)','');
pruned_fname =
3_FM001_02_05460$BandP$64_24000_FWD_1x1
Now, a note about the regexprep command I used here. This is specific for the filename you provided here. Since it looks like you want to trim off everything after the the first _PRI I used a tag (\w*) which means any combination of [a-z A-Z _ 0-9] can follow. Notice that since this is a filename there is a . there hence why I added a period in and followed that with another (\w*) to finish out the csv. You can find more info on these sorts of operators here.
Now that you have the pruned_fname you can simply add whatever you want to it. So if you want to add the date in with an underscore to space it just simply do something like this:
new_fname = [pruned_fname '_' date '.csv'];
new_fname =
3_FM001_02_05460$BandP$64_24000_FWD_1x1csv_26-Jul-2012.csv
Now you simply need to open the file to write to it. you might need to append the path to where you want to save it, I'm just going to call it path for now. It would be something like C:\Documents\
fid = fopen([path new_fname], 'w')
Now with the fid you have the id of the file you want to write to. This should be a new file and if it isn't you are going to overwrite the file contents if you do it this way. Just be aware =)
Next you can simply write those first two lines to the file using fwrite fprintf, just to name a few possible functions.
Hopefully that'll get you setup there!