检测字符批次sript

时间:2017-11-22 14:28:41

标签: batch-file

我有一个脚本,它通过检测基于_分隔符的字符和下面列出的数字来重命名并将文件移动到文件夹。

  

631598_S_14CG_Layout-次数1.pdf
  631598_1_Barcode.pdf
  631598_001_CutData.ppf

输出文件夹应为631598_S_1

  

631598_S_14CGBarcode.pdf
  631598_001_CutData.ppf

输出文件夹应为631598_S_2

  

631598_S_14CG_Layout-11.pdf
  631598_11_Barcode.pdf
  631598_011_CutData.ppf

输出文件夹应为631598_S_11

  

631598_S_14CG_Layout-22.pdf
  631598_22_Barcode.pdf
  631598_22_CutData.ppf

输出文件夹应为631598_S_22

但问题是我只检测了10个字符,因此所有拥有相同第10个字符的人都移到同一个文件夹中,例如:

  

631598_S_14CG_Layout-22.pdf
  631598_22_Barcode.pdf
  631598_22_CutData.ppf

...将移至631598_S_2而不是631598_S_22

以下是脚本

@echo off
setlocal enabledelayedexpansion

set "Pattern=_Layout-"
set "Replace=_"

for %%a in (*.pdf) do (
    set "File=%%~a"
    ren "%%a" "!File:%Pattern%=%Replace%!"
)

for /f "tokens=1-4 delims=_." %%a in ('dir /b *.pdf') do ren "%%a_%%b_%%c_%%d.pdf" "%%a_%%b_%%d.pdf"

pushd ."\*"
for /f "delims=" %%F in ('dir /b /a-d *.pdf ^|findstr /v "Barcode"') do (
  2>nul md "%%~nF"

)

TIMEOUT /T 01

set "Pattern1=_00"
set "Replace1=_S_"

for %%a in (*.ppf) do (
    set "File=%%~a"
    ren "%%a" "!File:%Pattern1%=%Replace1%!"
)

set "Pattern2=_0"
set "Replace2=_S_"

for %%a in (*.ppf) do (
    set "File=%%~a"
    ren "%%a" "!File:%Pattern2%=%Replace2%!"
)

set "Pattern3=Cut Data"
set "Replace3=CutData"

for %%a in (*.ppf) do (
    set "File=%%~a"
    ren "%%a" "!File:%Pattern3%=%Replace3%!"
)

set "Pattern=_Barcode"
set "Replace=_Barcode_S"

for %%a in (*.pdf) do (
    set "File=%%~a"
    ren "%%a" "!File:%Pattern%=%Replace%!"
)

for /f "tokens=1-4 delims=_." %%a in ('dir /b *.pdf') do ren "%%a_%%b_%%c_%%d.pdf" "%%a_%%d_%%b_%%c.pdf"


for /f "tokens=*" %%1 in ('dir /a-d /b *.pdf *.ppf') do (
    set filename=%%1&set dirname=!filename:~0,10!

        for /f "tokens=*" %%A in ('dir /ad /b') do (
        set dirid=%%A&set dirid=!dirid:~0,10!

        if "!dirid!" equ "!dirname!" move %%1 %%A

  )   
)

0 个答案:

没有答案