【发布时间】:2016-10-09 14:26:46
【问题描述】:
要将文件移动到文件夹中,我使用此脚本
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "SPLITCHAR=-" & rem // (a single character to split the file names)
set "SEARCHSTR=_" & rem // (a certain string to be replaced by another)
set "REPLACSTR= " & rem // (a string to replace all found search strings)
set "OVERWRITE=" & rem // (set to non-empty value to force overwriting)
rem // Get file location and pattern from command line arguments:
set "LOCATION=%~1" & rem // (directory to move the processed files into)
set "PATTERNS=%~2" & rem // (file pattern; match all files if empty)
rem /* Prepare overwrite flag (if defined, set to character forbidden
rem in file names; this affects later check for file existence): */
if defined OVERWRITE set "OVERWRITE=|"
rem // Continue only if target location is given:
if defined LOCATION (
rem // Create target location (surpress error if it already exists):
2> nul md "%LOCATION%"
rem /* Loop through all files matching the given pattern
rem in the current working directory: */
for /F "eol=| delims=" %%F in ('dir /B "%PATTERNS%"') do (
rem // Process each file in a sub-routine:
call :PROCESS "%%F" "%LOCATION%" "%SPLITCHAR%" "%SEARCHSTR%" "%REPLACSTR%"
)
)
endlocal
exit /B
:PROCESS
rem // Retrieve first argument of sub-routine:
set "FILE=%~1"
rem // Split name at (first) split character and get portion in front:
for /F "delims=%~3" %%E in ("%~1") do (
rem // Append a split character to partial name:
set "FOLDER=%%E%~3"
)
setlocal EnableDelayedExpansion
rem // Right-trim partial name:
if not "%~4"=="" set "FOLDER=!FOLDER:%~4%~3=!"
set "FOLDER=!FOLDER:%~3=!"
rem /* Check whether partial name is not empty
rem (could happen if name began with split character): */
if defined FOLDER (
rem // Replace every search string with another:
if not "%~4"=="" set "FOLDER=!FOLDER:%~4=%~5!"
rem // Create sub-directory (surpress error if it already exists):
2> nul md "%~2\!FOLDER!"
rem /* Check if target file already exists; if overwrite flag is
rem set (to an invalid character), the target cannot exist: */
if not exist "%~2\!FOLDER!\!FILE!%OVERWRITE%" (
rem // Move file finally (surpress `1 file(s) moved.` message):
1> nul move /Y "!FILE!" "%~2\!FOLDER!"
)
)
endlocal
exit /B
要使用脚本我必须
1- 打开 cmd
2-要执行批处理我必须
cd C:\Users\Administrator\Desktop\T\
"C:\Users\Administrator\Desktop\T\build-folder-hierarchy.bat" "C:\Users\Administrator\Desktop\T\" "*.pdf"
但是问题是什么?
为每个 .pdf 文件批处理创建一个相对文件夹,但我不希望它以这种方式创建文件夹。看http://i.imgur.com/TVhQyzv.png
aaaa aaaa S02 [folder]
aaaa aaaa S02e01.pdf [folder]
aaaa aaaa S02e02.pdf [folder]
aaa.aaaa.aaa.aa.aaaaa.S02 [folder]
我想要什么?
├─aaaa aaaa S02 [folder]
│ ├─aaaa aaaa S02e01.pdf[file]
│ ├─aaaa aaaa S02e02.pdf [file]
└─ ....
├─aaa.aaaa.aaa.aa.aaaaa.S02 [folder]
│ └─aaa.aaaa.aa.aa.aaaaa.S02E13.pdf [file]
:
只是一个示例名称,以了解 .pdf 文件名的格式
aaaaaaaaa aa aaaaa S01e12 720p Repack.pdf
aaa aaaaaaaaa S01e05 Versione 720p.pdf
aaa aaaaaaaa S01e05 Versione 1080p.pdf
aaa aaaa s2e06.pdf
aaa aaaa S03e12.pdf
aaa.aaaa.aaa.on.Earth.S02E13.pdf
aaa.aaaa.aaaa.S02E01.HDTV.x264.SUB.ITA.pdf
通常pdf文件名都是这样格式化的[pattern]
s01
s01e1
s1
s1e1
s1e01
s1e01-10
字符,例如 e 和 s 几乎总是出现在这些模式名称中
一般形式应该是
sxx
sxxex
sx
sxex
sxexx
sxexx-xx
X 是一个数字,字母 s 和 e 的大小写无关
Powershell 解决方案被广泛接受。
【问题讨论】:
-
请将您的实际文件夹结构作为文本放入您的问题中,而不是指向图像的链接!
-
我添加了脚本制作的实际结构
-
我知道我以前见过that script...我没有得到你真正想要的,因为你提供了多个例子(如
aaaa aaaa S02e01.pdf,s01e1[.pdf]);该脚本旨在将文件名拆分为某个字符,取之前的部分并将其用作目标文件夹名称;有没有这样的字符,例如e? -
取之前的部分并将其用作目标文件夹名称? 是。 BUT 该脚本不足,因为我有不同的文件名模式。字符,例如
e和s几乎总是出现在这些模式名称中。 -
所以基本上你有一大堆,呃......单本小说......在他们自己的目录中,你想将它们分组,呃......书籍系列......和您还想用句点替换空格以匹配,呃... 标准命名它们的方式?
标签: powershell batch-file