[SOLVED] Automation of folders/files

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
DalekDraco
Level 1
Level 1
Posts: 5
Joined: Sat Jul 01, 2023 12:58 am

[SOLVED] Automation of folders/files

Post by DalekDraco »

Hi all
I am trying to find a way to automate the creation of new folders with the relevant files inside them, for my business.
I run a law firm and each folder is named based on the year and the file number, for example 2024001, 2024002, etc.
Each matter has:
Folder: 20XXXXX
- Sub-folder Admin
-- which contains Timesheet.xlsx
- Sub-folder Client File
- Sub-folder Correspondence
- sub-folder Court
- 20XXXX - Telephone running sheet.docx

Currently I am manually duplicating the main folder and then renaming everything to the next file number.

I have done some searching and it seems I should be able to create a bash command that does this for me (and hopefully renames the telephone sheet to the next file number too?). However, all attempts thus far have been absolute failures. The closest I have come is:
mkdir -p 2024061/{Admin,Client\ File,Correspondence,Court,}
But that means I have to put the timesheet and telephone sheet in manually, and I'm still having to manually run mkdir for each new matter.

Any pointers would be most welcome!
Last edited by DalekDraco on Tue Mar 12, 2024 12:29 am, edited 1 time in total.
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: Automation of folders/files

Post by 1000 »

This script costs 2 hours of your work - that's how long it took me to write.
Have fun helping the Linux community for 2 hours.
I hope you can help someone for two hours or more.

Edited.
- And don't check the script directly.
Copy the folder with the files and first check if it works.
- I added a dot to the name because it makes the number more readable. If you need to, you can remove the dot.

Code: Select all

#!/bin/bash


Debug=ON

F_Debug() {
    if [[ "$Debug" == "ON" ]] ; then
        echo "  DEBUG: $@"
    fi
}

# Get number of the current year
Year=$(date +%Y)
    F_Debug "Year = $Year"

# Get last folder name
SearchedWord="${Year}.*"
Last_Folder=$( find . -maxdepth 1 -type d -name "${Year}.[0-9][0-9][0-9]" | LC_ALL=C sort | tail -n1 )
    F_Debug "Last_Folder = $Last_Folder"


# If folder with Year not exist
if [[ -z "$Last_Folder" ]]; then
    echo "  - We are creating the first folder this year."
    Next_N="001"
    mkdir -p ${Year}.${Next_N}/{Admin,Client_File,Correspondence,Court,Telephone}
    
# If folder with Year exist
else 
    # We need get 3 last number of folder
    Three_N="${Last_Folder: -3}"
        F_Debug "Three_N = $Three_N"
    
    # We need add + 1 to number
    Next_N=$((${Three_N} + 1))
    Next_N=$(printf "%03d" $Next_N)
        F_Debug "Next_N = $Next_N"
        
        if [ ! "${#Next_N}" -eq 3 ]; then
            echo "Error: Next_N not have 3 chars."
            exit 1
        fi
    
    echo "  - We will create next folder with number $Next_N."
    mkdir -p ${Year}.${Next_N}/{Admin,Client_File,Correspondence,Court,Telephone}
fi 

echo "  - The End."
mikeflan
Level 17
Level 17
Posts: 7162
Joined: Sun Apr 26, 2020 9:28 am
Location: Houston, TX

Re: Automation of folders/files

Post by mikeflan »

Copy these 8 lines to a text file:

Code: Select all

mkdir ./2024003
mkdir ./2024003/Admin
mkdir './2024003/Client File'
mkdir ./2024003/Correspondence
mkdir ./2024003/Court
touch './2024003/Telephone running sheet.docx'
touch './2024003/Court/2024003 - Timesheet.xlsx'

The 8th line is just a blank line.

Open the text file in XED.
Copy all the lines by clicking in the text file, hitting Ctrl-a, and then Ctrl-c.
Paste all 8 lines into a terminal that is open to the location you want the directory structure built. Ctrl-Shift-v will paste the commands into the terminal.
It creates all the files and folders.

Then do a 'Search and Replace' in the text file (Ctrl-h). Replace 2024003 with 2024004.
Then paste all 8 lines into the terminal again.
You get a second set of files and folders.
DalekDraco
Level 1
Level 1
Posts: 5
Joined: Sat Jul 01, 2023 12:58 am

Re: Automation of folders/files

Post by DalekDraco »

1000 - I couldn't get your script to work properly - it creates the folder structures but doesn't create a time-sheet or the telephone running sheet (makes a folder called telephone). I will work on it to see if I can get it going.

Mikeflan - your script works perfectly and with some tweaking to pull in the template telephone running sheet it will do the job. Thanks! I don't mind having to change the parameters of the script for new files that way, it is still a heck of a lot faster than manually doing the whole thing.

Thank you both for your answers!
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: [SOLVED] Automation of folders/files

Post by 1000 »

1000 - I couldn't get your script to work properly - it creates the folder structures but doesn't create a time-sheet
It's possible I didn't understand you.
1. Creating a sub-folder with the same name as the main one looks strange.

2. Creating empty files looks weird.
2.1 If you want to save some data, a file will simply be created.
2.2 If you really need to create files .docx / .xlsx ,
create a pattern / template and use the copy command [ Sorry " cp " command ] to copy the file to the chosen directory.
This is the easiest way.

3. The script creates folders from scratch.
- You can change the starting number 001 to your last number [ Or better yet, one number higher than last ]
- Or in the script you can remove all dots from the folder name, if you need.

Edited
However you can use tree command to show directory structure
DalekDraco
Level 1
Level 1
Posts: 5
Joined: Sat Jul 01, 2023 12:58 am

Re: [SOLVED] Automation of folders/files

Post by DalekDraco »

Thank you - I added a cp command for the files required from precedents and I tweaked the naming etc. It seems to be running!
Post Reply

Return to “Scripts & Bash”