How to perform a quick check if the file directory for a script in matlab is correct? - list

I have a script which relies on different files located in specific folders which are important to run the script without errors. In order to define the path location I decided to create many variables with the according path location name as string:
file directory var file directory location % default entries which
% only work with my computer
fd_1 = '\C:\Testrun\pathfinder.xls\';
fd_2 = '\C:\Testrun\pathfilter.slx\';
fd_3 = '\C:\Testrun\splinegenerator.xls\';
fd_4 = '\C:\Testrun\loftcreator.xls\';
fd_5 = '\C:\Testrun\surface_to_volume.xls\';
fd_6 = '\C:\Testrun\stp_creator.xls\';
fd_7 = '\C:\Testrun\CAD_file.stp\';
fd_8 = '\C:\Testrun\CAD_support_1.atm\';
fd_9 = '\C:\Testrun\CAD_support_2.atm\';
fd_10 = '\C:\Testrun\CAD_support_3.atm\';
This allowed me to use my script on my computer. However this was a pretty static solution which only works for one pc. Hence I need the following dynmamic routine to be coded:
0.) I created a while loop in order to rerun my script with the switch case/expression:
<<<here is the missing code for the file directory check>>>
%(I wanted to use the "strcmp" command to compare the strings with each other?)
<<<Here is my code with the specific while loop to rerun it>>>
1.) Before I enter this loop need to perform a quick check, if the files are correctly located.
2.) If the file directory cannot be assigned to the specific variables responsible for the file
directory name (e.g directory could not be found), a new file directory will be choosen by the
user
3.) The newly choosen file directory will be stored with the default file directory in a list
4.) The variable responsible for the file directory changes according to the list index which the
user choose from the list of stored file directory names
5.) The selection of the specific list index as well as the changes in the list will be permenantly
stored (The changes in the list should be saved and recalled again in the script upon rerunning
or exiting/reopening the script)
6.) The list index can be deleted if the user is unsatisfied with the file directory (e.g due the
file directory corruption)
Is it possible to write such a code and how would it be structered?

I think to put all those folders and files in the same path of main program, by this way, no need to mention drive letter like c:\ or d:, just mentiob folder name and its subfolders, and you can copy the main folder and run your program in another computer without changing anything, just run the main program.

Related

Linux : Can a directory rename be partially executed due to a power/transient mechanical failure in Server/Disk?

C/C++ rename function can be used to rename a directory.
Assume below situation (for Linux)
Directory X has files A, B and C.
X is renamed to Y (using C/C++ rename function). While the operation is in progress Server/Disk's power goes out. Then it is restarted.
Now is there a possibility of few files being in a directory X while others in Y.
e.g.
X : B
Y : A, C
Rename is only changing a name. The "id" of the file, and in linux a directory is a file which contains the "links" to other files and directories, stays the same!
As a file system is always directly relying to the physical block storage on any kind of hardware, always the complete block where the label is stored, must be rewritten and linked in the file system structure.
If a power fail happens in between, the "directory file" can be corrupted. This means, that more than the single renaming operations is involved!
BUT:
Modern file systems have many options to detect and repair such situations. E.G. ext4 has a journal in background. If any access may be interrupted, the journal has the information that these operation has started but not completed. By mounting such a partition/fs, the repair takes place automatically. If this is not possible, a fschk can do that job.
The situation, that only "some files" have been moved, is definitely never possible, because renaming a directory is not creating a new directory and moving the file names/links to the node id into the new directory, it is only a new name for an existing directory.
As a user: Simply use modern file systems and mostly all power down failures can be recovered by restart. You may find your filesystem in the "old" or the "new" version, but not in between.
It depends on implementation but for all file systems I know renaming is just replacing a label. So renaming of a directory doesn't differ from file rename operation.
And when you rename a file and turn off power you never end up with two files X and Y each having half of the file content.

For loop is throwing "OSError: [Errno 2] No such file or directory" after first iteration it is not iterating 2nd item in the list

I have one list with directories. I need to go each directory of a list and execute some command. like this I need to process entire list. But after first iteration it is throwing OSError. Please find the below code and help me.
CODE:
ls_2 = ['build', 'flexiserver', 'fvntools', 'gbuild', 'sample']
for dir in ls_2:
print ("Inside for loop")
os.chdir(dir)
ls_2 = os.listdir('.')
print ("Inside dir %s %s" %(dir, ls_2))
subprocess.call('buildme.sh')
Are the folder nested inside one another?
One possible reason is all these folders inside the same folder. In that case, after you get into build, you want to go one level back before you access flexiserver.
Your code seems to be looking for the flexiserver folder that is inside the build folder but there is no such folder there.

exception of boostlog when date changed to next day

I use boost log by this config.
[Sinks.2]
Filter="%Severity% >= 2"
Destination=TextFile
AutoFlush=true
Format="[%TimeStamp%] [%ThreadID%] <%Severity%> %Message%"
Asynchronous=false
Target="logs"
FileName="logs/quo.%Y%m%dT%H%M%S.%a.%5N.log.detail"
RotationTimePoint="00:00:00"
RotationSize=104857600
MinFreeSpace=4294967296
MaxSize=4294967296
ScanForFiles=All
when date change to next day. my program crash by exception:
terminate called after throwing an instance of
'boost::filesystem::filesystem_error'
what(): boost::filesystem::last_write_time: No such file or directory: "/root/work/hy-trade/bin/debug/logs/quo.20181027T173106.Sat.00000.log.detail"
I check my disk space, find the free space less than MinFreeSpace in config and the file quo.20181027T173106.Sat.00000.log.detail not exists.
how to avoid this exception?
version of boost is 1.67
thank you
It looks like someone had already deleted the log file before it was rotated. It may have been an external process, or Boost.Log.
With Boost.Log, this can happen if you have multiple file sinks that write log files into the same directory, which is also used as the target directory for the rotated files (i.e. the FileName parameter includes the path specified in the Target parameter, and there are multiple sinks that use that path). The problem is because, according to ScanForFiles=All, the library scans the target directory for any files but does not update the file counter to be used for creating new files. This means that if the file "quo.20181027T173106.Sat.00000.log.detail" was present in that directory when your process started then it would be considered as an old file, even if upon starting your process would be still writing new logs to that file. Then, when a file rotation happens and storage limits are exceeded (e.g. if MinFreeSpace is not satisfied), that file may be deleted. The rotation has to happen on another sink that still stores files into the same "logs" directory.
To solve the problem you can do one of the following:
Use ScanForFiles=Matching in your settings so that the file counter is updated after scanning. This will make sure that new log files have unique names and don't get deleted prematurely.
Write log files to a different directory from your target storage. I.e. specify FileName so that it doesn't point to the same directory as Target.
Also, you may want to add exception handling to avoid crashing in case of errors (which may still happen for whatever reason on filesystem operations). See here and here for more info (also, follow the links in those sections).

Windows up one directory from current path

I have been searching for hours but cant find a solution to this as yet. Apologies it is probably really simple.
My program is using CreateDirectory to create a new directory and then set the path to it to receive a number of data files:
if (CreateDirectory(dateTime.c_str(), NULL) || ERROR_ALREADY_EXISTS == GetLastError())
{
SetCurrentDirectory(dateTime.c_str());
}
Once all the data files have been generated I would like to move back up one directory without specifying the absolute path. Something equivalent to cd.. or ../ Does anyone know the best way to do this?
One possible approach is to get the current directory (GetCurrentDirectory) before changing to a new one and once complete, then change back the desired directory; akin to a push/pop.
In the sample I've left out error checking and buffer size requirements for simplicity.
TCHAR resetDir[1024] = {};
GetCurrentDirectory(1024, resetDir);
//... Do some work, change directories etc...
// Reset the directory
SetCurrentDirectory(resetDir);
Side note: the current directory when the process is launched is not necessarily the same as the directory the process image is in (the exe path).
Relative changes can be done with a simple
SetCurrentDirectory(_T(".."));
Although basing the relative from the current directory would also work (and may be preferable);
SetCurrentDirectory((currentDir + _T("\\..")).c_str());
Internally, cd command ends using SetCurrentDirectory. So to get something equivalent to cd.. or cd ../ you can simply use:
cr = ::SetCurrentDirectory("..");
cr should be non zero if it succeded and 0 if it failed. In the latter case use GetLastError to get further information.

Manually specify location of .vagrant folder in Vagrantfile

The folder where I have Vagrantfile is being auto-generated during the build, so it gets cleaned up, but I'd like to still be able to use the created machines. The easiest way would be to put .vagrant folder somewhere outside the auto-generated folder. Is this possible?
You have (at least) two options:
Use VAGRANT_DOTFILE_PATH to set the the location where the project specific data is stored (defaults to .vagrant as you already know). Note that the path has to be project/Vagrantfile specific.
cd to a directory where you want the .vagrant directory to be created, and use VAGRANT_VAGRANTFILE to specify the path to the generated Vagrantfile.
I know this is an old question, but for anyone arriving here via Google, there is a workaround if you really want to specify the metadata directory without mucking about with environment variables each time. Just put this in the top of your Vagrantfile:
VAGRANT_DOTFILE_PATH = 'custom/dotfile/path'
if(ENV['VAGRANT_DOTFILE_PATH'].nil? && '.vagrant' != VAGRANT_DOTFILE_PATH)
puts 'changing metadata directory to ' + VAGRANT_DOTFILE_PATH
ENV['VAGRANT_DOTFILE_PATH'] = VAGRANT_DOTFILE_PATH
puts 'removing default metadata directory ' + FileUtils.rm_r('.vagrant').join("\n")
system 'vagrant ' + ARGV.join(' ')
ENV['VAGRANT_DOTFILE_PATH'] = nil #for good measure
abort 'Finished'
end
I wanted each provider to use a separate Vagrant directory to easily be able to swap between them. I could not get #hairraisin's solution to work, but based on that I ended up with the following:
Vagrant.configure('2') do |config|
config.vm.provider :lxd do |lxd, override|
if ENV['VAGRANT_DOTFILE_PATH'].nil?
ENV['VAGRANT_DOTFILE_PATH'] = '.vagrant-lxd'
puts 'Removing default metadata directory ' + FileUtils.rm_r('.vagrant').join("\n")
exec 'vagrant ' + ARGV.map{|arg| Shellwords.escape arg}.join(' ')
end
…
This avoids endless recursion or aborting too early. exec rather than system avoids a non-zero exit code from every vagrant command.