1

ディレクトリを作成し、あるディレクトリから作成された各ディレクトリにファイルを転送するbashファイルを作成した後、バッチファイルでこれを行う方法に興味があります。

バッシュコードは次のとおりです。

#!/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

どんな入力でも大歓迎です

4

1 に答える 1

1

私はBashを知りませんが、あなたのプログラムの私のバッチバージョンは正確だと思います:

@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
   )
)

ただし、テンプレートファイルを1回だけ読み取り、その行を配列に格納するために、前のプログラムを変更します。

@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
   )
)

アントニオ別名「Aacini」

于 2013-01-09T23:33:55.213 に答える