PowerShell - Duplicate file using a list txt of names - list

all!
I am trying to solve the following issue using PowerShell.
Basically, I have setup a file with the needed properties. Let's call it "FileA.xlsx".
I have a text file which contains a list of names, i.e:
FileB.xlsx
DumpA.xlsx
EditC.xlsx
What I am trying is to duplicate "FileA.xlsx" serveral times and use all the names from the text file, so in the end I should end up with 4 files (all of them are copies of "FileA.xlsx":
FileA.xlsx
FileB.xlsx
DumpA.xlsx
EditC.xlsx

Assuming that you have a file called files.txt with the following content:
bob2.txt
bob3.txt
bob4.txt
Then the following will do what you want:
$sourceFile = "FileA.xlsx"
gc .\files.txt | % {
Copy-Item $sourceFile $_
}
This will create a copy of the FileA.xlsx with the names bob2.txt, bob3.txt and bob4.txt

Related

Matlab Use Regex Expression to load file from directory

I have looked a lot for this but have not found anything. I am very new to matlab and regex in general.
My problem is, have a directory path 'dir' with only one .txt file in it. I do however not know the filename of the txt file. I want to load this file.
I have tried multiple things but cannot find the solution.
foo = load(fullfile(dir, '-regexp', '*.txt'))
Thank you for your help!
That syntax isn't valid for fullfile, and dir is an in-built function which it appears you're using as a variable... Here is something a little clearer which should work when you have a single txt file within a given folder
folder = 'my\folder\path\';
files = dir( fullfile( folder, '*.txt' ) );
if numel( files ) ~= 1
error( 'More or less than one .txt file found!' );
end
filepath = fullfile( files(1).folder, files(1).name );
foo = load( filepath ); % load is designed for .mat files, if your .txt contains anything
% non-numeric then you may want something more like readtable here...

Traversing multiple folders for searching the same file in multiple foders in python

search the same file in multiple folders
I have tried with os.walk(path) but I am not getting the nested folders traversing
for current_root, folders, file_names in os.walk(self.path, topdown=True):
for i in folders:
print i
for filename in file_names:
count+= 1
file_path = os.path.join(current_root + '\\' + filename)
#print file_path
self.location_dictionary[file_path] = filename
in my code, it will print all folders but it will not enter to the nested folders recursively
ex: I have subdir,subdir1,subdir2 and in subdir I have another dir called abc
in subdir and abc both contain same file name I want to read that file
os.walk does not work that way.
for each current_root it traverses, it provides the list of directories and files directly under it.
You're nesting the loops, which does ... well I don't know...
Here you don't need the folder (so just mute the argument). current_root already contains that info for your files:
for current_root, _, file_names in os.walk(self.path, topdown=True):
for filename in file_names:
count+= 1
file_path = os.path.join(current_root,filename)
#print file_path
self.location_dictionary[file_path] = filename
aside: creating a dictionary with full file as key and filename as value looks, well, not what you want (the same information could be stored in a set or list and os.path.basename could be used to compute the filename. Maybe it's reverse (filename => full path), provided that there are no duplicate filenames.

Python: How can I open a folder with variable

question again... I have a list A=['AA','BB','CC'] and a folder path like ./ABC/. There are three sub folder inside called 032_AA, 0244_BB, 01_CC
format is like random number_AA(BB or CC).
now I tend to use this list to enter these folder and open a txt file which in there sub folder:
cmd1='cd ABC'
os.sys(cmd1)
for i in A:
???????? ---------------------- enter folder 032_AA according to list A
with open('xxx.txt','a') as f:
XXXXXXXX
my main question is I dont know how to enter a folder with a known file name with random number as begining.
So any idea? Thanks!
you can use glob:
import glob
regex = './ABC/*'
for i in A:
subdir = regex + i
for name in glob.glob(subdir):
U can check glob for more info on pattern matching

How to get a file to be used as input of the program that ends with special character in python

I have an output file from a code which its name will ends to "_x.txt" and I want to connect two codes which second code will use this file as an input and will add more data into it. Finally, it will ends into "blabla_x_f.txt"
I am trying to work it out as below, but seems it is not correct and I could not solve it. Please help:
inf = str(raw_input(*+"_x.txt"))
with open(inf+'_x.txt') as fin, open(inf+'_x_f.txt','w') as fout:
....(other operations)
The main problem is that the "blabla" part of the file could change to any thing every time and will be random strings, so the code needs to be flexible and just search for whatever ends with "_x.txt".
Have a look at Python's glob module:
import glob
files = glob.glob('*_x.txt')
gives you a list of all files ending in _x.txt. Continue with
for path in files:
newpath = path[:-4] + '_f.txt'
with open(path) as in:
with open(newpath, 'w') as out:
# do something

iTunes exported XML playlist refers to files in the wrong music structure

This is a simplified version of a question I asked at the end of last year but could not get to the bottom of it. I hope that somebody can help me with this explanation.
I exported my iTunes playlist as an XML file (songs.xml) onto an external drive. Each song exported appears to have its metadata stored under a node in the XML file. A fragment containing 2 Adele songs is below.
After exporting the playlist, I copied the music files to the /Music folder on the external drive. The issue is that ALL files are now directly in this folder and not within the subfolders. The songs.xml file references each song as being in a subfolder of /Music e.g. /Music/Adele/21 - but that is no longer the case - all files are in /Music. Thus when I attempt to import the songs back in they cannot be found.
Can somebody tell me how I can parse songs.xml and replace the /Music/Artist/Album references with just /Music ? Then I could successfully re-import my tunes with their metadata as described in the file! An added difficulty is that some songs are referenced just under the Music/Artist, and not Music/Artist/Album. e.g. the Artist could be 'Various' or a compilation.
I can get access to a Mac or Linux terminal to run SED or a RegEx or any other command that you can advise. If you can help I'd be very grateful.
Thanks a lot in advance.
Ben
<dict>
<key>Track ID</key><integer>22041</integer>
<key>Name</key><string>Rolling in the Deep</string>
<key>Artist</key><string>Adele</string>
<key>Album Artist</key><string>Adele</string>
<key>Album</key><string>21</string>
<key>Persistent ID</key><string>B123AA625019E726</string>
<key>Track Type</key><string>File</string>
<key>Purchased</key><true/>
<key>Location</key><string>file://localhost/Volumes/External%20Hard%20Drive/Music/Adele/21/RollingInTheDeep.m4a</string>
<key>File Folder Count</key><integer>1</integer>
<key>Library Folder Count</key><integer>1</integer>
</dict>
<dict>
<key>Track ID</key><integer>22042</integer>
<key>Name</key><string>Someone Like You</string>
<key>Artist</key><string>Adele</string>
<key>Album Artist</key><string>Adele</string>
<key>Album</key><string>Someone Like You</string>
<key>Persistent ID</key><string>A274ED723536E610</string>
<key>Track Type</key><string>File</string>
<key>Purchased</key><true/>
<key>Location</key><string>file://localhost/Volumes/External%20Hard%20Drive/Music/Adele/SomeoneLikeYou.mp3</string>
<key>File Folder Count</key><integer>1</integer>
<key>Library Folder Count</key><integer>1</integer>
</dict>
This awk may be what you need.
awk '/Drive\/Music/ {sub(/\/string/,":string");sub(/Music.*\//,"Music/");sub(/:string/,"/string")}1' file
It will change this type of lines:
<key>Location</key><string>file://localhost/Volumes/External%20Hard%20Drive/Music/Adele/21/RollingInTheDeep.m4a</string>
<key>Location</key><string>file://localhost/Volumes/External%20Hard%20Drive/Music/Adele/SomeoneLikeYou.mp3</string>
to
<key>Location</key><string>file://localhost/Volumes/External%20Hard%20Drive/Music/RollingInTheDeep.m4a</string>
<key>Location</key><string>file://localhost/Volumes/External%20Hard%20Drive/Music/SomeoneLikeYou.mp3</string>
How does this work:
awk '
/Drive\/Music/ { # Serch for all lines with Drive/Music lines
sub(/\/string/,":string") # Replace last / to prevent problem with greedy regex in next step
sub(/Music.*\//,"Music/") # Replace from Music to last / with only Music/ (using .* greedy)
sub(/:string/,"/string") # Replace last / back to its original
}
1 # Print all lines, changed and not changed
' file # input file
sed '/Location/ s|\(Drive/Music\)[^<]*\(/[^/<]*<\)|\1\2|' YourFile
use of | instead of traditional / for easier reading and treatment of / in path
you could also use variable instead of Drive/Music to adapt easily to another place (and use double quote around the sed action in this case