1

After making a bash file that creates directories and then transfers files from one directory to each created directories I am curious as to how I'd go about doing this in a batch file.

Here's the bash code:

#!/bin/bash
# For each item in file named in $1, make a directory with this name.
#   and copy all files named in file $2 from templates folder to new directory

for user in `cat $1`
do
  if [ -d $user ]
  then 
    echo Directory $user already exists
    rm -r $user
   echo $user has been deleted
  fi   

  mkdir $user
    echo Directory $user created


  for file in `cat $2`
  do
    cp /home/student/Desktop/OS/templates/$file $user
    chmod 700 $user/$file
  done
  echo Directory for $user set up
done

Any input would be greatly appreciated

Funkii
  • 77
  • 4
  • You might want to consider using [tag:powershell], instead of batch. – David Jan 09 '13 at 22:32
  • It would probably be less painful to install a working Bash on Windows with Msys than to do this conversion. – SirDarius Jan 09 '13 at 22:33
  • @SirDarius: Why do you think that? It takes me a couple minutes to do this conversion... – Aacini Jan 09 '13 at 23:38
  • @Aacini this was tongue-in-cheek... mostly... my level of appreciation of the Batch language is really low. As said David, powershell is such a better language. But in the end, I will do such scripting tasks using ruby. I would really like to post a ruby version of this, but it is off-topic. – SirDarius Jan 10 '13 at 09:12
  • @SirDarius and David: There are _many_ people with a low appreciation level of Batch language nowadays, but I think this is because they don't know about the profound changes Batch had have in the last years (or decades perhaps?). You will be surprised of the type of applications that can be written in Batch today: [arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990), but this is also off-topic... – Aacini Jan 10 '13 at 18:51

1 Answers1

1

Although I don't know Bash, I think my Batch version of your program is precise:

@echo off
rem For each line in file named in %1, make a directory with this name.
rem   and copy all files named in file %2 from templates folder to new directory
for /F "delims=" %%u in (%1) do (
   if exist "%%u" (
      echo Directory %%u already exists
      rd /S /Q "%%u"
      echo %%u has been deleted
   )
   md "%%u"
   echo Directory %%u created
   for /F "delims=" %%f in (%2) do (
      copy "/home/student/Desktop/OS/templates/%%f" "%%u"
      rem chmod not exist in Batch
   )
)

However, I would modify previous program in order to read the templates file just once and store its lines in an array:

@echo off
rem For each line in file named in %1, make a directory with this name.
rem   and copy all files named in file %2 from templates folder to new directory
setlocal EnableDelayedExpansion
set temp=0
for /F "delims=" %%f in (%2) do (
   set /A temp+=1
   set template[!temp!]=%%f
)
for /F "delims=" %%u in (%1) do (
   if exist "%%u" (
      echo Directory %%u already exists
      rd /S /Q "%%u"
      echo %%u has been deleted
   )
   md "%%u"
   echo Directory %%u created
   for /L %%i in (1,1,%temp%) do (
      copy "/home/student/Desktop/OS/templates/!template[%%i]!" "%%u"
      rem chmod not exist in Batch
   )
)

Antonio aka "Aacini"

Aacini
  • 65,180
  • 12
  • 72
  • 108