BATCH:如何移动索引低于最高索引的文件

BATCH : How to move files with index lower than the highest

我正在尝试将索引低于最高索引的文件从目录 1 移动到目录 2。

我以为我只需要做一个简单的 FOR 循环,但我无法对索引进行 REGEX,我什至无法找到一种方法来比较它们之间的索引

我暂时尝试了什么(什么也没做):

FOR %%i IN (C:\path\TEST_BAT\*) DO (SET current_path=%%~ni
SET C|findstr /r "ind.{1}" %current_path%
ECHO %C%)

想象一下有一套:

我想要运行一个脚本,将每个文件移动到另一个目录,除了文件名中带有 indB 的文件,因为这是此时的最高索引。

据我了解。您希望根据 ind# 状态移动下面示例中不包括最高索引的所有文件。

  • 1234-5678-ind0-example.swf - 移动文件
  • 1234-5678-indA-example.swf - 移动文件
  • 1234-5678-indB-example.swf - 不要移动文件

对于我的解决方案,我们将从文件名中提取 int#,将它们导出到一个文件,对所述文件进行排序,然后移动不包括最高索引的文件。为此,我们可以简单地使用 find /V "indB" 但是这个批处理文件获取动态数据并创建一个解决方案。它总是会以 1234-5678-ind0-example 格式的 3' delims 进行比较,这就是你想要的。

MoveButExcludeHighestIndex.bat:

@echo off
@setlocal enabledelayedexpansion

Rem | Configure Directories
Set "MainDirrectory=C:\folder"
Set "MoveToDirrectory=C:\new folder"

Rem | Get All Folders Locations In X Directory
for %%A in ("!MainDirrectory!\*") do (

    Rem | Get Each ind# From %%A
    for /f "tokens=3 delims=-" %%B in ("%%~nA") do (

        Rem Save Results To File
        Echo %%B>> SortNumbers.TEMP

    )
)

Rem | Sort TextFile - Script By dbenham
set "file=SortNumbers.TEMP"
>"%file%.new1" (
  for /f "usebackq tokens=*" %%A in ("%file%") do (
    set "n=000000000000000%%A"
    setlocal enableDelayedExpansion
    echo !n:~-15!
    endlocal
  )
)
>"%file%.new2" sort /r "%file%.new1"
>"%file%" (
  for /f "usebackq tokens=* delims=0" %%A in ("%file%.new2") do echo %%A
)
del "%file%.new?"

Rem | Grab Highest Index From TextFile
for /f "tokens=* delims=" %%A in ('Type SortNumbers.TEMP') do (
    set /a "count+=1"
    set "Line[!count!]=%%A"
)

Rem | Get All Folders Locations In X Directory
for %%A in ("!MainDirrectory!\*") do (

    Rem | Find Files Without Highest Index
    for /f "tokens=* delims=" %%B in ('Echo %%A^| find /V "!Line[1]!"') do (

        Rem | Move Files
        Set "CopyFile=%%B"
        Move "!CopyFile!" "!MoveToDirrectory!"

    )
)
del "SortNumbers.TEMP"
goto :EOF

如需任何命令的帮助,请执行以下操作:

  • call /?
  • set /?
  • for /?
  • if /?
  • find /?
  • 等等。