Folder and subfolder - import

I am new to python and I am struggling, I have a front end that takes in a username and title and stores it into a dictionary but I need to make a function that takes those key value pairs in and searches a path to see if a username folder exists and if not created it an searches if the title subfolder exists in that folder and if not creates it but I'm at a loss

Related

How to rename an XML file using the pattern in a different .txt file?

I have a folder say, source folder, containing 1000+ xml files with some ambiguous names, like:
_MIM_15646432635_6664684
_MIM_54154548557_6568436 etc.
Out of these thousands of XML files I’ve to select some 10-12 xml files with a particular node in them and move them to another folder (destination folder) and rename the files respectively with some meaningful names.
For example:
There is an xml file with name _MIM_15646432635_6664684 and it contains a node pattern like: “bab6e7h835468eg” and I’ve to rename it to name like: {1FE9909E-4450-B98665362022}
So for this I’ve to write a script which will search the file in source folder and if it finds my desired node pattern then move this file to destination folder post renaming it to some meaningful name.
Provided I’ve an excel sheet where I do have a list of two columns, one containing the specific node pattern and column two has a respective new name list.
Currenty, I’ve a script which can search a file and move it to another folder provided I’m giving input to the script with the node pattern and file name from that excel sheet:
Select-String – Path “\Dubwta01\AIR\Invalid*.xml” – Pattern ‘bab6e7h835468eg’ | %{Copy-Item – Path $_.Path – Destination ‘\Dubwta01\AIR\Invalid{1FE9909E-4450-B98665362022}.xml’
What now I need is that a new script which will pick all 10 files from source folder and move it to destination folder by renaming it and I don’t have to hard code the pattern and new name in script rather it shall fetch the details from that excel or I can save the details in text files whatever is suitable for script to pick the name and pattern from.

Powershell Only:File name change according to mapping file and then copy to another directory

I have a mapping file in import.CSV as following:
Name|Business
Jack|Carpenter
Rose|Secretary
William|Clerk
Now, I have a directory which contains files like
90986883#Jack#Sal#1000.dat
76889992#Rose#Sal#2900.dat
67899279#William#Sal#1900.dat
12793298#Harry#Sal#2500.dat
Please note #Sal will always be there after Name. I need to pick these files and put into another directory and end result in second directory should look like.
90986883#Carpenter#Sal#1000.dat
76889992#Secretary#Sal#2900.dat
67899279#Clerk#Sal#1900.dat
Basically Files need to be renamed based upon CSV file and if Name is not there in file name , then there is no action required. Please note that source file should not be changed.
I will appreciate all kind of help.

How to insert a link to a folder

I can insert links to specified documents within folders, I want to be able to insert a link that takes the user to the folder to open up any document within.
I've tried something to the effect of this, but get an error ///server/share/etc
///server/share/etc
It wont take the file address that I am inputting in the 'insert link' option.
So you can link to a folder, it should be in this format:
file:///"Drive":%5C"folder"%5C"folder"%20"folder"%5C"folder"%20"folder"/
Items in "" are to be filled in. the %5C is to separate folder names, and the drive from the first folder. %20 is used in place of spaces between words of a folder. like New%20Folder.

MATLAB - Get current path and then use it to navigate to a different folder

I have a bunch of codes that are currently stored on my local machine. There are two folders, one called "Resources" and another called "src". There is one main script that needs to be run called "main.m" in "src" which calls files from "Resources".
If I copy this whole thing onto a new computer, the paths will change and MATLAB may not be able to find "Resources" anymore. I know that relative to "main.m", I need to go up one level and then into "Resources".
What is the best way of getting MATLAB to point to "Resources"?
I am currently trying along the lines of
P = mfilename('fullpath')
which gives the path for main.m. Now, I want to navigate from here, one folder up and then into "Resources". Or if there is a better way, please let me know.
Eventually, I want to extend it to work for multiple folders "Resources1", "Resources2" etc. so MATLAB needs to be able to navigate to the right folder.
You can get it like:
fullfile(fileparts(mfilename('fullpath')), '..', 'Resources');
Explanation:
mfilename('fullpath') will return the full path and name of the
M-file in which the call occurs, without the extension
fileparts will return the path of the passed file (only the containing directory)
fullfile will build the full directory specification from the folder names passed (Note: '..' always means the parent directory)
Based on this it is quite simple to write a function that gets the sibling directory of the directory containing the file:
getSiblingOfParentDirectory.m
function siblingDirPath = getSiblingOfParentDirectory(filepath, siblingDirName)
siblingDirPath = fullfile(fileparts(filepath), '..', siblingDirName);
end
then to use it in an M-file:
for i = 1:3
disp(getSiblingOfParentDirectory(mfilename('fullpath'), ['Resources', num2str(i)]));
end
Sample output:
D:\pathtest\Resources1
D:\pathtest\Resources2
D:\pathtest\Resources3
You can try the following:
ResourcesFolder = strrep(mfilename('fullpath'), 'src\main', 'Resources');
addpath(ResourcesFolder);
%%Your code here where you need those files
rmpath(ResourcesFolder);
Which is fully dependant on the names of your folders & files of course. Basically "addpath" enables you to access the files in the mentioned directory by adding it to the search path, and "rmpath" does the exact opposite.
Also, if you literally want to navigate to a folder present on one level up, you can execute the following:
cd ..\Resources
Which goes one level up, searches for the folder 'Resources', then changes the current directory to that folder .

temporary remove all of the added paths in matlab

I have added a lot of paths to matlab and now I want to share my code, but I don't know exactly which function (in which path) I should add to my shared code. Each time I need to add a missing function and it is really bothering for me and the users who are using the code.
So, I would like to restore the matlab path to its original case. Is there any way to do this in matlab? I also want to keep a backup of my current added path in a .m file and use it later when I am done.
To restore the path to default value - http://www.mathworks.com/help/matlab/ref/restoredefaultpath.html
restoredefaultpath sets the search path to include only folders for
MathWorks® installed products. Use restoredefaultpath when you are
having problems with the search path.
restoredefaultpath; matlabrc sets the search path to include only
folders for MathWorks installed products and corrects search path
problems encountered during startup.
And to save the current path - http://www.mathworks.com/help/matlab/ref/savepath.html
savepath updates the MATLAB® search path for all users on the system
so that the path can be reused in a future session. savepath saves the
search path to the pathdef.m file that MATLAB located at startup, or
to the current folder if a pathdef.m file exists there.
Or you can just store path in variable p = path; and restore it later path(p);. If the path is saved into pathdef.m the call of pathdef returns the string that can be used to set the saved path.