2

Trying to automate some work stuff and this includes many folder generations but and I was thinking of using a dynamic array to do this, let me know how this sounds.

First Part of my Script is to export a directory list using the:

ls /home/USER/Downloads/ > "/home/USER/Desktop/scripts/FolderList.txt"

and it gives the output as expected:

Folder With Some Long Title
Folder With Another Long Title

In this example I only gave (2) but there could be upwards of 1500+ folders and I have already written the commands to create the other folders on my server and name them automatically based off the original folder name, but the problem I have is I want my scripts to index thru all the folders and preform my commands (there are about 8 that are mostly string manipulations and such). So I was thinking of getting the count of the number of items in the Directory then write them to the text file mentioned above, then using a FOR-Loop to run that number of times that will contain my 8 or so commands? any thoughts or better ideas? I Can get the count using:

wc -l FolderList.txt | awk '{ print $1 }'

But I'm not sure how to create or manage a Dynamic Array and use it with the for-loop to index thru my commands.

here is some of the script that goes with it currently some commented out for modular testing:

#TestString="Customer Info (YEAR) [PMNAME] [OWNERNAME] [EETYPE]"
cd "/home/USER/Downloads/"
ls /home/USER/Downloads/ > "/home/USER/Desktop/scripts/FolderList.txt"
wc -l FolderList.txt | awk '{ print $1 }'

#CUSTINFO=${TestString/(*)*}
#YEAR=$(grep -o '(.*)' <<<"$TestString")
#RESO3=$(grep -o '\[.*\]' <<<"$TestString")
#RESO2=${RESO3//\[PMNAME\]/}
#RESO_FINAL=${RESO2//\[EETYPE\]/}
#FULL_NAME=$TITLE_FINAL$CUSTINFO$YEAR
#mv "/home/USER/Downloads/$DOWNLOAD_NAME" "/home/USER/Downloads/$FULL_NAME"
#for f in */* ;do fp=$(dirname "$f"); ext="${f##*.}" ; mv "$f" "$fp"/"$fp"."$ext" ;done
Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
user354662
  • 35
  • 3
  • all functionality built into script since the read location is always the same. so just {./script.sh} – user354662 May 30 '19 at 04:35
  • So the script has hardcoded foldernames? – muru May 30 '19 at 04:36
  • yes, since the read location is always the same, i copy folders form my win pc to my linux VM to run this – user354662 May 30 '19 at 04:37
  • i added the majority of my commands that i use to generate the new folder names and such there is more that is still in my testing but all of the commands i posted are working – user354662 May 30 '19 at 04:52
  • so the `TestString` here is supposed to be the name of a directory in `/home/USER/Downloads/`, or a line in `FolderList.txt`? – muru May 30 '19 at 04:54
  • TestString is one of the lines that would be read in from the FolderList.txt and will be a folder with name formatted like "Microsoft Kadant Corp (2015) [AlexSmith] [Marky Mark] [IOCount]" me initializing a string value was just for testing the other string manipulation commands as i write them – user354662 May 30 '19 at 04:57

1 Answers1

0

Going by the commands and comments, it looks like what you want to do is:

for TestString in /home/USER/Downloads/*
do 
    #CUSTINFO=${TestString/(*)*}
    #YEAR=$(grep -o '(.*)' <<<"$TestString")
    #RESO3=$(grep -o '\[.*\]' <<<"$TestString")
    #RESO2=${RESO3//\[PMNAME\]/}
    #RESO_FINAL=${RESO2//\[EETYPE\]/}
    #FULL_NAME=$TITLE_FINAL$CUSTINFO$YEAR
    #mv "/home/USER/Downloads/$DOWNLOAD_NAME" "/home/USER/Downloads/$FULL_NAME"
    #for f in */* ;do fp=$(dirname "$f"); ext="${f##*.}" ; mv "$f" "$fp"/"$fp"."$ext" ;done
done

The outer for loop loops over files in /home/USER/Downloads/ (by wildcard expansion), setting TestString to each filename in turn.

muru
  • 69,900
  • 13
  • 192
  • 292