Copy & Paste not Cut & Paste Python - windows

I'm looking for a way to duplicate / clone an excel file from a source directory to a destination directory. Importantly, the source file(s) must remain in the source directory. Every example I've been able to find, however, uses the copy function of the shutil module which actually moves the source file to the destination.

What you can do is read the contents of the file into a variable, then create a new file where you want and write the contents of your variable in there.
Of course, there are better ways to do it, but this can be made cross-platform and will probably suit all your needs as long as you don't manage big files.

Related

bash: move new file as soon as it is written

I'm trying to write a bash script to move files from a source to a destination, in order to save space on the disk of the source (source and destination are two different machines).
I can handle the moving part by using the scp protocol, but the problem is that the software I'm using writes a lot of files very quickly (luckily each file has the same extension) and I want to move a file as soon as it is written.
So, in the directory of the source, I'd like to:
move the file on the destination,
remove the file from the source,
... until the software stops to write new files on the source.
Any help?
Thank you in advance.
This is a job for inotifywait.
inotifywait -meclose_write . |
while read d e f
do
echo Now is the time to move "$f".
done
Of course you can specify any source directory instead of ..

Use drag and drop function to select multiple files and process all

I need a way to select several files with check-boxes and drag them all to a batch-file icon. The first step in the script would be to be compressed them into a single zip file before proceeding to the next step. If possible, it would be useful to also end up with each in a separate zip of its own for storage.
I am not sure how to address the for:to commands to allow gathering all selected files into a single zip file in a script. The Windows 'sendto compressed zip' function works perfectly if I select all the files the copy and paste them to the Windows ZF filetype. But I don't know how to access it from within a batch.
Drag and drop multiple files
By default, if you drag a file onto a batch file it is the same as passing the filepath as a parameter. This can be accessed using %%1. Dragging multiple files would have a similar effect and be like calling test.bat file1.txt file2.txt file3.txt. Each "parameter" can be accessed using subsequent variables, %%1, %%2, %%3, etc, up to %%9.
Get more information here.
Zipping files
First you will need a utility that supports command line operations, such as 7-zip. Once you have the appropriate executable in your path, you will need to review the documentation on how to zip files from the command line. Instead of using the paths and filenames, you will use the variables mentioned above.
Here is some pseudo code:
zipfiles output.zip %%1 %%2 %%3 %%4 %%5
Notes
You will not be able to drop more than 9 files.
You may need to confirm that each variable exists. If you try to include %%9 in the zip command but you only dropped 8 files, you may get an error.

Move newly created text files to a var created directory

I have several text docs that are created each day from templates. This process I've achieved successfully albeit probably in a Cro-Magnon way. I want these newly created text files to be filed within a newly created dated folder.
The script creates the file docs from the templates successfully and also creates the newly dated directory. I don't really want to create these text files somewhere else and then move them to the newly created directory. Rather that they be created directly within it. All my research tends to involve directories that already exist rather than one created from a var.
I've included just one file creation example below.
Hope you can help. TIA
today=`date '+%y%m%d'`;
today_Folder=~/Desktop/test/"${today}"
if [[ ! -d $today_Folder ]]
then
mkdir "${today_Folder} `(date '+%A')`"
fi
cat ~/Desktop/test/template.txt >> ~/Desktop/test/dest.txt
P.S. I've tried to make the cat command regarding the text files clearer - it simply creates files. I'm NOT trying to create a tree of directories. Simply ONE newly created directory that could be in test along with the text files.
Your question is how to dynamically create a file, also creating all the path to contain that file? That's not possible in any intuitive/portable way, and it's not typically programs always have to create the directory before the file. What you can do is pass the -p flag to mkdir. On Linux systems (this may also not be portable), this flag means "create all the directories necessary for this path". Zero directories is okay, so you don't need to check whether the directory already exists. So change the whole if block to just this:
mkdir -p "${today_Folder} `(date '+%A')`"
Also, it's kind of smelly the way you want a string (the path) and you're using three operations to create it. Could it be simpler? You want more statements when they add clarity, but in this case the steps are so simple that the only thing accomplished is to make your colleagues go up and read what you wrote more than once. It might suit to change it to:
dir_path=...
mkdir -p "${dir_path}"
To accomplish this, keep in mind that instead of backticks, you can add command substitution with $(). It helps since backticks can't be nested--it makes the line more readable, since you clearly see the command's start/end.

Comparing variable with File names bash

I've recently started to learn bash script and have started to create a file repository system. I have gotten pretty far and am able to add files, remove files. When I remove a file from the repository it actually leaves the file in but changes permissions so only the user that removed can use it, it then send a copy to there home area, it also changes the name of the file left behind to "$fileNameOUT"
I know plan to add a feature to my add function which checks after a file has been added if there is a file with the same name but with "OUT" at the end, if it finds this the old file will be sent to a back folder so files can be restored. I know I have to loop through the directory using a for loop, however the problem I'm having is I don't know how I can compare the file I have just added to all of the files in the directory.
I hope someone can make send of what I just wrote.
If you know the name of the file you are interested in, you can use the -e test to check if it exists.
if [ -e fooOUT ]
then
echo File exists
fi

fsutil hardlink doesn't work?

I was looking for a way to create hard links under Windows and I found this page: http://technet.microsoft.com/en-us/library/cc788097.aspx
To try it out, I created a file (1.txt) on the root of my C: drive with 100 lines of the following content:
C:\1.txt (2.598 bytes):
test test test test test
Then I open the command prompt and typed:
fsutil hardlink create C:\2.txt C:\1.txt
Success, 2.txt was created but when I go to see it's size it has exactly 2.598 bytes and also noticed some strange behaviours (as far as my understanding of hard links goes):
If I delete 2.txt (the hard linked file) 1.txt is not deleted, and vice-versa.
If I open 2.txt after I delete 1.txt (the original file) the content is still the same.
How does the fsutil hardlink create command differs from the copy command? And how can I create a true hard link under Windows?
I'm using Windows XP SP 3, and my file system is NTFS.
That's exactly the behavior that supposed to happen. Are you sure you understand how hard links work? Hard links are really just multiple entries in the file system that refer to the exact same file. If you create a hard link to a file and modify one of the instances of the file, the other file will show up with your changes because they both refer to the same blocks of data on disk.
When you delete one of the hard links, you're really just deleting one of the file system entries referring to that file. The file doesn't get deleted until you delete all of its hard links (including the original one). After you delete 1.txt, 2.txt still exists and refers to the same file that was originally there, only now there is only a single file system entry referring to that file (namely, 2.txt). 2.txt is that file.
How does the fsutil hardlink create
command differs from the copy command?
And how can I create a true hard link
under Windows?
After creating the hardlinked file, try editing either. You will discover that the changes are reflected in both files.
Try creating a hard-link of a very large file (say a video or disk-image). Notice that the available space on the disk has not changed (or not changed by more than a few bytes, if additional metadata had to be written to disk to reflect the new dir entry).
That is a true hard link. In UNIX (I'm not familiar with the Windows variant) a file is just a set of data on the disk (simplistically). It's the directory entry itself that gives that file a name.
When you have two directory entries pointing at the same underlying file, deleting one of them does not delete the file unless it's the last directory entry attached to it (and the file is not still open by a process, but that's a different matter).

Resources