2016-04-07 7 views
2

私はメモを取るために毎日使うlibreoffice Writerファイルをバックアップするためのスクリプトを書いています。このスクリプトはWindowsの起動時に実行され、ライタファイルと最後のバックアップファイル(xcopyを使用)を比較し、変更があった場合は新しいローカルバックアップコピー(最大100ファイル)を作成し、Rarアーカイブを作成しますWinrar)にアップロードし、新しいバックアップをGoogleドライブにアップロードします(rclone同期を使用)。Windows Script DelayedExtensionが機能しない

SETLOCAL ENABLEDELAYEDEXPANSIONを使用しているにもかかわらず、ERRORLEVELは常に0です。たとえば、ファイルをアップロードするときにrclone同期がうまくいかないときを知りたいとします。

私はif "!errorlevel!" == "0"if !errorlevel! 0、i f not errorlevel 1をスクリプト内のすべての発生について試しましたが、誰も働いていませんでした。 上記の行をすべて削除すると、:mkrar!errorlevel!正常に動作します!

私はWindowsスクリプティングではプロフェッショナルではなく、ちょうどいくつかのコマンドを知っています。私のコードは効率的ではないことが分かります。正しいエラーレベルを取得する方法を見つける必要があります。

Setlocal ENABLEDELAYEDEXPANSION 
rem ** Check if required text files exist ** 
if not exist "D:\Backup\My Private Notes Backup\Last-Backup-Num.txt" goto error 
if not exist "D:\Backup\My Private Notes Backup\Compare.txt" goto error 
set oldnum=0 
rem ** Load the number of the last backup file ** 
set /P oldnum=<"D:\Backup\My Private Notes Backup\Last-Backup-Num.txt" 
rem ** Delete the extra "space" from the last number saved ** 
set oldnum=%oldnum: =% 
echo %oldnum% 
set newnum=0 
set /A newnum=oldnum+1 
if %newnum% GTR 100 set newnum=1 
set src=empty 
set oldback=D:\Backup\My Private Notes Backup\%oldnum%.odt 
set newback=D:\Backup\My Private Notes Backup\%newnum%.odt 

rem ** Check if all required odt files exist ** 
if not exist "E:\My Private Notes.odt" goto error 
if not exist "%oldback%" goto error 

if not exist "%newback%" goto error 

rem ** Compare the source file with the last backup file, to see if new backup is necessary ** 
xcopy "E:\My Private Notes.odt" "%oldback%" /D /Y /L > "D:\Backup\My Private Notes Backup\Compare.txt" 
set /P src=<"D:\Backup\My Private Notes Backup\Compare.txt" 
if "%src%"=="E:\My Private Notes.odt" (
rem ** Changes detected, create new backup file ** 
    xcopy "%src%" "%newback%" /Y 
    goto okay 
) else (
rem ** No change from the last backup detected ** 
     exit 
) 

:okay 
rem ** If the file was copied successfully, replace oldnum with newnum and save it as last backup number and run RAR2Drop ** 
if "!errorlevel!" == "0" (
echo %newnum% >"D:\Backup\My Private Notes Backup\Last-Backup-Num.txt" 
goto mkrar 
) else (goto error 
) 

rem ** Compress the New Backup File into a RAR Archive for Upload ** 
:mkrar 
rem ** The source file is the file just created as backup ** 
set src="%newback%" 
rem ** The Rar file name is the file number of new backup without ".rar" ** 
set newback="D:\Backup\My Private Notes Backup\%newnum%" 
rem ** Delete possible Rar files left from last script run ** 
del %newnum%.rar 
"C:\Program Files\WinRAR\winrar.exe" a -ma5 -hp5rte453 -m5 -ibck -ep %newback% %src% 
if "!errorlevel!" == "0" (
goto upload 
) else (goto rar-error 
) 

rem ** Upload the Rar File to Google Drive and Delete the Rar File ** 
:upload 
set newback=%newback%.rar 
"D:\Backup\My Private Notes Backup\rclone" --config "D:\\Backup\\My Private Notes Backup\\rclone.conf" copy %newback% remote:MyCisNoBacks 
del %newback% 
if "!errorlevel!" == "0" (
exit 
) else (goto up-error 
) 


:error 
echo There was an error while copying Private Notes Backup file ! 
Pause 
exit 

:rar-error 
echo There was an error while creating Rar Archive 4 Private Notes Backup file ! 
Pause 
exit 

:up-error 
echo There was an error while Uploadin Private Notes Rar Backup file to DropBox ! 
Pause 
exit 
+2

"errorlevel 1"(これは1インチ以上のエラーを意味する)を使用することを検討してください(https://support.microsoft.com/en-us/kb/69576)。また、なぜ遅延が必要ですか拡張? –

+0

AFAIRループ内にはDELAYEDEXPANSIONだけが必要です。あなたの場合は%errorlevel%を普通に確認してください。 –

+0

これはDELAYEDEXPANSIONに関係しているのか、DELAYEDEXPANSIONを使うべきなのか分かりません。 winrarまたはrcloneの同期がエラーで終了しても、通常の%errorlevel%は常に0を返します。 – Jackool

答えて

0

は突然、私はERRORLEVELは常に0を返すために原因をrcloneコマンドの後に実行されデル%newback%コマンドは(もうrcloneのerrorlevleではありません)エラーレベルは常に0を返すようになります。ました del%newback%コマンドをスクリプトの2行下に移動し、問題は終了しました。

関連する問題