Batch create folders from TXT file... with subfolders in them - macos

I have a TXT file with 1,000 lines of product numbers I need to make into 1,000 folders with two subfolders in each named IMAGE & SPEC. I want to run it in Automator or from Terminal on my mac but cannot find the answer ANYWHERE! Any help out there?

In Terminal.app, type “cd “ (without the quotes but make sure there is a space after cd). Then drag the folder from Finder (the folder where you want the 1000 new folders created in) Into Terminal.app. This will change the working directory to the directory to where the new folders will be created.
If your products.txt file is located in the same directory as the 1000 new folders will be created, enter this following line of code into the Terminal window and be sure to change the name “products.txt” to your actual file name.
while read line; do mkdir -p "$line/IMAGE"; mkdir -p "$line/SPEC"; done < products.txt
If your products.txt file is located elsewhere, then use this line of code instead.
while read line; do mkdir -p "$line/IMAGE"; mkdir -p "$line/SPEC"; done <
then drag the “products.txt” file from Finder Into Terminal.app. Then it should look something like this (assuming the “products.txt” file was on you Desktop)
while read line; do mkdir -p "$line/IMAGE"; mkdir -p "$line/SPEC"; done < /Users/YourShortName/Desktop/products.txt

you can use pure shellscript for this task like below:
while read line;
do
mkdir -p "$line/IMAGE";
mkdir -p "$line/SPEC";
done < products.txt

Related

Trying to write a MacOS automator script to make incrementing folders based off of scanned input

New to bash scripting, and I'm stuck. Within a static directory I'm trying to create a folder '001_scanned name' and within that directory create 6 more subfolders. I'm able to do it all brutishly with this code:
cd ~/deej/Test/Capture
mkdir "$1"
cd "$1"
mkdir "$1_1"
mkdir "$1_2"
mkdir "$1_3"
mkdir "$1_4"
mkdir "$1_5"
mkdir "$1_6"
Ugly, but works for now.
$1 is the scanned name and I was manually appending the prefix of the file names with "001, 002, etc.". Is there an easy way to do this within an Automator prompt since I'll be unable to keep the last variable stored in the code?
If you use the -p option with mkdir it will create intermediate directories as needed so you can do both commands at once. The seq function can create zero-padded integers:
for n in $(seq -f "%03g" 1 10);
do mkdir -p ${i}/${i}_${n};
done;
You can test using echo instead of mkdir -p. Below I had i set to 015...
015/015_001
015/015_002
015/015_003
015/015_004
015/015_005
015/015_006
015/015_007
015/015_008
015/015_009
015/015_010

Bash script to read .csv file to create/move content

I have a folder containing thousands of images (abc.jpg, 6_08.jpg, refugee_awareness_workshop_by_british_red_cross.jpg etc) and a csv file that contains columnA an ID number (1036, 10028 etc) and column B the image filenames.
Using Automator for Mac I'm going to open a finder file (the .csv) then run a bash script that would create a folder for each ID and move the corresponding image to that folder. A folder can have multiple images.
My bash script so far is:
cd "${1%/*}"
while read line
do
FolderName=${line%,*}
ImageName=${line#*,}
mkdir "$FolderName"
mv "$ImageName" "$FolderName"
done < "$1"
This seems to create the folders fine but then it returns this error:
mv: rename abc.jpg\r to 1030/abc.jpg\r: No such file or directory
And a screenshot of my CSV is as follows:
Any thoughts?
You can remove extraneous carriage returns from ImageName with:
ImageName=$(tr -d '\r' <<< "$ImageName")
You may also like to replace:
mkdir "$FolderName"
with:
mkdir -p "$FolderName"
because it won't emit errors when called with a directory name that already exists - like you say you have.

How do I Batch Rename Folders in OSX?

So I have been trying to rename about 5000 folders based on a CSV (Old name, Newname)
This is a one time operation, once hdkjsh2-2f8c-46b9-bbdb_doc is converted to 3 then it will not need to be touched again.
I have tried the solution here (Setting up an automator workflow with a command script) but found that it does not do a great deal when it comes to folder/directory names and all the guides/documentation is around file names and not folder.
Any suggestions would be much appreciated
Example of CSV
Doc_Location, New_ID
hdkjsh2-2f8c-46b9-bbdb_doc , 3
Please make a backup before trying the following.
Save the following script in your HOME directory as renamer:
#!/bin/bash
cat "file.csv" | while IFS=' ,' read dir new ; do
if [ -d "$dir" ]; then
echo Rename $dir as $new
#mv "$dir" "$new"
else
echo "ERROR: Directory $dir not found - ignored"
fi
done
Then start Terminal and make the script executable by running:
chmod +x $HOME/renamer
Then change directory to where your directories are that need renaming:
cd path/to/things/needing/renaming
Make sure you have your CSV, called file.csv saved in that directory, then run with:
$HOME/renamer
It doesn't actually do anything, it just tells you what it would do. If it looks correct, edit $HOME/renamer and remove the single # on the line that says:
#mv "$dir" "$new"
so that is looks like:
mv "$dir" "$new"
Then be doubly sure you have made a backup and run the script again:
$HOME/renamer
Go to the folder where the other folders you want to rename are located. Select all the folders you want to rename. Then click on the action icon at the top of finder window. This will open a window where one option is to rename x items. See image below.
When you select "Rename x items" you get a box like the one shown below where you can specify the new names.

How to write a bash script using Mac to take files with the same date and put them in a folder with that date

I have hundreds of datalogger files in a directory and I want to write a bash script that will take files with the same date in the filename (an example file name is "2016-06-15T170000_SMARTFLUX.data", where 2016-06-15 is the date) and store them in a folder with the date as the name. I am using a Mac Terminal window, which I believe is Linux (I apologize for my ignorance in computer terminology)
So far I have:
#Type of file (extension) to process:
FILES=*.data
#get date string from file name to use for newly created file
DATE=`ls $FILES|head -n 1|cut -c 1-10`
Any help would be greatly appreciated. I have only modified a bash script that combines these types of files into a text, and I have not created any folders or moved files.
Assuming your script is in the same dir as the data files:
#!/bin/bash
for filename in *.data; do
target_dir=${filename:0:10}
if [[ ! -d $target_dir ]]; then
mkdir $target_dir
fi
mv $filename $target_dir
done

Extract folder names and create directory

I have to extract folder names (folder10 folder44 etc) using for loop and make directory using each folder names but I get file names (file1 file12) and I get an error while creating directory i.e "Directory cannot be created". I have to operate on .txt files in my pipeline so I can't skip /home/data/folder*/file* in for loop
How can I get folder names instead of file names
#!/bin/bash
$out_dir=/home/data/results
for file in /home/data/folder*/file*
do
txtFile=${file##*/}
id=${txtFile%.txt}
echo "mkdir -p $out_dir/"${id}""
done
Folders and File structure
/home/data/folder10/file1/file1.txt
/home/data/folder44/file12/file12.txt
/home/data/folder100/file3/file3.txt
/home/data/folder250/file4/file4.txt
/home/data/folder1245/file5/file5.txt
output which I get
mkdir -p /home/data/results/file1
mkdir -p /home/data/results/file12
mkdir -p /home/data/results/file3
expected output will be
mkdir -p /home/data/results/folder10
mkdir -p /home/data/results/folder44
mkdir -p /home/data/results/folder100
This this:
#!/bin/bash
out_dir=/home/data/results
for file in /home/data/folder*/file*.txt; do
folder=${file%/*}
mkdir -p "$out_dir/${folder##*/}"
done
Or this:
#!/bin/bash
out_dir=/home/data/results
for file in /home/data/folder*/file*/file*.txt; do
folder=${file%/*/*}
mkdir -p "$out_dir/${folder##*/}"
done

Resources