将文件整理到文件夹中,然后根据文件名将文件组织

发布于 2025-01-30 04:02:10 字数 1551 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉态萌生 2025-02-06 04:02:10
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION 

rem The following setting for the source directoryis a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
PUSHD "%sourcedir%"

FOR /L %%s IN (0,1,9) DO FOR /L %%t IN (0,1,9) DO IF EXIST "* S%%s%%tE*" (
 FOR /L %%e IN (0,1,9) DO FOR /L %%f IN (0,1,9) DO IF EXIST "* S%%s%%tE%%e%%f -*" (
  FOR /f "delims=" %%b IN ('dir /b /a-d "* S%%s%%tE%%e%%f -*"') DO (
   SET "fullname=%%b"
   FOR /f "tokens=1,2delims=:" %%q IN ("!fullname: S%%s%%tE%%e%%f =:!") DO (
    IF %%s==0 (
rem     MD "%%q\%%t" 2>nul
rem     MOVE "%%b" "%%q\%%t\" >nul
     ECHO MD "%%q\%%t"
     ECHO MOVE "%%b" "%%q\%%t\"
    ) ELSE (
rem     MD "%%q\%%s%%t" 2>nul
rem     MOVE "%%b" "%%q\%%s%%t\" >nul
     ECHO MD "%%q\%%s%%t"
     ECHO MOVE "%%b" "%%q\%%s%%t\"
    )

   )
  )
 )
)

dir/s

popd

GOTO :EOF

该批次旨在报告它打算做什么,应在将其应用于实时数据之前对测试目录进行测试。

当您对结果感到满意时,请从两个md移动行中删除rem评论关键字以激活。如果需要,可以删除echo行。

最后,dir/s简单地显示结果。

这里的键是字符串s ?? e ??

通过使用%% S%% T每个设置为0 .. 9依次可以看到是否存在名为* S00E*的文件。* S99E*存在。如果存在这样的文件,请使用%% e%% f重复配方,以找到* s00e00-* .. ** S99E99-*

然后使用/b for Note-code>/AD 捕获文件名的列表,而无目录名称>与特定的s ?? e ??匹配,并匹配。字符串计算,并分配给%% B

我们需要分析%% b,因此需要delayedexpansion并将其转移到常规变量fullname进行此操作。

由于文件名不包含,只需替换s ??检测到的::并使用for/f </code> 令牌 delims s ?? e ??中打破名称的选项。 - &gt; : %% r已在此之前设置为%% q%% r在此过程中不使用。

然后,这是在%% Q中建立子目录的问题,以及%% S%% s %% T中的子标准。

2&gt; nul如果重新创建了目录,则抑制目录消息,而&gt; nul上移动命令抑制1文件移动消息。

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION 

rem The following setting for the source directoryis a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
PUSHD "%sourcedir%"

FOR /L %%s IN (0,1,9) DO FOR /L %%t IN (0,1,9) DO IF EXIST "* S%%s%%tE*" (
 FOR /L %%e IN (0,1,9) DO FOR /L %%f IN (0,1,9) DO IF EXIST "* S%%s%%tE%%e%%f -*" (
  FOR /f "delims=" %%b IN ('dir /b /a-d "* S%%s%%tE%%e%%f -*"') DO (
   SET "fullname=%%b"
   FOR /f "tokens=1,2delims=:" %%q IN ("!fullname: S%%s%%tE%%e%%f =:!") DO (
    IF %%s==0 (
rem     MD "%%q\%%t" 2>nul
rem     MOVE "%%b" "%%q\%%t\" >nul
     ECHO MD "%%q\%%t"
     ECHO MOVE "%%b" "%%q\%%t\"
    ) ELSE (
rem     MD "%%q\%%s%%t" 2>nul
rem     MOVE "%%b" "%%q\%%s%%t\" >nul
     ECHO MD "%%q\%%s%%t"
     ECHO MOVE "%%b" "%%q\%%s%%t\"
    )

   )
  )
 )
)

dir/s

popd

GOTO :EOF

This batch is designed to REPORT what it intends to do, and should be tested against a test directory before applying it to your live data.

When you are satisfied with the results, remove the rem comment keyword from the two md and move lines to activate. The echo lines may be removed if desired.

The dir/s at the end simply shows the result.

The key here is the string S??E??.

by using %%s and %%t each set to 0..9 in turn, we can see whether a file named * S00E*..* S99E* exists. If such a file exists, repeat the recipe using %%e and %%f to find * S00E00 -*..* S99E99 -*.

Then grab a list of the filenames using /b for names-only and /a-d for no directorynames, matching the specific S??E?? string calculated, and assign to %%b.

We need to analyse %%b so need delayedexpansion and transfer to a regular variable fullname to do so.

Since the filename does not contain :, simply replace the S??E?? detected by : and use for/f tokens and delims options to break the name at the S??E?? -> : %%r is set to the part after and %%q to that before. %%r is not used in this process.

Then it's a matter of establishing the subdirectory in %%q and the sub-subdirectory in %%s or %%s%%t.

The 2>nul suppresses the directory exists messages created if a directory is re-created, and the >nul on the move command suppresses the 1 file(s) moved message.

梦途 2025-02-06 04:02:10

痛苦地提出了Ver1.0。

目前还没有傻瓜。虽然很慢。需要从那里的专家进行一些调整,也许是更好的方法,甚至大修。

非常欢迎您的建议。

@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR %%a in (*) DO (
    SET FileName=%%~na
    CALL:findString FileName ") S" pos
    SET /A pos=pos+1
    SET /A pos2=pos+2
    CALL SET "NewFolderName=%%FileName:~0,!pos!%%"
    CALL SET "NewSubFolderNumber=%%FileName:~!pos2!,2%%"
    SET /A NewSubFolderNumber=NewSubFolderNumber
    SET NewSubFolder="%%~pa!NewFolderName!\SubFolderName !NewSubFolderNumber!\"
    SET str="%%~fa" !NewSubFolder!
    MD !NewSubFolder!
    MOVE !str!
)

寻找,你们会找到。在网中的某个地方找到了此缺失的链接,以节省我剩余的头发锁。 (任何人都知道为什么首先没有内置的instr()功能?)

:findString -- returns position of first occurrence of a string in another string, case sensitive, maximum string length is 1023 characters
::          -- %~1: in - varible name of a string to be searched
::          -- %~1: in - string to be found
::          -- %~3: out- return variable name, will be set to position or undefined if string not found
:credit belongs to
:$source http://www.dostips.com
:Posted By: DosItHelp
:Expert
:Posts: 239
:Joined: 18 Feb 2006 19:54

SETLOCAL ENABLEDELAYEDEXPANSION
set "pos="
set "str=!%~1!"
for /L %%a in (0,1,1023) do (
   set "s=!str:~%%a!"
   if not defined pos if "%~2!s:*%~2=!"=="!s!" set "pos=%%a"
)
ENDLOCAL & IF "%~3" NEQ "" SET "%~3=%pos%"
GOTO:EOF

希望这可以帮助他人。

Painfully came up with a ver1.0.

Not fool-proof yet but does the job for now. Kind of slow though. Needs some tuning up from a kind expert out there, maybe a far better approach or even an overhaul.

Your kind suggestions are very much welcome.

@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR %%a in (*) DO (
    SET FileName=%%~na
    CALL:findString FileName ") S" pos
    SET /A pos=pos+1
    SET /A pos2=pos+2
    CALL SET "NewFolderName=%%FileName:~0,!pos!%%"
    CALL SET "NewSubFolderNumber=%%FileName:~!pos2!,2%%"
    SET /A NewSubFolderNumber=NewSubFolderNumber
    SET NewSubFolder="%%~pa!NewFolderName!\SubFolderName !NewSubFolderNumber!\"
    SET str="%%~fa" !NewSubFolder!
    MD !NewSubFolder!
    MOVE !str!
)

Seek and ye shall find. Found this missing link somewhere in the net just in time to save my remaining lock of hair. (Anyone knows why there isn't a built-in INSTR() function in the first place?)

:findString -- returns position of first occurrence of a string in another string, case sensitive, maximum string length is 1023 characters
::          -- %~1: in - varible name of a string to be searched
::          -- %~1: in - string to be found
::          -- %~3: out- return variable name, will be set to position or undefined if string not found
:credit belongs to
:$source http://www.dostips.com
:Posted By: DosItHelp
:Expert
:Posts: 239
:Joined: 18 Feb 2006 19:54

SETLOCAL ENABLEDELAYEDEXPANSION
set "pos="
set "str=!%~1!"
for /L %%a in (0,1,1023) do (
   set "s=!str:~%%a!"
   if not defined pos if "%~2!s:*%~2=!"=="!s!" set "pos=%%a"
)
ENDLOCAL & IF "%~3" NEQ "" SET "%~3=%pos%"
GOTO:EOF

Hope this can help others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文