HOW TO CREATE THIS BAT FILE FOR MAC??

I am a novice at writing and using scripts. I am using a book to help with my data management workflow and it suggested using the following batch file. I've tried changing the file to .sh and changing all the "\" to "/" but its not working. Probably becuase I have no clue what I am doing. Can kind smart human willing to explain to me how to change this script correctly? Please!


Orginal Batch file from Windows:

REM workflow chapter 2 \ wfsetupsingle.bat jsl 2008-05-11.

REM directory structure for single person.

FOR /F "tokens=2,3,4 delims=/- " %%a in ("%DATE%") do set CDATE=%%c-%%a-%%b

md "- History\%cdate% project directory created"

md "- Hold then delete\- Delete when space is needed"

md "- Pre posted\- Files being prepared to post"

md "- To clean\- Files to be cleaned"

md "- Versions\- Waypoint versions to keep\"

md "Administration"

md "Documentation"

md "Posted\- Files should never be changed"

md "Posted\- Datasets\Source"

md "Posted\- Datasets\Derived"

md "Posted\- Text"

md "Resources"

md "Work\Text"

md "Work\- To do"


What I tried to do and failed with many error signs:

REM workflow chapter 2 \ wfsetupsingle.bat jsl 2008-05-11.

REM directory structure for single person.

FOR /F "tokens=2,3,4 delims=/- " %%a in ("%DATE%") do set CDATE=%%c-%%a-%%b

md "- History/%cdate% project directory created"

md "- Hold then delete/- Delete when space is needed"

md "- Pre posted/- Files being prepared to post"

md "- To clean/- Files to be cleaned"

md "- Versions/- Waypoint versions to keep/"

md "Administration"

md "Documentation"

md "Posted/- Files should never be changed"

md "Posted/- Datasets\Source"

md "Posted/- Datasets\Derived"

md "Posted- Text"

md "Resources"

md "Work/Text"

md "Work/- To do"

MacBook Pro, iOS 10.3.1

Posted on Dec 28, 2017 10:16 AM

Reply
Question marked as Top-ranking reply

Posted on Dec 29, 2017 8:54 PM

As has been noted, Windows .bat files are not the same as shell scripts.


However, your batch file isn't doing much beyond creating a bunch of subdirectories. The only mildly complex part is creating the directory with the current date in its name.


For all of the batch's 'md' statements, just replace them with the UNIX command 'mkdir'.

The only remaining question in my mind is what the final result looks like - it's been about a million years since I've used DOS, but from what I recall, the backslash is a directory delimiter, so looking at the command:


md "Posted/- Datasets\Derived"


I would expect this to create a directory called 'Derived' inside the 'Posted- Datasets' directory. Is that right?

If so, replace any backslash with the UNIX directory delimiter of forward slash - and nix the existing forward slashes since they're irrelevant. Then add the -p switch to the mkdir command to tell mkdir to make multiple directories as needed, so the line


md "Posted/- Datasets\Derived"

becomes:

mkdir -p "Posted- Datasets/Derived"


As for the date command - I have no idea how Windows formats the output of the %DATE% variable, so I don't know what you're aiming for, but assuming you want something like '2017-12-29', you can use the UNIX command: date +%Y-%m-%d, and you can embed this directly in the mkdir command using backpacks (so no need to use a temporary variable:


mkdir -p "History`date +%Y-%m-%d` project directory created"


If you want a different order, mix the %Y, %m %d characters into the format you want. There are also other substitutions you can use - e.g. %Y = 2017, %y = 17. For a complete list open Terminal.app and run: man strftime


The final gotcha is that you have directory names that begin with a -. That's OK, but you'll need to jump through one extra hoop since the shell will interpret the - as a switch, and will likely error. The fix is simple - just precede the directory name with '--'.


Putting that all together (-p switch for any multi-level directory, -- for any directory name that begins with a "-"), you get something like:


#! /bin/bash

mkdir -p -- "- History/`date +%Y-%m-%d` project directory created"

mkdir -p -- "- Hold then delete/- Delete when space is needed"

mkdir -p -- "- Pre posted/- Files being prepared to post"

mkdir -p -- "- To clean/- Files to be cleaned"

mkdir -p -- "- Versions/- Waypoint versions to keep"

mkdir "Administration"

mkdir "Documentation"

mkdir -p "Posted/- Files should never be changed"

mkdir -p "Posted/- Datasets/Source"

mkdir -p "Posted/- Datasets/Derived"

mkdir -p "Posted/- Text"

mkdir "Resources"

mkdir -p "Work/Text"

mkdir -p "Work/- To do"


Note that I highly recommend using a leading character other than '-' if you ever expect to access these files via the shell. It will really mess things up.


Save the above in a plain text file, make it executable (via chmod a+x scriptname) and you're pretty much done.

5 replies
Question marked as Top-ranking reply

Dec 29, 2017 8:54 PM in response to brennan3117

As has been noted, Windows .bat files are not the same as shell scripts.


However, your batch file isn't doing much beyond creating a bunch of subdirectories. The only mildly complex part is creating the directory with the current date in its name.


For all of the batch's 'md' statements, just replace them with the UNIX command 'mkdir'.

The only remaining question in my mind is what the final result looks like - it's been about a million years since I've used DOS, but from what I recall, the backslash is a directory delimiter, so looking at the command:


md "Posted/- Datasets\Derived"


I would expect this to create a directory called 'Derived' inside the 'Posted- Datasets' directory. Is that right?

If so, replace any backslash with the UNIX directory delimiter of forward slash - and nix the existing forward slashes since they're irrelevant. Then add the -p switch to the mkdir command to tell mkdir to make multiple directories as needed, so the line


md "Posted/- Datasets\Derived"

becomes:

mkdir -p "Posted- Datasets/Derived"


As for the date command - I have no idea how Windows formats the output of the %DATE% variable, so I don't know what you're aiming for, but assuming you want something like '2017-12-29', you can use the UNIX command: date +%Y-%m-%d, and you can embed this directly in the mkdir command using backpacks (so no need to use a temporary variable:


mkdir -p "History`date +%Y-%m-%d` project directory created"


If you want a different order, mix the %Y, %m %d characters into the format you want. There are also other substitutions you can use - e.g. %Y = 2017, %y = 17. For a complete list open Terminal.app and run: man strftime


The final gotcha is that you have directory names that begin with a -. That's OK, but you'll need to jump through one extra hoop since the shell will interpret the - as a switch, and will likely error. The fix is simple - just precede the directory name with '--'.


Putting that all together (-p switch for any multi-level directory, -- for any directory name that begins with a "-"), you get something like:


#! /bin/bash

mkdir -p -- "- History/`date +%Y-%m-%d` project directory created"

mkdir -p -- "- Hold then delete/- Delete when space is needed"

mkdir -p -- "- Pre posted/- Files being prepared to post"

mkdir -p -- "- To clean/- Files to be cleaned"

mkdir -p -- "- Versions/- Waypoint versions to keep"

mkdir "Administration"

mkdir "Documentation"

mkdir -p "Posted/- Files should never be changed"

mkdir -p "Posted/- Datasets/Source"

mkdir -p "Posted/- Datasets/Derived"

mkdir -p "Posted/- Text"

mkdir "Resources"

mkdir -p "Work/Text"

mkdir -p "Work/- To do"


Note that I highly recommend using a leading character other than '-' if you ever expect to access these files via the shell. It will really mess things up.


Save the above in a plain text file, make it executable (via chmod a+x scriptname) and you're pretty much done.

Dec 28, 2017 12:17 PM in response to brennan3117

To begin with you are no longer in Redmond, WA. You are now in the land of Unix, and Windows .bat files are for the most part nothing like a Unix shell scripts. There maybe a tiny bit of overlap, but not much.


You actually might find it better to use Applications -> Automator to do what you want. Use the "New Folder" action.


Or if you must, the "Run Shell Script" action.


If you are going to proceed with a shell script, even one that is from from Automator "Run Shell Script", then a comment starts with a # (US keyboard pound sign). The # can be at the beginning of the line, or it can be the last thing on a command line. The comment continues until the end-of-the-line.


Upper and lowercase matters. That is to say, everything in a shell script is case sensitive. So 'CDATE' is a different variable from 'cdate', and 'Cdate' is different, as well as 'CDate', etc....


'md' (make dir) in Unix is 'mkdir'. For more information about 'mkdir', from an Applications -> Utilities -> Terminal session, enter the command:

man mkdir

Or just Google 'mkdir'


I do not really understand the .bat FOR command, so all I can tell you is that for a shell script there is a command called 'for' but that is totally where the similarities end. Oh yea, 'for' is lowercase ONLY. If you want information on the 'bash' 'for' command, then

man bash

Or maybe some Googling for creating bash 'for' loops


Getting the current date information would be done using the 'date' command, and you can get information on 'date' from:

man date

For example, this command

CDATE="$(date +%Y-%m-%d)"

will put "2017-12-28" into the ALL UPPERCASE variable 'CDATE' (the above command does NOT have any spaces between CDATE the = and the "...". If you add spaces the assignment will not work.


The various % codes can be found in

man strftime

And you would substitute the contents of 'CDATE' with a ${CDATE}

mkdir -p "- History/${CDATE} project directory created"

If -<space> has special meaning for 'md', it is meaningless for the Unix 'mkdir' command. The Folder created by this command will be:

"Your Home Folder" -> "- History" -> "2017-12-28 project directory created"

where the folder "- History" will be created in your Home folder, and "2017-12-28 project directory created" will be a subfolder under "- History".

If this is NOT what you want, then you will need to modify the various 'md' commands to follow Unix 'mkdir' syntax.



If you want to get deeper into Unix 'bash' shell scripting, then Google "macos bash script basics", and you will find some introductory guides.

Dec 28, 2017 2:32 PM in response to brennan3117

I recommend this book:

Practical Guide to Linux Commands, Editors, and Shell Programming, A (2nd Edition) [Paperback] the book
Don't be fooled by the name, the second addition includes Mac OS X.


other possibilities.

Advanced Bash Script. premise: Examples for everything. I have revision 6.2.
http://tldp.org/LDP/abs/abs-guide.pdf


BASH Programming - Introduction HOW-TO
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html#toc14


Apple administrative commands
http://manuals.info.apple.com/en_US/IntroCommandLine_v10.6.pdf


Apple Shell Scripting Primer
https://developer.apple.com/library/mac/documentation/OpenSource/Conceptual/Shel lScripting/ShellScripting.pdf


Check you local library. Any book on Bash syntax will do. There will be minor differences but they will not be great.


Sometimes you can get a good deal on used books in Amazon.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

HOW TO CREATE THIS BAT FILE FOR MAC??

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