Automatic extract zipped files with passwords in the file name - automation

I analyse data from being sent multiple ZIP files.
They are always in this format:
service_SC30COM_####_20191130_1834.zip
#### is a random number generated by the computer.
Password is SC30COM_####, which is always part of the file name.
Any suggestions on an automation to unzip in bulk?

There is no way to do it on the command prompt without any application.
You can check this
If your remove the password, the code you need, as explain in that microsoft article should be:
$shell=new-object -com shell.application
$CurrentLocation=get-location
$CurrentPath=$CurrentLocation.path
$Location=$shell.namespace($CurrentPath)
$ZipFiles = get-childitem *.zip
$ZipFiles.count | out-default
foreach ($ZipFile in $ZipFiles)
{
$ZipFile.fullname | out-default
$ZipFolder = $shell.namespace($ZipFile.fullname)
$Location.Copyhere($ZipFolder.items())
}
If you install any application that can run on command prompt, you can extract with password. As example, for a individual file, the maximum you will get on Windows 10 is:
PowerShell Expand-Archive -Path "C:\Users\Tuffy\Desktop\PowerShell
Expand-Archive -Path "C:\Users\Whatever\Desktop\service_SC30COM_####_20191130_1834.zip"
-DestinationPath "C:\Users\Whatever\Desktop"p" -DestinationPath "C:\Whatever\Tuffy\Desktop"
Hope it helps!

You can run the following code as a bash script:
#!/bin/bash
for FILE in *.zip
do
echo "Unzipping $FILE ..."
PASSWORD=$(echo $FILE | grep -o -P '(?<=service_)[A-Za-z0-9]*_[0-9]*(?=_)')
unzip -P $PASSWORD $FILE
done
Copy the code
Paste it into FILENAME.sh
Make it executable (chmod +x FILENAME.sh)
Put it besides the zip files
Run it (./FILENAME.sh)

Related

Move file, change permissions and rename it keeping the same extesion

Using zsh 5.2 on Fedora 24 workstation.
I want to be programatically able to:
move an image file (can have jpg/ jpeg/ png/ JPG/ PNG extensions)
from /tmp/folder1 to ~/Pictures
This file will have the same few initial characters --- prefix111.jpg OR prefix222.png, etc.
rename the file such that samefilename.JPG becomes 20161013.jpg
20161013 is today's date in yyyymmdd format
Note that the extension becomes small letters
And JPEG or jpeg becomes jpg
change the permissions of the moved file to 644
All at one go.
If there are multiple prefix* files, the command should just fail silently.
I will initially like to do it at the command prompt with an option to add a cron job later. I mean, will the same zsh command/ script work in cron?
I am sure, this is doable. However, with my limited shell knowledge, could only achieve:
mv /tmp/folder1/prefix-*.JPG ~/Pictures/$(date +'%Y%m%d').jpg
Problems with my approach are many. It does not handle capitalization, does not take care of different extensions and does not address the permission issue.
How about this:
#!/bin/sh
FILES="/tmp/folder1/prefix*.jpg /tmp/folder1/prefix*.jpeg /tmp/folder1/prefix*.png h/tmp/folder1/prefix*.JPG /tmp/folder1/prefix*.PNG"
if [ $(ls $FILES | wc -l ) -gt 1 ]; then
exit 1
fi
if [ $(ls $FILES | grep -i '\.png$') ]; then
SUFF=png
else
SUFF=jpg
fi
DEST=$HOME/Pictures/$(date +'%Y%m%d').$SUFF
mv $FILES $DEST
chmod 644 $DEST

Mac Terminal /bin/settitle.sh: No such file or directory

I'm trying to create a bash script to change the titles of my terminal windows so I can identify what they are doing. I spent a few hours on this and cant figure it out. The idea is to be able to execute settitle NewTitle. Thank you.
This is my echo:$PATH. It looks like Users/klik/bin is there twice. Maybe that is the issue?
~ klik echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/klik/bin:/Users/klik/bin
This is the script which was created in textedit in plain text format.
#!/bin/bash
# settitle: set the Mac Terminal title
# usage: to set the titlebar to 'PLAY', type: settitle PLAY
echo -e "\033]0;${1}\007\c"
This is my bash_profile and bin file.
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
export PATH=$PATH:$HOME/bin
alias desk='cd ~/Desktop/'
alias down='cd ~/Downloads/'
alias github='cd ~/github/'
This is my ls -a output
Current directories
~ klik ls -l $HOME/bin | pbcopy
total 8
-rwx--x--x# 1 klik staff 147 Mar 9 21:39 settitle.sh
Try this:
echo -e "\033]0;FreddyFrog\007\c"
You need to use -e to turn on interpretation of escape characters. You can also use printf.
printf '\033]0;%s\007\015' "Hippo Croco Horror Pig"
This issue above was that the file was saved with .txt extension. I dont know why this was the case given the ls command showed a .sh ext. At any rate, this is the process I used for creating this script and and executing it.
Open Finder -> Applications->TextEdit in Mac.
Select New Document at bottom left.
From menu select Format -> Make Plain Text
Paste in this code:
#!/bin/sh
# settitle: set the Mac Terminal title
# usage: to set the titlebar to 'PLAY', type: settitle PLAY
echo "\033]0;${1}\007\c"
Thanks to Alvin Alexander for the code.
Still in TextEdit select menu File -> Save
Uncheck "If no extension is provided, use ".txt" "
When I chose my file name I saved it with no extension so i could just type the command settitle NewTitle without having to type the extension every time.
Note the folder the file is being saved to. It defaults to desktop on my machine.
Open Finder -> Go -> Go to Folder
Type in the path to your User Bin folder: mine was /Users/klik/bin
You can check to see if you have a User/bin folder by running: ls -l from your home directory.
If you don't have a bin folder in this directory you can create one by going to your $HOME directory and executing:
mkdir bin
To find out what is your home directory see this
You can then open the directory by executing:
open bin
This will open the folder in Finder.
Drag the script file you created into this folder.
Make sure the script is executable by executing the following command from the folder the file is in or by including the path to the file in name of file:
chmod +x <name of file>
Make sure that the script is in your executable $PATH by executing:
echo $PATH
You will get something like this:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/<you>/bin
If you dont see the path of your script, ie /Users/''/bin, then the script file is not in your executable path and you need to put it in your .bash_profile. Execute ls -l to see if you have a .bash_profile file.
ls -l
If you don't have one, make sure your are in your $HOME directory then create one by executing:
mkdir .bash_profile
Open your .bash_profile file in your default editor:
open .bash_profile
Or open with nano (to save and close nano see this link):
nano .bash_profile
Add the following line to the .bash_profile then save/close:
export PATH=$PATH:$HOME/bin
Exit the terminal to reset by executing:
exit
Open the terminal then type:
settitle <whateveryouwant>
I hope this saves someone some time. Thanks to Mark Setchell for his constructive help.

Using PowerShell variable for example in FTP.exe

How do you use powershell variables when you run program in it? Is it simply not possible?
Example:
$url = "test.com"
ftp
ftp> open $url
Unknown host $url.
The FTP command will read commands from a file, so you can use Powershell to create a file containing the required commands and then tell the command to read from the file. It is best to create a unique name for the temporary file and remember to delete it when you are finished:
PS C:\> $url = "test.com"
PS C:\> $filename = "foo.bar"
PS C:\> $tempFile = [io.path]::GetTempFileName()
PS C:\> #"
>> open $url
>> get $filename
>> quit
>> "# >>$tempFile
>>
PS C:\> cat $tempFile
open test.com
get foo.bar
quit
PS C:\> ftp -s:$tempFile
PS C:\> remove-item $tempFile
A multi-line string #"..."# is a good way to create a temporary file for external commands because it lets you write the commands in a natural way.
Other commands may not accept input from a file. In that case it is worth checking whether you can specify all of the needed commands on the command line. Many non-Powershell commands will actually accept multi-line arguments even though when run from cmd.exe there would be no way to pass such arguments. For example:
PS C:\python34> python -c #"
>> for i in range(10):
>> print(i)
>> "#
>>
0
1
2
3
4
5
6
7
8
9
PS C:\python34>

Chaining terminal script on mac os x

I am trying to chain some terminal commands together so that i can wget a file unzip it and then directly sync to amazon s3. Here is what i have so far i have s3cmd tool installed properly and working. This works for me.
mkdir extract; wget http://wordpress.org/latest.tar.gz; mv latest.tar.gz extract/; cd extract; tar -xvf latest.tar.gz; cd ..; s3cmd -P sync extract s3://suys.media/
How do i then go about creating a simple script i can just use variables?
You will probably want to look at bash scripting.
This guide can help you alot; http://bash.cyberciti.biz/guide/Main_Page
For your question;
Create a file called mysync,
#!/bin/bash
mkdir extract && cd extract
wget $1
$PATH = pwd
for f in $PATH
do
tar -xvf $f
s3cmd -P sync $PATH $2
done
$1 and $2 are the parameters that you call with your script. You can look at here for more information about how to use command line parameters; http://bash.cyberciti.biz/guide/How_to_use_positional_parameters
ps; #!/bin/bash is necessity. you need to provide your script where bash is stored. its /bin/bash on most unix systems, but i'm not sure if it is the same on mac os x, you can learn it by calling which command on terminal;
→ which bash
/bin/bash
you need to give your script executable privileges to run it;
chmod +x mysync
then you can call it from command line;
mysync url_to_download s3_address
ps2; I haven't tested the code above, but the idea is this. hope this helps.

SSH unzip AND change the filename (or get the filename contained therein)

/usr/bin/curl http://somewebsite.com/foo.zip -o 4232.zip
unzip -o -q -L 4232.zip
chown 508 /home/me/www/inbound/data/??????.xml
rm -f 4232.zip
I am using this SSH script to download a zip file called foo.zip, rename the file to 4232.zip, the extract the contents.
My problem is that the zip file contains a single file whose name is constantly changing. I cannot see a flag for unzip that lets me rename the file(s) inside the zip.
How can I rename the mystery file inside. There is really only ever one file in my immediate project.
-or-
How can I get that filename so I can change ownership and use it in a PHP script that will process it later on...
Any help would be appreciated.
This will tell you the name of the .xml file in the zip
unzip -l z.zip | grep -o '[^/]*\.xml'
If you want to extract and rename the xml file
unzip -p z.zip \*.xml | cat > NEWFILE.xml