仅合并2个.bats

时间:2018-06-16 03:04:03

标签: batch-file

我在下面有这个脚本:

@echo off & setlocal
del /f /s /q  %temp%\DuplicateRemover.txt 
del /f /s /q %temp%\DuplicateRemover.bat

echo SetLocal DisableDelayedExpansion >>%temp%\DuplicateRemover.txt
echo @echo off ^& setlocal >>%temp%\DuplicateRemover.txt
echo rem Group all file names by size >>%temp%\DuplicateRemover.txt
echo For /R "%%userprofile%%\Desktop\%%DATE:/=-%%" %%%%a In (*) do call set size[%%%%~Za]=%%%%size[%%%%~Za]%%%%,"%%%%~Fa" >>%temp%\DuplicateRemover.txt
echo rem Process groups >>%temp%\DuplicateRemover.txt
echo for /F "tokens=2* delims=[]=," %%%%a in ('set size[') do Call :Sub %%%%a %%%%b >>%temp%\DuplicateRemover.txt
echo Goto ^:Eof >>%temp%\DuplicateRemover.txt
echo ^:Sub >>%temp%\DuplicateRemover.txt
echo If "%%~3"=="" (Set "size[%%1]="^&goto :EOf) >>%temp%\DuplicateRemover.txt
echo processing %%* >> %temp%\DuplicateRemover.txt
echo Keep %%2 >> %temp%\DuplicateRemover.txt
echo Shift^&shift >> %temp%\DuplicateRemover.txt
echo :loop >> %temp%\DuplicateRemover.txt
echo Del  %%1 >> %temp%\DuplicateRemover.txt
echo if not "%%~2"=="" (shift^&goto :loop) >>%temp%\DuplicateRemover.txt

ren "%temp%\DuplicateRemover.txt" DuplicateRemover.bat

set "spool=%systemroot%\System32\spool\PRINTERS"
set "output=%userprofile%\Desktop\%date:/=-%"

rem Timeout for loop cycle.
set "sleeptime=1"

if not exist "%output%" mkdir "%output%"


:loop

setlocal
call %temp%\DuplicateRemover.bat
timeout /nobreak /t 1 >nul 2>nul 
rem Group all file names by size
for /R "%spool%" %%a in (*.spl) do call set size[%%~Za]=%%size[%%~Za]%%,"%%~Fa"
2>nul set size[|| (
    endlocal
    >nul timeout /t %sleeptime% /nobreak
    goto :loop
)
rem Process groups
for /F "tokens=2* delims=[]=," %%a in ('set size[') do call :Sub %%a %%b

endlocal
>nul timeout /t %sleeptime% /nobreak
goto :loop

exit /b 0

:Sub
setlocal

@rem If "%~3"=="" (set "size[%1]=" & exit /b 1)
echo processing %*

rem Skip 1st argument.
set "skip1="

for %%a in (%*) do (
    if not defined skip1 (
        set skip1=1
    ) else if not exist "%output%\%%~NXa" (
        rem Unique name
        echo Keep: "%%~a"
        copy "%%~a" "%output%\%%~NXa" >nul 2>nul
    ) else (
        for %%b in ("%output%\%%~NXa") do (
            for %%c in ("%%~a") do (
                if "%%~Zb" == "%%~Zc" (
                    rem Same name same size
                    call :SaveAs "%%~a" "%output%\%%~NXa"
                ) else (
                    rem Same name different size
                    call :SaveAs "%%~a" "%output%\%%~NXa"
                )
            )
        )
    )
)
exit /b 0

rem Renames to output with an index number.
:SaveAs
setlocal
set "name=%~dpn2"
:NewNameLoop
set /a i+=1
if exist "%name%(%i%).spl" goto :NewNameLoop
echo Keep: "%~1" as "%name%(%i%).spl"
copy "%~1" "%name%(%i%).spl" >nul 2>nul
exit /b 0

当脚本运行时,它会创建另一个与主脚本一起工作的.bat。 主脚本从假脱机文件中复制文件并将其粘贴到输出文件夹中,而不会停止复制同一文件。第二个脚本的功能是删除这些重复的文件,通过特定的文件大小识别它。

它的工作效率为75%。有时,第二个脚本没有时间删除重复的文件。我想最好只将这两个脚本合并为一个。所以它会更好。

有人可以帮助我,我该怎么办?

1 个答案:

答案 0 :(得分:1)

为什么文件大小相同? 这些是在不同的文件夹中吗?

使用versioning system可以更轻松地完成此操作。

@echo off
setlocal
set prompt=$g$s

:: This is a versioning system
:: Transfer of none or one or more parameters (folders / files)
:: A folder is created on the same level as the original folder.
:: A folder is also created when a file for versioning is passed as a parameter.
:: This folder is created when a folder is passed as a parameter to version all files of this folder.
:: Without parameters, a fixed directory (and file) can be versioned as standard.
:: A log file is maintained in the versioning folder.
:: Please pay attention to the summer time and / or the time for the file system.
:: The variable rCopyCMD is used to pass other Robocopy options.
:: The versioned file gets the current time stamp as a version feature.

set       "folderOriginal=d:\WorkingDir"

::::::::::::::::::::::::::::::::::::::::::::::
set        "filesOriginal=*"
set        "folderVersions=.Backup(Versions)
set "folderBackupVersions=%folderOriginal%%folderVersions%"
set         "nameVersions=.(v-timeStamp)"
set      "fileLogVersions=%folderBackupVersions%\Log.(Versions).log"

:getAllParameters
if :%1 equ : goto :EndParameter
if exist %1\ (
  set       "FolderOriginal=%~1"
  set "folderBackupVersions=%~1%folderVersions%"
  set        "filesOriginal=*"
) else (
  set       "FolderOriginal=%~dp1"
  for %%i in ("%~dp1\.") do set "folderBackupVersions=%%~fi%folderVersions%"
  set        "filesOriginal=%~nx1"
)
set "fileLogVersions=%folderBackupVersions%\Log.(Versions).log"

:EndParameter

call :TAB
set "timeStamp=."

set "rCopyCmd= /njh /ts /fp /ns /nc /np /ndl /njs "

for %%F in ("%folderOriginal%\%filesOriginal%"
) do (
  set "timeStampFileName="
  set "versionTimeStamp="
  for /f "tokens=2,3delims=%TAB%" %%A in ('
    robocopy /L "%folderBackupVersions%" ".. versions Listing only..\\" ^
     "%%~nF%nameVersions:timeStamp=*%%%~xF" %rCopyCmd% ^|sort ^& ^
    robocopy /L "%%~dpF\" ".. original List only ..\\" "%%~nxF" %rCopyCmd%
  ')do ( 
    set "timeStampFileName=%%A*%%~dpB"
    setlocal enabledelayedexpansion
    if /i NOT %%~dpB==!folderBackupVersions!\ if %%A gtr !versionTimeStamp! (
      call :getCurrent.timestamp
      for /f "tokens=1-3delims=*" %%S in ("%nameVersions:timeStamp=!timeStamp!%*!timeStampFileName!"
      ) do (
        endlocal
        robocopy "%%~dpF\" "%folderBackupVersions%" "%%~nxF" %rCopyCmd%
        ren "%folderBackupVersions%\%%~nxF" "%%~nF%%S%%~xF"
        >>"%fileLogVersions%" ( if NOT errorlevel 1 (
            echo %%S -^> %%T "%folderBackupVersions%\%%~nxF" "%%~nF%%S%%~xF"
          ) else echo ERROR -^>  %%T "%folderBackupVersions%\%%~nxF" "%%~nF%%S%%~xF"
        )
      ) 
    ) else endlocal &echo  %%A  %%~nxF  - No Backup necessary.
    if .==.!! endlocal
    set "versionTimeStamp=%%A"
  )
)
if NOT :%2==: shift & goto :getAllParameters
pause
exit /b

:TAB
for /f "delims= " %%T in ('robocopy /L . . /njh /njs') do set "TAB=%%T"
 rem END TAB
exit /b

:getCurrent.timestamp
 rem robocopy /L "\.. Timestamp ..\\" .
for /f "eol=D tokens=1-6 delims=/: " %%T in (' robocopy /L  /njh "\|" .^|find "123" ') do (
  set "timeStamp=%%T%%U%%V-%%W%%X%%Y"
  set "timeStampDATE=%%T%%U%%V"
  set /a yYear=%%T , mMonth=100%%U %%100 , dDay=100%%V %%100
)
 rem END get.currentTimestamp
exit /b