使用评分关键字的排序器正在起作用,并且无法在不失败的情况下回显



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

:: 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:videosmovies" (
set "datafile=movies"
set "categories=features psychedelic pornography concerts standup featurettes documentaries"
)
if /i "%cd%"=="d:videosmoviesdocumentaries" (
set "datafile=docu"
set "categories=1-scarcity 2-globalists 3-disinformation 4-agendas 5-abundance"
)
if /i "%cd%"=="d:videosmoviesfeatures" (
set "datafile=films"
set "categories=comedy drama action thriller venture crime horror mystery fantasy science western warfare"
)
if /i "%cd%"=="d:videosseries" (
set "datafile=series"
set "categories=comedy stories reality trippy"
)
if /i "%cd%"=="d:videosseriescomedy" (
set "datafile=comedy"
set "categories=cartoon classic modern reality sketch standup"
)
if /i "%cd%"=="d:videosseriespilots" (
set "datafile=pilots"
set "categories=reality drama comedy scifi fantasy crime mystery action thriller"
)
if /i "%cd%"=="d:videosshorts" (
set "datafile=shorts"
set "categories=psychedelic entertaining music media useful conspiracies"
)
if /i "%cd%"=="d:videosshortsmedia" (
set "datafile=media"
set "categories=trailers games fandom extras facts analysis features"
)
if /i "%cd%"=="d:videosshortsmusic" (
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:systemscripts%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:systemscriptssorter.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

打开 Echo 后,错误如下所示(请注意,当 Echo 关闭时,这工作正常(

d:VIDEOS>for /F "tokens=1,2,3,*delims=|" %a in ('sort "E:TEMPsortables"') 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:videosseriesd:nvidia(隐藏替代其他子目录 d:\videos\porno(

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%路径可以双引号。

相关内容

最新更新