文件夹用于批处理脚本的另一进程

时间:2018-06-25 14:39:11

标签: powershell batch-file

我编写的备份脚本有问题。备份非常简单。它将目录复制到目标路径,然后将其压缩。我有几个If子句,例如,如果以前的备份中有5个或更多.zip,则删除最旧的备份。

我面临的问题是:XCOPY命令运行完毕后,我随后从批处理中运行PowerShell脚本来压缩备份,但出现此错误:

enter image description here

发生这种情况是因为.bat文件正在运行。我检查了。

批处理和Power Shell脚本的代码如下:

BATCH:

echo "========================================="
echo "=====| Backuping DCT Light's Files |====="
echo "========================================="

SET dct-light_src=\\w102xnk172\c$\inetpub\wwwroot\DCT_NEW

For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set dct_light_startting_date=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set dct_light_startting_time=%%a%%b)

SET starttime=%dct_light_startting_date%

CD C:\Users\william_silva4\Desktop\tools_backup\dct_light\
SETLOCAL ENABLEDELAYEDEXPANSION
SET /A "N=0"
FOR /F %%f IN ('DIR /B /A:-D "*"') DO (SET /A "N=!N!+1")

IF %n% == 5 (

powershell.exe -noexit -file "C:\Users\william_silva4\Desktop\remove_oldest.ps1"

) ELSE (
    IF EXIST C:\Users\william_silva4\Desktop\tools_backup\dct_light\dctlight_backup_%starttime%\ ( 
        MD C:\Users\william_silva4\Desktop\tools_backup\dct_light\dctlight_backup_%starttime%\dctlight_backup
        SET dct-light_dtn=C:\Users\william_silva4\Desktop\tools_backup\dct_light\dctlight_backup_%starttime%\dctlight_backup
        echo A folder for this backup already exists. Beggining overwrite...
    ) ELSE ( 
        MD C:\Users\william_silva4\Desktop\tools_backup\dct_light\dctlight_backup_%starttime%\dctlight_backup
        SET dct-light_dtn=C:\Users\william_silva4\Desktop\tools_backup\dct_light\dctlight_backup_%starttime%\dctlight_backup\
    )   
)
CD C:\Users\william_silva4\Desktop\backup_completo
XCOPY %dct-light_src% %dct-light_dtn% /w /e /y /EXCLUDE:C:\Users\william_silva4\Desktop\backup_completo\exclusion.txt

For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set dct_light_finished_date=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set dct_light_finished_time=%%a%%b)

SET startdate=%dct_light_finished_date%
SET starttime=%dct_light_startting_date%_%dct_light_startting_time%
SET finishedtime=%dct_light_finished_date%_%dct_light_finished_time%

CD C:\Users\william_silva4\Desktop\tools_backup\dct_light\dctlight_backup_%startdate%\

echo.LOG of %starttime%'s Backup>LOG_%startdate%.txt
echo.Start time: %starttime%>>LOG_%startdate%.txt
echo.Finished time: %finishedtime%>>LOG_%startdate%.txt

CD C:\Users\william_silva4\Desktop\tools_backup\dct_light\dctlight_backup_%startdate%\dctlight_backup\

SETLOCAL ENABLEDELAYEDEXPANSION
SET /A "N=0"
FOR /F %%f IN ('DIR /S /B /A:-D "*"') DO (SET /A "N=!N!+1")

CD ../
echo.Files copied: %N%>>LOG_%startdate%.txt

powershell.exe -noexit -file "C:\Users\william_silva4\Desktop\zip_backups.ps1"

pause>nul

电源外壳:

#DCT Light's zipping
Start-Sleep -s 15
$source = "C:\Users\william_silva4\Desktop\tools_backup\dct_light"
$date = Get-Date -UFormat "%Y-%m-%d"
$destination = "C:\Users\william_silva4\Desktop\tools_backup\dct_light\dctlight_backup_$date.zip"

If(Test-path $destination) {Remove-item $destination}
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($source, $destination)

Remove-Item $source\* -Recurse -Exclude *.zip

我试图解决的问题:

  1. 使用要运行的脚本列表创建一个Master.ps1,其中第一个脚本显然是备份批处理文件,然后是压缩ps1文件。
  2. 从另一个窗口上的备份批处理文件调用zip1压缩文件,然后终止该批处理。在这种情况下,压缩脚本的第一行是:

    Start-Sleep -s 15

因此,我确定在我真正开始压缩之前,该批次已被杀死。

以上方法均无效。有什么帮助吗?如果没有任何效果,我将它们安排为Windows上的单独任务。

0 个答案:

没有答案