使用计分关键字的分类器运行正常,并且无法回传而不会失败

时间:2018-09-24 14:39:10

标签: batch-file batch-processing

此脚本通过使用充满评分关键字的数据库文件将文件快速分类到已知子目录中。它在大多数情况下都起作用,但有时会给我带来我无法弄清的错误,因为打开回声时,它的行为有所不同,但失败了。这段代码对我来说非常重要,因此这对我来说是一个巨大的问题。回声问题必须解决或无法解决,此问题无需解决其他可能的问题!请帮忙,这在很大程度上超出了我的理解。

:: sorts files from %cd% into known subdirs according to database of scored keywords. minimum score is 2.0

rem todo if echo is on it fails with certain keywords like a simple *1x0* which is super weird
rem sometimes outputs unlocated error missing operator while still doing its job correctly
rem sometimes outputs unlocated error the system cannot fing the path specified
rem keywords or subdir names cannot contain spaces
rem fails with filenames with irregular dash characters
rem Adds * to start and end of keyword. start/end wildcards should be used surgically in the keywords file instead

dir *.* /a-d >nul 2>nul || exit /b
set "tempfile=%temp%\sortables"
set "sourcedir=%~1"
setlocal enabledelayedexpansion

rem set datafile, categories according to current location
set "categories="
if /i "%cd%"=="d:\videos" (
    set "datafile=videos"
    set "categories=series porno"
)
if /i "%cd%"=="d:\videos\movies" (
    set "datafile=movies"
    set "categories=features psychedelic pornography concerts standup featurettes documentaries"
)
if /i "%cd%"=="d:\videos\movies\documentaries" (
    set "datafile=docu"
    set "categories=1-scarcity 2-globalists 3-disinformation 4-agendas 5-abundance"
)
if /i "%cd%"=="d:\videos\movies\features" (
    set "datafile=films"
    set "categories=comedy drama action thriller venture crime horror mystery fantasy science western warfare"
)
if /i "%cd%"=="d:\videos\series" (
    set "datafile=series"
    set "categories=comedy stories reality trippy"
)
if /i "%cd%"=="d:\videos\series\comedy" (
    set "datafile=comedy"
    set "categories=cartoon classic modern reality sketch standup"
)
if /i "%cd%"=="d:\videos\series\pilots" (
    set "datafile=pilots"
    set "categories=reality drama comedy scifi fantasy crime mystery action thriller"
)
if /i "%cd%"=="d:\videos\shorts" (
    set "datafile=shorts"
    set "categories=psychedelic entertaining music media useful conspiracies"
)
if /i "%cd%"=="d:\videos\shorts\media" (
    set "datafile=media"
    set "categories=trailers games fandom extras facts analysis features"
)
if /i "%cd%"=="d:\videos\shorts\music" (
    set "datafile=music"
    set "categories=bigbeat classical clubbing country electro swing reggae dub experimental geeky metal rap rock synthwave triphop xxx"
)
if not defined categories exit /b
set database=d:\system\scripts\%datafile%.txt
if not exist "%database%" echo critical error: database %datafile%.txt doesn't exist && exit /b
if defined verbal echo sorting "%cd%"

rem =============================================================================================================================
rem     setup sorting categories (do not change anything lightly or without backup after this point)
rem =============================================================================================================================

set "sortingcategories="
for %%a in (%categories%) do set "sortingcategories=!sortingcategories!,%%~a"
set "sortingcategories=%sortingcategories: =_%"

rem =============================================================================================================================
rem     create tempfile containing lines of: name|sortingcategory|weight
rem =============================================================================================================================
(
 for /f "tokens=1,2,*delims=," %%s in (%database%) do (
 set "sortingcategory=%%s"
 set "sortingcategory=!sortingcategory: =_!"
 for /f "delims=" %%a in (
  'dir /b /a-d "%sourcedir%\*%%u*" 2^>nul'
  ) do (
   echo %%a^|!sortingcategory!^|%%t^|%%s^|%%u
 )
)
)>"%tempfile%"
type "%tempfile%" >>d:\system\scripts\sorter.log

rem =============================================================================================================================
rem     reset and call processing for each file in tempfile + dummy (helps counting the last score?)
rem =============================================================================================================================

set "lastname="
echo off
for /f "tokens=1,2,3,*delims=|" %%a in ('sort "%tempfile%"') do call :resolve %%b %%c "%%a"
call :resolve dummy 0
rem declare failures
if defined verbal if not "%datafile%"=="videos" if not "%datafile%"=="music" if not "%datafile%"=="media" (
    dir "%~1\*" /a-d >nul 2>nul && for /f "delims=" %%q in ('dir %1 /b /a-d') do echo unsortable in %datafile% "%%q"
)
exit /b

:resolve
IF "%~3" equ "%lastname%" GOTO accum
rem report and reset accumulators
IF NOT DEFINED lastname GOTO RESET
SET "winner=none"
SET /a maxfound=1
FOR %%v IN (%sortingcategories%) DO (
 FOR /f "tokens=1,2delims=$=" %%w IN ('set $%%v') DO IF %%x gtr !maxfound! (SET "winner=%%v"&SET /a maxfound=%%x)
)
if "%winner%"=="none" goto reset
SET "winner=%winner:_= %"
SET "lastname=%lastname:&=and%"
rem this has a problem with different type of dash -
rem this once overwrote a same-name, much smaller file, wtf?
if "%winner%"=="porno" move "%sourcedir%\%lastname%" "d:\nvidia\" >nul && echo "d:\nvidia\%lastname%"
if not "%winner%"=="porno" move "%sourcedir%\%lastname%" "%sourcedir%\%winner%\" >nul && echo "%sourcedir%\%winner%\%lastname%"
if "%winner%"=="features" if exist "%sourcedir%\%lastname%" move "%sourcedir%\%lastname%" "%sourcedir%\%winner%\" >nul && echo "%sourcedir%\%winner%\%lastname%"
rem before or after successful filing we could do a surgical dupe check for only that file, rendering the old style obsolete

:RESET
FOR %%v IN (%sortingcategories%) DO SET /a $%%v=0
SET "lastname=%~3"
:accum
SET /a $%1+=%2

在打开回声的情况下,错误如下所示(请注意,在关闭回声后,此方法可以正常工作)

d:\VIDEOS>for /F "tokens=1,2,3,*delims=|" %a in ('sort "E:\TEMP\sortables"') do call :resolve %b %c "%a"

d:\VIDEOS>call :resolve !sortingcategory! 2 " for /F "delims=" %a in ('dir /b /a-d "\*1x0*" 2>nul') do (echo %a"
The syntax of the command is incorrect.

d:\VIDEOS>IF " for /F "delims" equ "" GOTO accum

d:\VIDEOS>

这是使用的数据库文件(对不起,行话)

series,2,1x0
series,2,1x1
series,2,2x0
series,1,s0
series,1,s1
series,1,s2
series,1,s3
series,1,s4
series,1,s5
series,1,s6
series,1,s7
series,1,s8
series,1,s9
series,-1,s00
series,-1,e00
series,1,e0
series,1,e1
series,1,e2
series,1,e3
series,1,e4
series,1,e5
series,1,e6
series,1,e7
series,1,e8
series,1,e9
series,-1,extras
series,1,judge?judy?s
series,1,pilot
porno,1,amateur
porno,1,wet
porno,1,wmv
porno,1,xxx
missing,0,not appearing in this directory

它从d:\videos中获取文件,并将它们分类为d:\videos\seriesd:\nvidia(其他子目录d:\ videos \ porno的隐藏替代品)

1 个答案:

答案 0 :(得分:1)

rem =============================================================================================================================
rem     create tempfile containing lines of: name|sortingcategory|weight
rem =============================================================================================================================
(
 for /f "tokens=1,2,*delims=," %%s in (%database%) do (
 set "sortingcategory=%%s"
 set "sortingcategory=!sortingcategory: =_!"
 for /f "delims=" %%a in (
  'dir /b /a-d "%sourcedir%\*%%u*" 2^>nul'
  ) do (
   echo %%a^|!sortingcategory!^|%%t^|%%s^|%%u
 )
)
)>"%tempfile%"

更改为

rem =============================================================================================================================
rem     create tempfile containing lines of: name|sortingcategory|weight
rem =============================================================================================================================
(
 for /f "usebackq tokens=1,2,* delims=," %%s in ("%database%") do (
  set "sortingcategory=%%s"
  set "sortingcategory=!sortingcategory: =_!"
  for /f "delims=" %%a in (
   'dir /b /a-d "%sourcedir%\*%%u*" 2^>nul'
  ) do (
   >&3 echo %%a^|!sortingcategory!^|%%t^|%%s^|%%u
  )
 )
) 3>"%tempfile%"

先前的echo off设置允许回显文本 从流1重定向到文件。

设置echo on时,流1包含以下命令 以及文字的回声。

为避免命令和文本的合并输出, 使用辅助流。 CMD支持辅助 流3到9。

更改的代码将回显的文本设置为重定向 到流3(>&3)的句柄。 重定向到文件使用流3(3>)。

发布的错误是由以下原因引起的延迟影响 回显的命令和写入%tempfile%的文本 在发布的代码上使用echo on时。

添加了for循环选项usebackq,因此%database% 路径可以用双引号引起来。