-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshiftr
99 lines (93 loc) · 2.59 KB
/
shiftr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash
# Set up variables
ext1=""
ext2=""
source_dir=""
dest_dir=""
# Parse command line options
while getopts ":e:s:d:" opt; do
case $opt in
e)
ext1=$OPTARG
ext2=$OPTARG
;;
s)
source_dir=$OPTARG
;;
d)
dest_dir=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
# Check if variables are set
if [ -z "$ext1" ] || [ -z "$ext2" ] || [ -z "$source_dir" ] || [ -z "$dest_dir" ]; then
# Print message to console
echo "Variables are not set, asking you to provide them"
# Prompt user to enter values for variables
read -p "Enter value for ext1: " ext1
read -p "Enter value for ext2: " ext2
read -p "Enter value for source_dir: " source_dir
read -p "Enter value for dest_dir: " dest_dir
fi
# Function to extract date from filename
extract_date() {
# Print value of filename variable
echo "Filename: $filename"
# Use regex to extract date from filename
if [[ $filename =~ ^([0-9]{4})([0-9]{2})([0-9]{2})\.([^.]+)$ ]]; then
year=${BASH_REMATCH[1]}
month=${BASH_REMATCH[2]}
day=${BASH_REMATCH[3]}
else
# Print error message if regex does not match
echo "Error: Unable to extract date from filename"
return 1
fi
}
# Function to extract date and time from filename
extract_datetime() {
# Use regex to extract date and time from filename
if [[ $filename =~ ^([0-9]{4})([0-9]{2})([0-9]{2})_([0-9]{2}):([0-9]{2}):([0-9]{2})\.([^.]+)$ ]]; then
year=${BASH_REMATCH[1]}
month=${BASH_REMATCH[2]}
day=${BASH_REMATCH[3]}
hour=${BASH_REMATCH[4]}
minute=${BASH_REMATCH[5]}
second=${BASH_REMATCH[6]}
# Print value of BASH_REMATCH array
echo "BASH_REMATCH: ${BASH_REMATCH[@]}"
else
# Print error message if regex does not match
echo "Error: Unable to extract date and time from filename"
return 1
fi
}
# Iterate over files in source directory
for file in "$source_dir"/*; do
filename=$(basename "$file")
extension="${filename##*.}"
# Check if file is of correct type
if [ "$extension" == "$ext1" ] || [ "$extension" == "$ext2" ]; then
# Construct destination path
construct_path
if [ $? -eq 0 ]; then
# Check if destination directory exists
if [ ! -d "$dest_path" ]; then
# Create destination directory if it does not exist
mkdir -p "$dest_path"
fi
# Move file to destination directory
mv "$file" "$dest_path/$filename"
# Print success message
echo "Success: Moved $filename to $dest_path"
fi
fi
done