Batch file 批处理文件以读取共享中的文件夹,并将每个文件夹名称附加到路径的末尾

Batch file 批处理文件以读取共享中的文件夹,并将每个文件夹名称附加到路径的末尾,batch-file,Batch File,我正在尝试并行化一个脚本,我用它来备份一些使用TSM的文件 基本上,我运行以下命令: net use y: \\Server\share dsmc incr y: -optfile="c:\path\folder\optionfile.opt" > c:\logs\logfile 。。TSM以串行方式备份整个目录树 但我想这样做是为了创建一些单独的脚本,使用多个客户端来备份目录树的子集(以加快速度) (y:\现在应该是\server\share\first\u d

我正在尝试并行化一个脚本,我用它来备份一些使用TSM的文件

基本上,我运行以下命令:

net use y: \\Server\share

dsmc incr y: -optfile="c:\path\folder\optionfile.opt" > c:\logs\logfile 
。。TSM以串行方式备份整个目录树

但我想这样做是为了创建一些单独的脚本,使用多个客户端来备份目录树的子集(以加快速度)

(y:\现在应该是\server\share\first\u dir\u name)

我仍然需要if逻辑来计算共享中的文件夹数

第一步是查找共享中有多少顶级子目录:

我使用
dir/a:d/s/b“文件夹路径”| find/c:\“
但该命令从未完成

希望这有意义


谢谢。

您可以使用信号量文件执行类似操作。 在本例中,我将其设置为8个单独的任务。 我建议您复制此代码并进行测试,以了解其工作原理,然后更改要同时运行的脚本数,或者找出要同时运行的最佳脚本数,并更改代码以保持该脚本数一直运行到完成为止。你没有说你将有多少独立的脚本

分离过程。bat

@echo off
setlocal EnableDelayedExpansion

set "Script[1]=Script1"
set "Script[2]=Script2"
set "Script[3]=Script3"
set "Script[4]=Script4"
set "Script[5]=Script5"
set "Script[6]=Script6"
set "Script[7]=Script7"
set "Script[8]=Script8"
set /a nScripts=8

rem For test only. Change values (seconds) below if desired
set /A testDelay[1]=10
set /A testDelay[2]=4
set /A testDelay[3]=8
set /A testDelay[4]=14
set /A testDelay[5]=17
set /A testDelay[6]=6
set /A testDelay[7]=9
set /A testDelay[8]=7

rem time to wait between checking status of all tasks
set /A timeout=5
set /A tasksProcessing=0

rem Set all Status to 0 and delete semaphore files.
for /L %%L in (1,1,%nScripts%) do set /A Status[%%L]=0 & del /f /q  "SemaphoreFile!Script[%%L].txt!" 2>nul

echo This program will simulate starting separate processes
echo Open a Windows Explorer to %CD% and observe that semaphore files appear.
echo Observe that the files disappear when the processes complete after varying  delays.
pause

echo Starting %nScripts% processes
for /L %%L in (1,1,%nScripts%) do (
   echo( >"SemaphoreFile!Script[%%L]!.txt"
   start /MIN cmd.exe /C !Script[%%L]!.bat "SemaphoreFile!Script[%%L]!.txt" !testDelay[%%L]!
   set /A Status[%%L]=1
   set /A tasksProcessing+=1
   )

:KeepChecking
for /L %%A in (1,1,%nScripts%) do (
   echo Waiting %timeout% seconds for !tasksProcessing! processes to complete
   timeout %timeout% >nul
   for /L %%L in (1,1,%nScripts%) do (
      if !Status[%%L]!==1 if not exist "SemaphoreFile!Script[%%L]!.txt" (
         set /A Status[%%L]=0
         set /A tasksProcessing-=1
         echo !Script[%%L]! finished.
         )
      )
   if !tasksProcessing! GTR 0 goto :KeepChecking
   goto :Done
   )

:done
echo All processes completed.
pause
endlocal
然后有8个相同的脚本(对于本测试),其中n为1-8

脚本n.bat(代码如下)


执行SeparateProcess.bat以查看其工作原理。当您对结果感到满意时,用您需要的代码替换Script1-8中的代码。只需确保他们做的最后一件事是删除信号量文件,以表明他们已经完成。

看来您已经确定需要一个新的文件。因此,我建议您使用搜索工具(在每一页的顶部),将一些代码放在一起,运行它,如果它不能像编写的那样工作,请发布一篇文章,并附上开头和结果。在此之前,您的问题是一个主题外的代码请求,因为您自己没有充分尝试创建所需的代码。如果您备份到单个设备,那么我怀疑并行运行多个进程是否会显著提高性能。写操作可能已经是您的瓶颈,因此并行执行多个写操作不太可能改善很多。不,我使用TSM的本机工具执行了多个性能分析,写操作速度不是瓶颈,事实上,没有瓶颈。问题是,您只能使用TSM从NetApp SAN运行snappdiff备份,使用一个流到完整卷,而不是子树。但是使用多个客户端请求可以解决这个问题。关于\\Server\share中有多少子目录?当然,您可以执行代码块。例如,见@RGuggisberg的答案。嗯,我不知道这里发生了什么,老实说,你能告诉我吗?我不确定这是如何解决上述问题的。SeparateProcess.bat启动n个单独的进程(本例中为8个)。它为每个进程编写一个信号量文件,然后等待进程完成。。。它们通过删除信号量文件来表示。制作8份Scriptn副本,并将其命名为Scriptn(1-8).bat。运行SeparateProcess.bat并观察流程完成情况。当然,当你确信脚本正在做你想做的事情后,你会用脚本替换Scriptn(1-8)。我不认为这是在做我需要的事情。我不想写很多脚本,我需要写一个脚本来读取fileshare的内容,然后将每个文件夹名替换到TSM命令的路径中。(如上所述)SeparateProcess.bat将作为您的引擎。只要同时运行进程,它就可以做您想要的事情。脚本n.bat仅用于测试。它经过测试,可以工作。我无法测试你的代码。由您自己决定是否使用脚本替换Scriptn.bat,可以是一个脚本,也可以是多个脚本。
FOR /D %%a IN (W:) DO ( 

    net use y: \\server\share\%%a
    run command to backup y: 
    net use y: /delete

    timeout /t 10
)
@echo off
setlocal EnableDelayedExpansion

set "Script[1]=Script1"
set "Script[2]=Script2"
set "Script[3]=Script3"
set "Script[4]=Script4"
set "Script[5]=Script5"
set "Script[6]=Script6"
set "Script[7]=Script7"
set "Script[8]=Script8"
set /a nScripts=8

rem For test only. Change values (seconds) below if desired
set /A testDelay[1]=10
set /A testDelay[2]=4
set /A testDelay[3]=8
set /A testDelay[4]=14
set /A testDelay[5]=17
set /A testDelay[6]=6
set /A testDelay[7]=9
set /A testDelay[8]=7

rem time to wait between checking status of all tasks
set /A timeout=5
set /A tasksProcessing=0

rem Set all Status to 0 and delete semaphore files.
for /L %%L in (1,1,%nScripts%) do set /A Status[%%L]=0 & del /f /q  "SemaphoreFile!Script[%%L].txt!" 2>nul

echo This program will simulate starting separate processes
echo Open a Windows Explorer to %CD% and observe that semaphore files appear.
echo Observe that the files disappear when the processes complete after varying  delays.
pause

echo Starting %nScripts% processes
for /L %%L in (1,1,%nScripts%) do (
   echo( >"SemaphoreFile!Script[%%L]!.txt"
   start /MIN cmd.exe /C !Script[%%L]!.bat "SemaphoreFile!Script[%%L]!.txt" !testDelay[%%L]!
   set /A Status[%%L]=1
   set /A tasksProcessing+=1
   )

:KeepChecking
for /L %%A in (1,1,%nScripts%) do (
   echo Waiting %timeout% seconds for !tasksProcessing! processes to complete
   timeout %timeout% >nul
   for /L %%L in (1,1,%nScripts%) do (
      if !Status[%%L]!==1 if not exist "SemaphoreFile!Script[%%L]!.txt" (
         set /A Status[%%L]=0
         set /A tasksProcessing-=1
         echo !Script[%%L]! finished.
         )
      )
   if !tasksProcessing! GTR 0 goto :KeepChecking
   goto :Done
   )

:done
echo All processes completed.
pause
endlocal
@echo off
REM 1=Filespec of semaphor file
REM 2=Delay in seconds. This is for test only.
echo %~nx1 is doing something
timeout %~2
del "%~1"
got :eof