I'm making a file monitor for a folder where I download subtitles. So far, it works like this:
Look for new .rar files in the folder.
If found, extract the subtitles and delete the .rar file
If a single .srt file was extracted, save the file name to a variable.
Now, I'm clueless about how to achieve the next (and final) part of the script:
I want to find a pattern based on the way subtitles are named.
Let's say, the subtitles file can be something like this:
SomeShow.1x03.stuff.srt
some_show s01e03-stuff.srt
some show 1-03 stuff.srt
etc.
I want to get something like: SomeShow 1 3 and based on that, start the video with the name that matches that pattern, which I guess would be a matter of reversing the process that was used to get the Show, season and episode based on the name of the .srt file.
Is this possible at all? It'd be really simple stuff in most languages, but I really need this to be a .bat and I'm clueless about how to approach this... so far all I've managed to do is to remove the extension from the variable.
Thanks in advance.
Batch files are Turing complete - you can do anything in them, but it is usually not wise to go to extremes. You might be able to package a sed or grep or your own binary alongside your .bat file for a good compromise between batchiness and function. If you can assume a suitable operating system, you will have Powershell installed and go that route.
You should recognize that the task is not exactly defined and that the "solution" may need some tweaking and be never robust enough.
For this reason, the richer language you can pick, the further you will get.
Related
I am looking for some kind of logic in linux where I can place files with same name in a directory or file system.
For e.g. i create a file abc.txt, so the next time if any process creates abc.txt it should automatically check and make the file named as abc.txt.1 should be created, then next time abc.txt.2 and so on...
Is there a way to achieve this.
Any logic or third party tools are also welcomed.
You ask,
For e.g. i create a file abc.txt, so the next time if any process
creates abc.txt it should automatically check and make the file named
as abc.txt.1 should be created
(emphasis added). To obtain such an effect automatically, for every process, without explicit provision by processes, it would have to be implemented as a feature of the filesystem containing the files. Such filesystems are called versioning filesystems, though typically the details are slightly different from what you describe. Most importantly, however, although such filesystems exist for Linux, none of them are mainstream. To the best of my knowledge, none of the major Linux distributions even offers one as a distribution-supported option.
Although it's a bit dated, see also Linux file versioning?
You might be able to approximate that for many programs via a customized version of the C standard library, but that's not foolproof, and you should not expect it to have universal effect.
It would be an altogether different matter for an individual process to be coded for such behavior. It would need to check for existing files and choose an appropriate name when opening each new file. In doing so, some care needs to be taken to avoid related race conditions, but it can be done. Details would depend on the language in which you are writing.
You can use BASH expression to achieve this. For example if I wanted to make 10 files all with the same name, but having a unique number value I would do the following:
# touch my_file{01..10}.txt
This would create 10 files starting at 01 all the way to 10. This method is also hand for looping over files in a sequence or if your also creating directories.
Now if i am reading you question right your asking that if you move a file or create a file in a directory. you would want the a script to automatically create a new file for you? If that is the case then just use a test and if there is a file move that file and mark it. Me personally I use time stamps to do so.
Logic:
# The [ -f ] tests if the file is present
if [ -f $MY_FILE_NAME ]; then
# If the file is present move the file and give it the PID
# That way the name will always be unique
mv $MY_FILE_NAME $MY_FILE_NAME_$$
mv $MY_NEW_FILE .
else
# Move or make the file here
mv $MY_NEW_FILE .
fi
As you can see the logic is very simple. Hope this helps.
Cheers
I don't know about Your particular use case, but You may try to look at logrotate:
https://wiki.archlinux.org/index.php/Logrotate
So I have a program right now that reads text files from a folder. They are in a separate folder than the one my program is in, and both of them are in a dropbox folder together. Right now if I want to read one of these .txt files I'll do
file='/Users/JohnDoe/application/folder/file.txt'
text=open(file,'r').read()
print(text)
and it works just fine on my pc. The problem is that if I open my dropbox on another computer and run this program, it won't work because the filepath will probably be slightly different. Is there a way to work around this? Maybe a module that searches for files using something other than filepath, or something like that.
I'm brand new to python and to programming in general so any suggestions are appreciated. Thanks.
If you know the specific name of the file, but you're unsure where it might be located on another machine, you can use os.walk like this previous answer suggests. Or you can read up on using regex (regular expressions) in Python like this previous answer suggests.
There are some files like .bashrc or linux config files (cannot remind them right now), which contain lines of information inserted by different software/apps/libraries, e.g. when they were installed.
Is there any way to differentiate information in such files, i.e. to know which lines belong to which software? Maybe there is some convention for that? If yes, where can I learn about it. If no, how do software clean up their configuration and other property in OS files on uninstall?
A note about tags:
I mentioned low-level languages as well as linux and shell because I think only guys from those techs may know the answer.
"Is there any way to differentiate information in such files, i.e. to know which lines belong to which software" - No.
You are dealing with plain text files and there is no way to tell who put what into those files. It may have been a program you installed/ran or it may have simply been a user editing the file in a text editor - you don't know.
You also have no way of knowing whether the contents of the file is well formed or not until you parse it.
If the file does not parse (is messed up) you have two options: 1) read what you can and discard everything else. 2) give up and tell the user the file is garbage
In any case, you cannot trust the information in the file. Anyone could have put it there.
Edit: Oops my bad! I wasn't clear enough... I guess I need to explain more...
I need to create a package installer for my customers. I want them to extract and overwrite the contents only in their specific folder.
I don't want them to be able to extract the contents wherever they want to let them steal my work /or know what my files are all about.
So I was thinking maybe the installer could be created in a way to check for a file name inside the folder and after the file name has been recognized then it can be simply extracted and overwritten and if not then the operation will be cancelled.
Any Idea ?
Assuming you're running Windows...If the file name is recognised in the specified extraction folder, then it'll prompt you to either overwrite or cancel. - Either using Window's own extractor or something like 7Zip will achieve this.
Okay, so I believe this might help:
https://superuser.com/questions/259353/7-zip-windows-7-make-extract-to-folder-default-on-double-click
Particularly Haiggoh's comment:
1.Start regedit as administrator
2.Open HKEY_CLASSES_ROOT\7-Zip.7z
3.Under that key, expand the Shell sub-key
4.Set the (Default) value to the string extract
5.Create a new sub-key named extract
6.Set the (Default) value for the extract key to Extract to Folder
7.Create a new sub-key under extract named command
8.Set the (Default) value of the command key to:
C:\ProgramFiles\7-Zip\7zG.exe x "%1" -o* (you might have to a adjust
this to match the path of you 7zip installation)
Instead of 7z with -aou like Justin Roettger suggested I ended up
using 7zG, because this way you can choose to overwrite if you like
just like extracting with the normal context menu.
That's it! 7z files are now extracted to a folder with their own name
by double click. For other extensions like .rar and .zip you need to
repeat these steps for the according keys. (i.e.
HKEY_CLASSES_ROOT\7-Zip.rar and HKEY_CLASSES_ROOT\7-Zip.zip and so on)
Oh and to clarify: It does work with multiple files selected as well.
No batch file need.
Have a read through the above and hopefully it'll point you in the right direction.
Good day,
I am a CNC program not a computer programer. I am using CAM software to make cutting programs for our CNC router. The router is a bit old and can only take files 200-300 kb big. We are doing carvings that require 1-2 megs text files. I am using a program called GSplit ( http://www.gdgsoft.com/gsplit/ ) to divvy up the text file. It generates 10-25+ files with a custom header that our machine can read. All the files are great and it works, but I have to manually add the closing lines/footer to each file. The files that are created and used are normal .txt files but with a specific extension, .ANC.
Is there any way to automate this process of opening each individual file, scrolling to the end and copy/pasting the same 1-2 lines of code? The files are NAME[number].ANC in a contained folder. Would it be possible to just direct to a folder and say "add this 'text' to every file in this folder"?
Thanks for your time.
What OS are you using? Using Unix you can do a simple script on command line. If you are in the directory with the specific files simply execute:
for file in *; do echo "APPEND THIS" >> $file; done
If you are running Windows you should be able to do the same using cygwin (probably you could also use the power shell, but I don't know anything about the that)
I found a program Notepad++ (apparently the last person to find it...). USed the find/replace files option. A regular expression(note sure exactly what these are but I'm sure you guys do) "\s+\z" as to what to look for. It finds the last space or whatever at the end of all the files and then adds the code I need. Easy, free, and I don't need to write any computer code. Thanks for the attempt to help me Dirkk! :)