[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文件操作] [已解决]批处理如何检测某目录下是否有指定文件夹,并将路径赋值给变量?

[已解决]bat检测某个盘符根目录下,是否有000-000文件夹。并将路径赋值给变量。

1、如果没有,就直接退出。
2、如果有,就将X:\000-000赋值给变量aaaa,
然后继续检测,D:\0驱动-软件\0-udisk\sd4g\000-000这个目录。
2.1\如果不存在就直接退出。
2.2\如果存在,就执行:
文件夹同步.vbs /C %aaaa% D:\0驱动-软件\0-udisk\sd4g\000-000


注意,实际测试,下面带参数的vbs命令好用。
文件夹同步.vbs /C E:\000-000 D:\0驱动-软件\0-udisk\sd4g\000-000

本帖最后由 ygqiang 于 2016-1-26 18:06 编辑

解决。。。
  1. @echo off
  2. for %%a in (e f g h i j k l m n o p q r s t) do (
  3.     if exist "%%a:\000-000" (
  4.     if exist "D:\0驱动-软件\0-udisk\sd4g\000-000" (
  5. for /f "delims=:" %%i in ('findstr /n "^:sync-copy$" "%~f0"') do (more +%%i "%~f0" > 文件夹同步.vbs)
  6. 文件夹同步.vbs /C %%a:\000-000 D:\0驱动-软件\0-udisk\sd4g\000-000
  7. ping 127.0.0.1 -n 5 >nul 2>nul
  8. if exist 文件夹同步.vbs del /q 文件夹同步.vbs
  9. cd /d D:\0驱动-软件\0-udisk\sd4g\000-000
  10. if exist 文件夹同步.vbs del /q 文件夹同步.vbs
  11.          )
  12.     )
  13. )
  14. exit
  15. ———————————————————————
  16. :sync-copy
  17. 'VBS Sync 同步文件夹
  18. 'VBS 同步文件夹 [翻译+转载 by yu2n]
  19. '功能:  同步文件夹,只更新已修改(或新增)的文件及文件夹,支持删除多余'(目标文件夹中存在,源文件夹不存在)的文件及文件夹
  20. '参数:  sync.vbs  [/RD]  [/RF]  [/C]  <sourcePath>  '<destinationPath>
  21. 'sourcePath  源文件夹
  22. 'destinationPath  目标文件夹z
  23. '/RD 删除目标文件夹中多余的文件夹;
  24. '/RF 删除目标文件夹中多余的文件;
  25. '/C  等于同时使用 /RD /RF 参数
  26. '源代码下载:  'http://www.robotii.co.uk/wp-'content/uploads/2008/10/sync.vbs
  27. '此脚本效率及兼容性还不错
  28. ' Download:  'http://www.robotii.co.uk/wp-'content/uploads/2008/10/sync.vbs
  29. '************************************************************
  30. ' sync.vbs
  31. '
  32. ' This script makes a "synchronised" backup of a folder tree from one
  33. ' path to another.
  34. '
  35. ' Extraneous directories (folders) may be removed from the 'destination
  36. ' tree by specifying the /RD switch on the command line.  These are
  37. ' directories already existing in the destination tree but no longer
  38. ' found in the source tree.
  39. '
  40. ' Extraneous files may be removed from the destination tree by 'specifying
  41. ' the /RF switch on the command line.  These are files which exist in
  42. ' destination directories but not in source directories.
  43. '
  44. ' Both extraneous directories and files can be removed by specifying 'the
  45. ' /C switch on the command line. This switch is equivalent to 'specifying
  46. ' both the /RD and /RF switches
  47. '
  48. ' Usage:  sync.vbs <sourcePath> <destinationPath>
  49. '    or:  cscript /nologo sync.vbs <sourcePath> <destinationPath>
  50. '    or:  wscript /nologo sync.vbs <sourcePath> <destinationPath>
  51. '
  52. ' Notes:  Paths can use drive letters or UNC specifications.
  53. '
  54. '*******************************************************
  55. Dim oArgs
  56. Dim TempArg
  57. Dim bRemoveFolders
  58. Dim bRemoveFiles
  59. Dim SleepTime
  60. Dim SrcFolder
  61. Dim arrSrcFolders()
  62. Dim s
  63. Dim DstFolder
  64. Dim arrDstFolders()
  65. Dim d
  66. Dim arrCopyFrom()
  67. Dim arrCopyTo()
  68. Dim arrSizes()
  69. Dim fMax
  70. Dim TotalSize
  71. Dim oExplorer
  72. Dim FSO
  73. On Error Resume Next
  74.   bRemoveFolders = False
  75.   bRemoveFiles   = False
  76.   SleepTime      = 10    ' default to leaving display onscreen for 10 seconds
  77.   Set oArgs = WScript.Arguments
  78.   For s = 0 To oArgs.Count - 1
  79.     TempArg = Ucase(Left(oArgs(s),3))
  80.     Select Case TempArg
  81.       Case "/?"
  82.             ShowSyntax()
  83.             Wscript.Quit
  84.       Case "/S:"
  85.             SrcFolder = Trim(Mid(oArgs(s),4))
  86.       Case "/D:"
  87.             DstFolder = Trim(Mid(oArgs(s),4))
  88.       Case "/RD"
  89.             bRemoveFolders = True
  90.       Case "/RF"
  91.             bRemoveFiles = True
  92.       Case "/C"
  93.             bRemoveFiles = True
  94.             bRemoveFolders = True
  95.       Case "/T:"
  96.             SleepTime = Cint(Mid(oArgs(s),4))
  97.             If SleepTime < 0 Then
  98.               SleepTime = 0
  99.             End If
  100.       Case Else
  101.             If Left(oArgs(s),1) <> "/" Then
  102.               If SrcFolder = "" Then
  103.                 SrcFolder = oArgs(s)
  104.               ElseIf DstFolder = "" Then
  105.                 DstFolder = oArgs(s)
  106.               Else
  107.                 Wscript.Echo "Unknown parameter on command line... " & oArgs(s)
  108.                 ShowSyntax()
  109.                 Wscript.Quit
  110.               End If
  111.             End If
  112.     End Select
  113.   Next
  114.   If SrcFolder = "" or DstFolder = "" Then
  115.     Wscript.Echo "You must enter a 'SourcePath' and 'DestinationPath' when starting this script."
  116.     ShowSyntax()
  117.     Wscript.Quit
  118.   End If
  119.   If Right(SrcFolder,1) <> "\" Then
  120.     SrcFolder = SrcFolder & "\"
  121.   End If
  122.   If Right(DstFolder,1) <> "\" Then
  123.     DstFolder = DstFolder & "\"
  124.   End If
  125.   If InStr(LCase(SrcFolder),LCase(DstFolder)) > 0 _
  126.   Or InStr(LCase(DstFolder),LCase(SrcFolder)) > 0  Then
  127.     Wscript.Echo "The 'SourcePath' and 'DestinationPath' cannot overlap!"
  128.     Wscript.Quit
  129.   End If
  130.   TotalSize = 0
  131.   Set FSO = CreateObject("Scripting.FileSystemObject")
  132.   Set oExplorer = CreateObject("InternetExplorer.Application")
  133.   oExplorer.Navigate2 "about:blank"
  134.   oExplorer.Width     = 550
  135.   oExplorer.Height    = 300
  136.   oExplorer.Top       = 300
  137.   oExplorer.Left      = 300
  138.   oExplorer.Toolbar   = False
  139.   oExplorer.Menubar   = False
  140.   oExplorer.Statusbar = False
  141.   oExplorer.Visible   = True
  142.   oExplorer.Document.Write "<font face=Verdana>"
  143.   oExplorer.Document.Write "<h2>File Backup in Progress</h2>"
  144.   oExplorer.Document.Write "<p>Beginning backup...<br>"
  145.   oExplorer.Document.Title = "Please be patient.... "
  146.   GetSrcInfo
  147.   GetDstInfo
  148.   CreateMissingFolders
  149.   If bRemoveFolders Then
  150.     DeleteExtraFolders
  151.   End If
  152.   If bRemoveFiles Then
  153.     DeleteExtraFiles
  154.   End If
  155.   FindFilesNeedingBackup
  156.   CopySourceToDest
  157.   oExplorer.document.write "<font color=red>"
  158.   oExplorer.Document.Write "  Finished!<br>"
  159.   If SleepTime > 0 Then
  160.     Wscript.Sleep (SleepTime * 1000)
  161.   End If
  162.   QuitScript()
  163. Sub GetSrcInfo()
  164.   oExplorer.Document.Write "Analyzing source directories...<br>"
  165.   '-- get information about source directories
  166.   If FSO.FolderExists(SrcFolder) Then
  167.     ReDim arrSrcFolders(100)
  168.     s = 0
  169.     arrSrcFolders(0) = Left(SrcFolder, Len(SrcFolder) - 1)
  170.     GetSrcFolders FSO.GetFolder(SrcFolder)
  171.     ReDim Preserve arrSrcFolders(s)
  172.   Else
  173.     Wscript.Echo "Source folder not found... aborting."
  174.     QuitScript()
  175.   End If
  176. End Sub
  177. Sub GetSrcFolders(Folder)
  178.   Dim Subfolder
  179.   For Each Subfolder In Folder.SubFolders
  180.     s = s + 1
  181.     If s > Ubound(arrSrcFolders) Then
  182.       ReDim Preserve arrSrcFolders(Ubound(arrSrcFolders) + 100)
  183.     End If
  184.     arrSrcFolders(s) = SubFolder.Path
  185.     GetSrcFolders Subfolder
  186.   Next
  187. End Sub
  188. Sub GetDstInfo()
  189.   oExplorer.Document.Write "Analyzing backup directories...<br>"
  190.   '-- get information about destination directories
  191.   If Not FSO.FolderExists(DstFolder) Then
  192.     CreateMultiLevelFolder DstFolder
  193.   End If
  194.   ReDim arrDstFolders(100)
  195.   d = 0
  196.   arrDstFolders(0) = Left(DstFolder, Len(DstFolder) - 1)
  197.   GetDstFolders FSO.GetFolder(DstFolder)
  198.   ReDim Preserve arrDstFolders(d)
  199. End Sub
  200. Sub CreateMultiLevelFolder(FolderPath)
  201. Dim TempPath
  202. Dim Path
  203. Dim arrNodes
  204. Dim n
  205.   TempPath = FolderPath
  206.   Path = ""
  207.   ' See if input is in UNC format...
  208.   If Instr(TempPath,"\\") > 0 Then
  209.     TempPath = Mid(TempPath,3)
  210.     n = Instr(TempPath,"\")
  211.     Path = "\\" & Left(TempPath,n)
  212.     TempPath = Mid(TempPath,n+1)
  213.   End If
  214.   arrNodes = Split(TempPath,"\")
  215.   Path     = Path & arrNodes(0)
  216.   For n = 1 To UBound(arrNodes)
  217.     Path = Path & "\" & arrNodes(n)
  218.     If Not FSO.FolderExists(Path) Then
  219.       FSO.CreateFolder(Path)
  220.     End If
  221.   Next
  222. End Sub
  223. Sub GetDstFolders(Folder)
  224.   Dim Subfolder
  225.   For Each Subfolder In Folder.SubFolders
  226.     d = d + 1
  227.     If d > Ubound(arrDstFolders) Then
  228.       ReDim Preserve arrDstFolders(Ubound(arrDstFolders) + 100)
  229.     End If
  230.     arrDstFolders(d) = SubFolder.Path
  231.     GetDstFolders Subfolder
  232.   Next
  233. End Sub
  234. Sub CreateMissingFolders()
  235.   Dim LenOfSrcBaseDir
  236.   Dim strFolder, f
  237.   oExplorer.Document.Write "Creating any missing backup directories...<br>"
  238.   LenOfSrcBaseDir = Len(SrcFolder) + 1
  239.   For f = 0 to s
  240.     strFolder = DstFolder & Mid(arrSrcFolders(f), LenOfSrcBaseDir)
  241.     If Not FSO.FolderExists(strFolder) Then
  242.       FSO.CreateFolder(strFolder)
  243.     End IF
  244.   Next
  245. End Sub
  246. Sub DeleteExtraFolders()
  247.   Dim LenOfDstBaseDir
  248.   Dim strFolder, f
  249.   oExplorer.Document.Write "Removing any extra backup directories...<br>"
  250.   LenOfDstBaseDir = Len(DstFolder) + 1
  251.   For f = 0 To d
  252.     strFolder = SrcFolder & Mid(arrDstFolders(f), LenOfDstBaseDir)
  253.     If (Not FSO.FolderExists(strFolder)) Then
  254.       FSO.DeleteFolder arrDstFolders(f),True
  255.     End IF
  256.   Next
  257. End Sub
  258. Sub DeleteExtraFiles()
  259.   Dim LenOfSrcBaseDir
  260.   Dim LenOfDstBaseDir
  261.   Dim strDstFolder
  262.   Dim strDstFile
  263.   Dim strSrcFile
  264.   Dim oFldr, oFile
  265.   Dim fc, f
  266.   oExplorer.Document.Write "Removing any extra backup files...<br>"
  267.   LenOfSrcBaseDir = Len(SrcFolder) + 1
  268.   LenOfDstBaseDir = Len(DstFolder) + 1
  269.   For f = 0 to s
  270.     strDstFolder = DstFolder & Mid(arrSrcFolders(f), LenOfSrcBaseDir)
  271.     set fc = FSO.GetFolder(strDstFolder).Files
  272.     For Each oFile In fc
  273.       strDstFile = strDstFolder & IIf(f = 0, "", "\") & oFile.Name
  274.       strSrcFile = SrcFolder & Mid(strDstFile, LenOfDstBaseDir)
  275.       If (Not FSO.FileExists(strSrcFile)) And FSO.FileExists(strDstFile) Then
  276.         FSO.DeleteFile strDstFile,True
  277.       End If
  278.     Next
  279.   Next
  280.   Set fc    = Nothing
  281. End Sub
  282. Sub FindFilesNeedingBackup()
  283.   Dim LenOfSrcBaseDir
  284.   Dim strFrom
  285.   Dim strTo, f, fc, oFile
  286.   oExplorer.Document.Write "Determining which files to backup...<br>"
  287.   ReDim arrCopyFrom(100)
  288.   ReDim arrCopyTo(100)
  289.   ReDim arrSizes(100)
  290.   fMax = -1
  291.   LenOfSrcBaseDir = Len(SrcFolder) + 1
  292.   arrSrcFolders(0) = SrcFolder
  293.   arrDstFolders(0) = DstFolder
  294.   For f = 0 To s
  295.     Set fc = FSO.GetFolder(arrSrcFolders(f)).Files
  296.     For Each oFile In fc
  297.       strFrom = arrSrcFolders(f) & "\" & oFile.Name
  298.       strTo   = DstFolder & Mid(strFrom, LenOfSrcBaseDir)
  299.       If Not FSO.FileExists(strTo) Then
  300.         AddToList strFrom, strTo, oFile.Size
  301.       Else
  302.         Set oDstFile = FSO.GetFile(strTo)
  303.         If FileIsNewer(oFile, oDstFile) Then
  304.           AddToList strFrom, strTo, oFile.Size
  305.         End If
  306.       End If
  307.     Next
  308.   Next
  309.   Set fc = Nothing
  310. End Sub
  311. Function FileIsNewer(Source, Dest)
  312.   FileIsNewer = False
  313.   If Source.DateLastModified > Dest.DateLastModified Then
  314.     FileIsNewer = True
  315.   End If
  316. End Function
  317. Sub AddToList(strFrom, strTo, FileSize)
  318.   Dim fCur
  319.   fCur = Ubound(arrCopyFrom)
  320.   fMax = fMax + 1
  321.   if fMax > fCur Then
  322.     ReDim Preserve arrCopyFrom(fCur + 100)
  323.     ReDim Preserve arrCopyTo(fCur + 100)
  324.     ReDim Preserve arrSizes(fCur + 100)
  325.   End If
  326.   arrCopyFrom(fMax) = strFrom
  327.   arrCopyTo(fMax)   = strTo
  328.   arrSizes(fMax)    = FileSize
  329.   TotalSize         = TotalSize + FileSize
  330. End Sub
  331. Sub CopySourceToDest()
  332.   Dim CurPercent, TotalCopied, myProgressIndicator, f
  333.   TotalCopied = 0
  334.   If fMax > -1 Then
  335.     If fMax = 0 Then
  336.       oExplorer.Document.Write "Backing up 1 file...<br>"
  337.     Else
  338.       oExplorer.Document.Write "Backing up " & fMax + 1 & " files...<br>"
  339.     End If
  340.     oexplorer.document.write "<div id='ProgressIndicator' style='background-color:blue;text-align:center; color:white;'></div>"
  341.     Set myProgressIndicator = oExplorer.Document.All("ProgressIndicator")
  342.     For f = 0 To fMax
  343.       If FSO.FileExists(arrCopyTo(f)) Then
  344.         FSO.DeleteFile arrCopyTo(f), True
  345.       End If
  346.       FSO.CopyFile arrCopyFrom(f), arrCopyTo(f)
  347.       TotalCopied = TotalCopied + arrSizes(f)
  348.       CurPercent  = CStr(Round(100 * TotalCopied / TotalSize)) & "%"
  349.       ' Set the current progress indicator
  350.       myProgressIndicator.innerHTML = CurPercent
  351.       myProgressIndicator.style.width = CurPercent
  352.     Next
  353.   Else
  354.     oExplorer.Document.Write "No files need to be backed up...<br>"
  355.   End If
  356. End Sub
  357. Function IIf( expr, truepart, falsepart )
  358.   If expr Then
  359.     IIf = truepart
  360.   Else
  361.     IIf = falsepart
  362.   End If
  363. End Function
  364. Sub QuitScript()
  365.   oExplorer.Quit
  366.   Set oExplorer = Nothing
  367.   Set FSO       = Nothing
  368.   Wscript.Quit
  369. End Sub
  370. Sub ShowSyntax()
  371.   Wscript.Echo "Sync.vbs [/S:]<source> [/D:]<destination> [/RD] [/RF] [/C] [/T:<num>]          "
  372.   Wscript.Echo "                                                                               "
  373.   Wscript.Echo "  All switches are case insensitive as well as 'order insensitive'.  Directory "
  374.   Wscript.Echo "  names with embedded spaces are supported either with or without surrounding  "
  375.   Wscript.Echo "  quotes (single or double), however, if they contain multiple adjacent spaces "
  376.   Wscript.Echo "  they must be quoted.  Spaces after the ':' are ignored.                      "
  377.   Wscript.Echo "                                                                               "
  378.   Wscript.Echo "  /RD specifies to remove extraneous directories (folders) from the            "
  379.   Wscript.Echo "      destination path if found.                                               "
  380.   Wscript.Echo "                                                                               "
  381.   Wscript.Echo "  /RF specifies to remove extraneous files from the destination path if found. "
  382.   Wscript.Echo "                                                                               "
  383.   Wscript.Echo "  /C  Equivalent to specifying /RD and /RF on the command line.                "
  384.   Wscript.Echo "                                                                               "
  385.   Wscript.Echo "  /T:<num> Optional switch to specify number of seconds to leave final         "
  386.   Wscript.Echo "           screen display visible. Defaults to 10 seconds if not specified.    "
  387.   Wscript.Echo "                                                                               "
  388.   Wscript.Echo "  The /S: and /D: switches are optional.  When not used, the first parameter   "
  389.   Wscript.Echo "  on the command line not preceded by '/' will be taken as the source path and "
  390.   Wscript.Echo "  the second parameter not preceded by '/' will be taken as the destination    "
  391.   Wscript.Echo "  path.                                                                        "
  392.   Wscript.Echo "                                                                               "
  393.   Wscript.Echo "  The use of the '-' character in lieu of '/' is not supported.                "
  394.   Wscript.Echo "                                                                               "
  395. End Sub
复制代码

TOP

返回列表