7z: Is it possible to exclude files conditionally? - 7zip

It is possible to exclude files from zipping them with the 7zip -x switch, which allows wildcards too. So I can exclude all text files like this
7z a output.zip myfolder -x\!*.txt.
Now I want some txt files not to be excluded if they have a special name, like all text files named like this: *-KEYWORD.txt
I tried to use the exclude switch with the include switch together, like 7z a -xr\!*.txt -ir\!*KEYWORD.txt output.zip myfolder, but once the exclude switch is invoked, the include switch doesn't seem to reinclude excluded files again.
Is it possible to only include text files named like this, while excluding all other text files, inside the 7z syntax?

So this seems not to be possible in one command, especially not with the include and the exclude switch used both.
The solution I use in my script now is just to make two commands, the first excludes all files ending on *.txt, then another 7z command attaches all files like *-KEYWORD.txt to the package. It's not great but it works.

Related

Bash - Extract filenames without extensions and check if identically named files with a different extension exist

I have two folders with many files of the same naming format but differing extensions. I would like to loop through the files of the first folder and extract their filenames to check if a file of the same name exists in the other folder, then create one (with the other extension) if it doesn't. I can't get a grapple on bash for whatever reason, so I was wondering if there's a simple way to do this.

Batch file to rename files to specific names

So a quick explanation. We have hundreds of projects and in each of them and in every new project we have a program where we fill in a ton of information. From that we get 20 pdf files that are called File_1, File_2 etc.
What I'd like to do is to rename the files as the example below with a bat file.
File_1 = abc.pdf
File_2 = xyz.pdf
File_3 = qwe.pdf
I want to specific in the bat file what I want file_1 should be called and file_2 etc. The files I get will always be called File_1, File_2 etc and I always want them to be renamed the same. So each time I get those files I just run the bat file. Is there a way to do this? Or is there a better way to do this?
Thanks!
I'm assuming your files will always be called "file_1", "file_2" etc.??
If they are, then you can just write a file with a line for each file. For example
ren file_1 foo.pdf
ren file_2 bar.pdf
ren file_3 foobar.pdf
Note that as you didn't specify extensions for the original filenames, I haven't either. You would want to put the full file name with extension, or use wildcards if appropriate, e.g. ren file_1.*. (Be very careful with wildcards though, or you may end up trying to rename multiple files with the same name!)
You could check if the file exists first, and only run the ren on files that are there, or you could run it and let it error for missing files (though I'm sure other people will have reasons why you shouldn't).
You should also consider whether or not there will already be a file with the name you are trying to rename to, because if there the is then the rename will error and fail.
If your files will be different names daily, you will need to give more information as to what you have and what you need.
EDIT - Response to comment below
Copy (and complete) this code, and save it as 'rename.bat' or whatever you need.
#echo off
ren file_1 abc.pdf
ren file_2 xyz.pdf
ren file_3 qwe.pdf
<<repeat as necessary>>
The #echo off just stops the batch file from displayed the commands. It will still display errors (if the file doesn't exist or if you try to rename a file to an existing filename). You really should be looking at catching the errors and doing something with it, but if you can be 100% certain that the new filenames don't exist it will work.
Also worth pointing out that as I haven't used full file names, the batch file would need to be in the same folder as your file_1, file_2 etc.
You could use move to specify the full path of the original file, and a new path (it will move and rename the files), but you still have potentially the same problems with duplicate filenames etc.
I'm assuming you will end up with a folder containing file_1, file_2 etc. so you can just copy your batch file into the folder, run it, then move all your renamed files to where they need to go. Then next time you need to run the file, your folder would only have in it the new set of file_1, file_2 etc. so you could copy in the batch file again, run it... and so on.
EDIT2 - After thought for filenames including spaces
It just occurred to me that your existing files (file_1, file_2 etc.) don't appear to have any spaces in the name, but your new filenames might (you didn't specify the names exactly). If you have spaces if filenames, make sure to add quotes to the command e.g.
ren file_1 "my new file.pdf"
You can also quote your original filenames too (quoting both wouldn't hurt even if there are no spaces) so you could try
ren "file_1" "abc.pdf"
ren "file_2" "x y z.pdf"
ren "file_3" "qw e.pdf"
etc.

BuildMaster - FTP - How to include or exclude a file type

Is there a way to exclude a file? I would like to exclude all *.config files. Everything else should be included.
Or if I could say include: *.aspx, *.ascx, *.xml, *.png, *.gif, *.html that would be fine.
To quote what Tod said in this forum post:
I'm not sure the component we use to FTP supports negated wildcards,
but you can simply add a Delete Files action before this that operates
on *.config
Alternatively (if you don't want to delete because you may re-use the
files), you can use the Synchronize/Transfer Files action to a
temporary directory (e.g. ~\Ftp) and use a !*.config mask on that to
not transfer the configs, then use the FTP action from ~\Ftp as the
source directory.

SonarQube - Using wildcards to ignore all xml files

I have confused on SonarQube's wildcards usage. Say I want to ignore all xml files. Should I just put *.xml in the Global Source File Exclusions? But will it ignore xml files in different layers, for instance /foo/bar/file.xml and /foo/bar/folder/folder2/xml?
In your sonar-project.properties, you have two ways to ignore files:
sonar.exclusions=the/full/path/*.xml will ignore all .xml files in path.
sonar.exclusions=**/*.xml will ignore all .xml files in the folder and sub-folders where you are.
Here are the different wildcards:
* zero or more characters
** zero or more directories
? a single character
You can find more information on Sonar Documentation

Replacing a file in a zip archive

Using Ruby (1.9.3) I need to replace a single file in a zip archive.
The situation is as follows. I have ~1000 zip archives that need to be updated, specifically one file in each of them needs to be replaced. The archives are all of the same structure. Is there a quick and dirty way for Ruby, or a library/gem for Ruby, to simply say "replace the file in this zip archive with this file on the filesystem"?
I'll work on a solution of my own in the meantime.
You can use the zip command, called from the ruby, which probably will be the best solution. From the zip manpage zip manpage
-d
--delete
Remove (delete) entries from a zip archive. For example:
zip -d foo foo/tom/junk foo/harry/\* \*.o
will remove the entry foo/tom/junk, all of the files that start with foo/harry/, and all of the files that end with .o (in any path). Note that shell path‐
name expansion has been inhibited with backslashes, so that zip can see the asterisks, enabling zip to match on the contents of the zip archive instead of the
contents of the current directory. (The backslashes are not used on MSDOS-based platforms.) Can also use quotes to escape the asterisks as in
zip -d foo foo/tom/junk "foo/harry/*" "*.o"
Not escaping the asterisks on a system where the shell expands wildcards could result in the asterisks being converted to a list of files in the current
directory and that list used to delete entries from the archive.
Under MSDOS, -d is case sensitive when it matches names in the zip archive. This requires that file names be entered in upper case if they were zipped by
PKZIP on an MSDOS system. (We considered making this case insensitive on systems where paths were case insensitive, but it is possible the archive came from
a system where case does matter and the archive could include both Bar and bar as separate files in the archive.) But see the new option -ic to ignore case
in the archive.
If you want a pure ruby solution take a look at ZipFileSystem
Zip::ZipFile looks promising. It appears to have a way to delete and add files to a zip archive.

Resources