Developer Forums relocated!

Need help with Apple Developer tools and technologies? Want to share information with other developers and Apple engineers? Visit Developer Forums at Apple.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How do I create a directory subfolder that will remove all but the first 6 numbers from the folder name, AND add _mmyy to the end of it

Need help with script in Automator.

echo " mkdir "$1/$2" "

mkdir -p "$1 $(date -j +"%m%y")/info"

mkdir "${1:0:6}_"$(date -j +"%m%y")

rm -r "$1"


I can get the first folder to be created (302123Denise 0123_ but need a second folder that is called 302123_0123. Can someone help me? See attached screen shot.

Posted on Jan 30, 2023 10:38 AM

Reply
Question marked as Best reply

Posted on Jan 31, 2023 7:26 AM

Absent some details on the required task, and defaulting to the home directory, and completely lacking of error checking and related verification, here's a zsh example that prompts for a string, then shows the commands used to create two directories (the actual mkdir commands are commented out) by parsing the name:


#!/bin/zsh
jobName=$(osascript -so <<EndOfScript
    set userJobInput to (display dialog "Enter Job Name" default answer "" \
    with icon note \
    buttons {"Done"} default button "Done")
    return text returned of userJobInput
EndOfScript
)
#    text returned of (display dialog "Enter Job Name" default answer "" \
#    with icon note \
#    buttons {"Cancel", "Continue"} default button "Continue")
#EndOfScript
#)
digits=${jobName:0:6}
date=$(date -j +"%m%y")
echo "Job Name: $jobName"
echo "Digits: $digits"
echo "mkdir -p \"$HOME/${jobName}_${date}/info\""
# mkdir -p "$HOME/${jobName}_${date}/info"
echo "mkdir -p \"$HOME/${digits}_${date}\""
# mkdir -p "$HOME/${digits}_${date}"


There are lots of ways this script can fail, not the least of which is a too-short string.


Similar questions

11 replies
Question marked as Best reply

Jan 31, 2023 7:26 AM in response to MrHoffman

Absent some details on the required task, and defaulting to the home directory, and completely lacking of error checking and related verification, here's a zsh example that prompts for a string, then shows the commands used to create two directories (the actual mkdir commands are commented out) by parsing the name:


#!/bin/zsh
jobName=$(osascript -so <<EndOfScript
    set userJobInput to (display dialog "Enter Job Name" default answer "" \
    with icon note \
    buttons {"Done"} default button "Done")
    return text returned of userJobInput
EndOfScript
)
#    text returned of (display dialog "Enter Job Name" default answer "" \
#    with icon note \
#    buttons {"Cancel", "Continue"} default button "Continue")
#EndOfScript
#)
digits=${jobName:0:6}
date=$(date -j +"%m%y")
echo "Job Name: $jobName"
echo "Digits: $digits"
echo "mkdir -p \"$HOME/${jobName}_${date}/info\""
# mkdir -p "$HOME/${jobName}_${date}/info"
echo "mkdir -p \"$HOME/${digits}_${date}\""
# mkdir -p "$HOME/${digits}_${date}"


There are lots of ways this script can fail, not the least of which is a too-short string.


Jan 30, 2023 12:46 PM in response to Neecerp3

This thread is a continuation of this earlier thread:


Create a Folder with the mm/yy at the end… - Apple Community


The mkdir -p I’d mentioned over there replaces both mkdir commands (that were originally used over there) with one mkdir command that creates both directories.


More generally… I’d stay out of Automator here, and would debug the zsh or bash shell script directly.


I would strongly recommend avoiding this command for now:

rm -r


That can potentially clobber unexpected things, if $1 is not entirely as expected. rm errors are unrecoverable, absent backups or other copies of the data. The following rm is somewhat safer, while testing the script:

rm -ri


Rather than the Automator stuff, and the shell script stuff, please post the input path spec and the required output path spec, and somebody here can then provide the shell script commands.

Jan 30, 2023 2:31 PM in response to MrHoffman

Did you see the above original above screen shot?

The automator creates a pop up in the mac finder window and they enter it into that.


What you say here "Or are these production jobs just the operator inputting the string into some tool, and you then want two nested folders created? " is basically what we want. Thank you in advance.


But it will end up being one folder with another info subfolder inside it, and then ANOTHER folder as described earlier....we need two folders for production areas.


Here is what the pop up looks like

Jan 30, 2023 1:17 PM in response to Neecerp3

Neecerp3 wrote:

I dont want to have to learn bash. I just need to finish this up in automator so I can use it as a desktop droplet.


That much is clear.


Please post the droplet renaming specification—your preferred behavior—for your path naming transformation.


To start the spec, from my understanding so far: leading six characters of the input path get appended with month and year.


What should happen if the leading characters aren’t digits? Anything different?


What should happen if the specified input path is less than six characters?


And to confirm, you really do want path names that will sort all the same-month entries together across all years?


0123

0124

0125

0223

0224

0225

etc


That makes you entirely dependent on how those first six characters sort. If they’re possibly duplicated, the months will sort in preference to the years.


And it seems you’re now deleting (rm) directory contents. Are you presumably also transferring some or all of contents from one directory to another? (I ask this because a rename (mv) operation might work better here, if some or all of the contents are being moved into the new directory.)

Jan 30, 2023 1:33 PM in response to MrHoffman

Basically these folders are created for individual jobs we do in my department.

The first 6 digits will always be numbers and always be 6 digits.

The date naming convention works for what we do on our server.


The operator will enter something like this in the first variable entry: 123456Description

We then need two main folders to be created, one like this: 123456Description 0123 (which contains the subfolder "info"

and one like this: 123456_0123 (which removes the description and places underbar with the date at the end)


forgot to mention....it works for the first folder and subfolder. Just need it to add that second one.


Jan 30, 2023 2:26 PM in response to Neecerp3

What is the connection between the operator inputting that descriptive string, and the folder creation?


Your use of a droplet implies manual processing.


Your description implies the operator is entering some text, and the folders fall out.


Is there some other task active here that's creating these directories, or is this all in this same Automator script?


Details like this can send things in different directions, such as some operation creating a folder in a known place, and the requirements here then operate—if some other tool or operation is involved writing a folder into a known place, then it's possible to have the folder take an action when the new folder arrives. Automatically.


Or are these production jobs just the operator inputting the string into some tool, and you then want two nested folders created? (You're referring to a droplet and which implies some other steps elsewhere and with some manual processing to drag something onto the script, but your replies here are also referring to an operator that is entering something. Is all this input specification and folder-related processing one sequence, from operator text input through to folder creation? Or is there an intermediate file or directory here that's getting dragged around and dropped?)

How do I create a directory subfolder that will remove all but the first 6 numbers from the folder name, AND add _mmyy to the end of it

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.