iT邦幫忙

0

關於批次檔的問題

檔案正在複製到x:磁碟中:因x磁碟的容量有限,在複製過程中導致複製失敗。

我想製作批次檔,複製檔案到某磁碟前,首先檢查z:的容量夠不夠?若不夠,再跳至Y:再檢查Y:的容量,若Y:容量夠的,就繼續把檔案複製到Y:...
if exist z:容量夠(500mb) copy xxx.zip z:\tools\xxx.zip(500mb)
if exist x:容量夠(500mb) copy xxx.zip x:\tools\xxx.zip(500mb)
if exist y:容量夠(500mb) copy xxx.zip y:\tools\xxx.zip(500mb)
請各位大人知道怎麼做法?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

26
海綿寶寶
iT邦大神 1 級 ‧ 2008-12-19 23:40:43
最佳解答

批次檔裡沒有偵測可用空間的指令
而且可用空間在每一次複製檔案進去之後也會變動
因此以下換成另一種做法
看看能不能達到你的目的

原理是利用每次執行DOS指令後回傳的狀態碼來判斷
如果ERRORLEVEL=0表示複製成功
如果ERRORLEVEL=1表示複製失敗(就試下一個磁碟)

批次檔如下供參考

<pre class="c" name="code">
@echo off

:try1
copy xxx.zip z:\tools\xxx.zip
if errorlevel 1 goto try2
echo File was copied to [Z] successfully.
goto end

:try2
copy xxx.zip x:\tools\xxx.zip
if errorlevel 1 goto try3
echo File was copied to [Z] successfully.
goto end

:try3
copy xxx.zip y:\tools\xxx.zip
if errorlevel 1 goto error
echo File was copied to [Z] successfully.
goto end

:error
echo Error happened.
goto end

:end
echo End.
12
jsperng
iT邦研究生 1 級 ‧ 2008-12-23 15:02:47

建議你使用強大的Host script,將下列文字存成 .vbs,用檔案總管或排程都可以直接執行。
範例只抓取 D 槽的可用空間顯示出來,複製的語法,如有需要才提吧

<pre class="c" name="code"> Dim fso, d, s, drvPath
   drvPath = "D:"
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set d = fso.GetDrive(fso.GetDriveName(drvPath))
   s = "Drive " & UCase(drvPath) & " - " 
   s = s & d.VolumeName
   s = s & " 可用空間: " & FormatNumber(d.FreeSpace/1024, 0) 
   s = s & " Kbytes"
   MsgBox(s)

我要發表回答

立即登入回答