【发布时间】:2018-05-23 19:43:46
【问题描述】:
例如,试图浏览一个文件,
C:\Users\Admin\Desktop\Test\12345
C:\Users\Admin\Desktop\Test\45635
C:\Users\Admin\Desktop\Test\12345-2018-04-21
并创建一个名为“12345”的子文件夹,并将两个文件(12345 和 12345-2018-04-21)存储在子文件夹“12345”中。这将遍历整个文件夹以确保没有副本
:: ---------------------------------------------------------------
:: - This is a program that searches for files with the same 5
:: digit number and puts them in a subfolder.
:: ---------------------------------------------------------------
@ECHO off
SETLOCAL
SET "sourcedir=C:\Users\Admin\Desktop\Test"
SET "destdir=C:\Users\Admin\Desktop\Test"
SET /a lastnum=200000
if "%first5%" equ "%lastnum%"(
SET "destdir=%destdir%\%first5%-sub"
md %destdir%
)
FOR /f "delims=" %%a IN (
'dir /b /a-d /on "%sourcedir%\*" '
) DO (
CALL :detect "%%a"
IF DEFINED dupnum (ECHO(MOVE "%sourcedir%\%%a" "%destdir%\")
)
GOTO end
:: Routine to detect whether the first 5 characters of the filename "%1"
:: are all numeric and if so, whether they match the previous 5-digit number
:: found.
:detect
SET "dupnum="
:: Get the first 5 characters of the filename; prefix with a `1`
SET "fullfilename=%~1"
SET "first5=1%fullfilename:~0,5%"
IF "%first5%" neq "%lastnum%" (
SET "lastnum=%first5%"
GOTO end
)
:: First 5 chars of this filename = first 5 of previous filename
:: Check to see whether numeric
SET /a dummy=first5 + 0
IF "%dummy%" neq "%first5%" (
SET "lastnum=%first5%"
GOTO end
)
SET "dupnum=Y"
GOTO end
ENDLOCAL
:end
PAUSE
不知道为什么我不能让它创建一个文件夹并将重复的文件存储到其中。它将通读我的测试文件夹并注意到重复项并提示移动它们,但我不确定将其移动到新文件夹需要做什么。
【问题讨论】:
-
嗯,你不能在同一个文件夹中有两次相同的文件名,所以我不明白你所说的重复文件是什么意思。
-
让我澄清一下,对不起。他们扫描的文件将它们的 pdf 文件放入其服务器上的文件夹中。如果有副本,它将在名称末尾添加日期。所以我需要检查前5位数字是否相同。
-
律师事务所根据随机获取的信息为每个文件命名,该信息将获得一个 5 位数字。
标签: file batch-file duplicates directory