批处理脚本根据文件名的一部分创建文件夹,然后将文件复制到特定文件夹

时间:2015-04-13 12:01:45

标签: batch-file

目前,我已启动并运行此脚本:

@echo off
setlocal enabledelayedexpansion
set FolderIncoming=D:\Recordings\Calls\All_Calls
set FileMask=201*.wav
set FolderSorted=D:\Recordings\ODrive - Mitus\CallRecordings
Set LameLocation=C:\Program Files (x86)\Lame

for %%a in ("%FolderIncoming%\%FileMask%") do (
   set FileName=%%~na
   set TargetYear=!FileName:~0,4!
   set TargetFolder=!TargetYear!\!FileName:~0,10!
            if not exist "%FolderSorted%\!TargetFolder!\%%~na.mp3" (
               echo Processing '!FileName!' ...
               if not exist "%FolderSorted%\!TargetFolder!" md "%FolderSorted%\!TargetFolder!"
               "%LameLocation%\lame.exe" -V9 --vbr-new -mm -h -q 0 "%%a" "%FolderSorted%\!TargetFolder!\%%~na.mp3"
            )
)

它工作得很好但是我如何添加功能,它将每个转换的wav文件放在基于IP扩展的特定文件夹位置。现在,我们有7个IP扩展(10200,10201,10202,10203,10204,10205,10206),我想将它保存在文件夹中:

IP Extension\Date\Converted_wav-to-mp3_Files_Here

保存的录音名称示例:

2015-04-10-08-52-15_xxxxxxxxxxxx_10201.mp3
2015-04-10-10-03-52_10202_xxxxxxxxxxx.mp3

文件应分别保存在适当的文件夹中,如下所示:

10201\2015-04-10\2015-04-10-08-52-15_xxxxxxxxxxxx_10201.mp3
10202\2015-04-10\2015-04-10-10-03-52_10202_xxxxxxxxxxx.mp3

因为扩展开始,中间或以_(下划线)结束,所以有点令人费解。

这是我到目前为止所做的,但它在创建文件夹时不包含扩展名。

@echo off
setlocal enabledelayedexpansion
set extensions=/10200/10210/10220/10230/10240/10250/10260/
set FolderIncoming=D:\Recordings\Calls\All_Calls
set FileMask=201*.wav
set FolderSorted=D:\Recordings\ODrive - Mitus\CallRecordings
Set LameLocation=C:\Program Files (x86)\Lame

for %%a in ("%FolderIncoming%\%FileMask%") do (

   set FileName=%%~na

   for %%b in (!Filename:_= !) do (
      if "!extensions:/%%b/=!" neq "%extensions%" set "extension=%%b"
   )

   set TargetYear=!FileName:~0,4!
   set TargetFolder=!extension!\!TargetYear!\!FileName:~0,10!

            if not exist "%FolderSorted%\!TargetFolder!\%%~na.mp3" (
               echo Processing '!FileName!' ...
               if not exist "%FolderSorted%\!TargetFolder!" md "%FolderSorted%\!TargetFolder!"
          REM"%LameLocation%\lame.exe" -m j --cbr -b 320 -q 0 --lowpass 22.05 "%%a" "%FolderSorted%\!TargetFolder!\%%~na.mp3"
               "%LameLocation%\lame.exe" -V9 --vbr-new -mm -h -q 0 "%%a" "%FolderSorted%\!TargetFolder!\%%~na.mp3"
            )

)

这应该有什么变化才能让它按原样运作?

谢谢。

1 个答案:

答案 0 :(得分:0)

下一个代码段可以工作:

set "FileName=%%~na"
set "NameFile=!Filename:_= !"

set "extension=abcde"
rem this will not resolve: for %%b in (!Filename:_= !)
for %%b in (!NameFile!) do (
   if "!extensions:/%%b/=!" neq "%extensions%" set "extension=%%b"
)
if "!extension!"=="abcde" echo !Filename! does not match desired extensions

但是,在你的set extensions=/10200/10210/10220/10230/10240/10250/10260/中没有出现任何建议的10201,10202,10203,10204,10205,10206 ......

因此,还有另一种可能的机制:

set "FileName=%%~na"
set "NameFile=!Filename:_= !"

set "extension=abcde"
for %%b in (!NameFile!) do (
   set "strFragment=%%b"
   rem if "!strFragment:-=!" equ "!strFragment!" ( set /A "numExtens=%%b") else ( set /A "numExtens=0")
   set /A "numExtens=!strFragment:-0=-!"
   if !numExtens! geq 10200 if !numExtens! leq 10299 set "extension=%%b"
)
if "!extension!"=="abcde" echo !Filename! out of desired range

注意set /A "numExtens=!strFragment:-0=-!"中的替换。在datetime中处理可能的无效八进制值,例如2015-04-09-08-52-15 set /A numvar=08 {或set /A numvar=09会导致出现下一条错误消息:Invalid number. Numeric constants are either decimal (17), hexadecimal (0x11), or octal (021).numvar保持不变。

flash :也许有更好的方法:

set /A "numExtens=0"
set /A "numExtens=!strFragment!" 2>NUL

这是更普遍的,可以适用于几乎任何xxxxxxxxxxxx文件名部分...