根据某些文件名模式将文件排序并移动到目录中

时间:2016-10-09 14:26:46

标签: powershell batch-file

要将文件移动到文件夹,我使用此脚本

cd C:\Users\Administrator\Desktop\T\
"C:\Users\Administrator\Desktop\T\build-folder-hierarchy.bat" "C:\Users\Administrator\Desktop\T\" "*.pdf"

要使用脚本我必须

1- open cmd
2-执行批处理我必须

aaaa aaaa S02 [folder]
aaaa aaaa S02e01.pdf [folder]
aaaa aaaa S02e02.pdf [folder]
aaa.aaaa.aaa.aa.aaaaa.S02 [folder]

但问题是什么?

对于每个.pdf文件批处理创建一个相对文件夹,但我不希望它以这种方式创建文件夹。看http://i.imgur.com/TVhQyzv.png

├─aaaa aaaa S02 [folder]
│ ├─aaaa aaaa S02e01.pdf[file]
│ ├─aaaa aaaa S02e02.pdf [file]
  └─ ....
├─aaa.aaaa.aaa.aa.aaaaa.S02 [folder]
│ └─aaa.aaaa.aa.aa.aaaaa.S02E13.pdf [file]
:

我想要的是什么?

aaaaaaaaa aa aaaaa S01e12 720p Repack.pdf
aaa aaaaaaaaa S01e05 Versione 720p.pdf
aaa aaaaaaaa S01e05 Versione 1080p.pdf
aaa aaaa s2e06.pdf
aaa aaaa S03e12.pdf
aaa.aaaa.aaa.on.Earth.S02E13.pdf
aaa.aaaa.aaaa.S02E01.HDTV.x264.SUB.ITA.pdf

只是一个示例名称,用于了解 .pdf 文件名称的格式

s01
s01e1
s1
s1e1
s1e01
s1e01-10

通常pdf文件名的格式为[pattern]

e

字符,如ssxx sxxex sx sxex sxexx sxexx-xx 几乎总是存在于这些模式名称中 一般形式应该是

list.clear()

X 是一个数字,字母为 s ,而 e 则不相关

Powershell解决方案已被广泛接受。

2 个答案:

答案 0 :(得分:1)

获取正则表达式子字符串的最后一个实例的最简单方法是将字符串分解为块并以相反的顺序处理块。

:: A script for grouping PDF files based on book series name
:: http://i.imgur.com/seh6p.gif

@echo off
setlocal enabledelayedexpansion
cls

:: Main Directory Containing PDF Directories (change this to suit your needs)
set "source_dir=.\test"

:: Move to source dir and process each folder, one at a time.
pushd "%source_dir%"

for /f "delims=" %%A in ('dir /b /a:d') do (
    call :getSeriesName "%%A" series_name

    mkdir !series_name! 2>nul

    REM If you want to do additional cleanup, change the copy to a move
    copy "%%A\*.pdf" !series_name! >nul
)

popd
exit /b

::------------------------------------------------------------------------------
:: Extracts the series name from the directory and changes spaces to periods
:: 
:: Arguments: %1 - The original book release name
::            %2 - The variable that will contain the returned value because
::                 batch doesn't actually have functions
:: Returns:   The series name and volume number
::------------------------------------------------------------------------------
:getSeriesName
:: Convert spaces to periods
set "raw_name=%~1"
set standard_name=!raw_name: =.!

:: Split the folder name into period-delimited tokens
set token_counter=0
:name_split
for /f "tokens=1,* delims=.-" %%B in ("!standard_name!") do (
    set name_part[!token_counter!]=%%B
    set standard_name=%%C
    set /a token_counter+=1
    goto :name_split
)

:: Get the volume number
for /l %%B in (0,1,!token_counter!) do (
    echo !name_part[%%B]!|findstr /R /C:"[sS][0-9][0-9]*[eE][0-9][0-9]*" >nul
    if !errorlevel! equ 0 (
        set /a name_end=%%B-1
        set volume_value=!name_part[%%B]!
        set volume_value=!volume_value:~0,3!
    )
)

:: Rebuild the series name
set "extracted_name="
for /l %%B in (0,1,!name_end!) do set "extracted_name=!extracted_name!!name_part[%%B]!."
set extracted_name=!extracted_name!!volume_value!

:: Purge the name_part array
for /l %%B in (0,1,!token_counter!) do set "name_part[%%B]="

:: Return the extracted name
set "%~2=!extracted_name!"

答案 1 :(得分:1)

你的问题令人困惑。您还没有描述文件名的格式,但只是展示了一些示例,使用示例而不是规范可能会被误解。为其他问题写的帖子代码对于这个问题不起作用当然没用。您没有使用真实文件名显示输入和所需输出的示例,因此基于示例数据的解决方案可能无法用于实际数据。

编辑添加了新规范。规范和程序代码都已根据评论中给出的请求进行了相应修改。

根据我的理解,下面是这个问题的规格:

  

“给定一系列具有此格式的* .pdf文件:

any string hereS##Eany string here.pdf
              / | ^-- "E" letter
    "S" letter  digit
     

提取在“S-digit”分隔符后面的“E”之前结束的字符串,并将文件移动到具有这样名称的文件夹,“S”和“E”字母不区分大小写。忽略没有以前格式的文件。“

此代码基于此类规范解决问题

@echo off
setlocal EnableDelayedExpansion

rem Change current directory to the one where this .bat file is located
cd "%~P0"

set "digits=0123456789"

rem Process all *.pdf files
for %%f in (*.pdf) do (

   rem Get the folder name of this file
   call :getFolder "%%f"

   rem If this file have a properly formatted name: "headS##Etail"
   if defined folder (
      rem Move the file to such folder
      if not exist "!folder!" md "!folder!"
      move "%%f" "!folder!"
   )

)
goto :EOF


:getFolder file

set "folder="
set "file=%~1"
set "head="
set "tail=%file%"

:next
   for /F "delims=%digits%" %%a in ("%tail%") do set "head=%head%%%a"
   set "tail=!file:*%head%=!"
   if not defined tail exit /B
   if /I "%head:~-1%" equ "S" goto found
   :digit
      if "!digits:%tail:~0,1%=!" equ "%digits%" goto noDigit
      set "head=%head%%tail:~0,1%"
      set "tail=%tail:~1%"
   goto digit
   :noDigit
goto next

:found
for /F "delims=Ee" %%a in ("%tail%") do set "folder=%head%%%a"
exit /B

要使用此批处理文件,请将其放在原始文件所在的同一文件夹中,并在不带参数的情况下执行该文件;您也可以通过在资源管理器中双击来执行它。示例会话:

C:\Users\Antonio\Documents\test> dir /B
test.bat
The_Good_Wife_S06e15.pdf
The_Good_Wife_S06e22.pdf
TOCCO_ANGELO_4.pdf
True Blood S07e07_001.pdf
True Detective S02E03-04 Repack.pdf
True Detective S02e03.pdf
True Detective S02e03_001.pdf
True.Detective.S02e02.1080p.WEBMux.pdf
Tudors S04e08.pdf
Tutti pazzi per amore s3e15-16.pdf
Tutto Pu‗ Succedere S01e01-02.pdf
Twin Peaks s1e1-8.pdf
Twin Peaks s2e16-22.pdf
Tyrant S02e07.pdf
Tyrant.S01e01_02.720p.DLMux.pdf
Ultimo 2 - La Sfida.pdf
Ultimo 3 -L Infiltrato.pdf
Una Mamma Imperfetta S02e01-13.pdf
Under the Dome S02e02 Versione 720p.pdf
Under.the.Dome.S03E07.HDTV.x264.SUB.ITA.pdf

C:\Users\Antonio\Documents\test> test.bat

C:\Users\Antonio\Documents\test> tree /F
Listado de rutas de carpetas
El número de serie del volumen es 00000088 0895:160E
C:.
│   test.bat
│   TOCCO_ANGELO_4.pdf
│   Ultimo 2 - La Sfida.pdf
│   Ultimo 3 -L Infiltrato.pdf
│
├───The_Good_Wife_S06
│       The_Good_Wife_S06e15.pdf
│       The_Good_Wife_S06e22.pdf
│
├───True Blood S07
│       True Blood S07e07_001.pdf
│
├───True Detective S02
│       True Detective S02E03-04 Repack.pdf
│       True Detective S02e03.pdf
│       True Detective S02e03_001.pdf
│
├───True.Detective.S02
│       True.Detective.S02e02.1080p.WEBMux.pdf
│
├───Tudors S04
│       Tudors S04e08.pdf
│
├───Tutti pazzi per amore s3
│       Tutti pazzi per amore s3e15-16.pdf
│
├───Tutto Pu‗ Succedere S01
│       Tutto Pu‗ Succedere S01e01-02.pdf
│
├───Twin Peaks s1
│       Twin Peaks s1e1-8.pdf
│
├───Twin Peaks s2
│       Twin Peaks s2e16-22.pdf
│
├───Tyrant S02
│       Tyrant S02e07.pdf
│
├───Tyrant.S01
│       Tyrant.S01e01_02.720p.DLMux.pdf
│
├───Una Mamma Imperfetta S02
│       Una Mamma Imperfetta S02e01-13.pdf
│
├───Under the Dome S02
│       Under the Dome S02e02 Versione 720p.pdf
│
└───Under.the.Dome.S03
        Under.the.Dome.S03E07.HDTV.x264.SUB.ITA.pdf

如果“S”分隔符之前的文件名不能包含数字,则此代码会更简单。此解决方案假定文件名中没有感叹号!