我的目录包含很多文件作为示例:
杰基尔博士和海德先生的奇怪案例 - 罗伯特路易斯史蒂文森 审判 - Franz Kafka.doc
悲惨世界 - Victor Hugo.prc
恋爱中的女人 - D. H. Lawrence.mobi
被绑架 - 罗伯特路易斯史蒂文森.prc 房间 - Emma Donoghue.pdf
请帮我写一个CMD命令来重命名所有文件和输出:
杰基尔博士和海德先生的奇怪案例 Trial.doc
Les Miserables.prc
恋爱中的女人。 H. Lawrence.mobi
Kidnapped.prc
Room.pdf
然后单独归档文件:
杰基尔博士和海德先生的奇怪案例 Trial.rar
Les Miserables.rar
恋爱中的女性.rar Kidnapped.rar
Room.rar
P / s:我已经编写了一个归档文件的命令,但它没有用。哪里错了?
for / f“delims =”%% b in('dir“。”/ ad / b')do(
“c:\ Program Files \ 7-Zip \ 7z.exe”a -r -tzip“%% b.zip”
)
答案 0 :(得分:0)
@ECHO OFF
SETLOCAL enabledelayedexpansion
SET "sourcedir=U:\books"
SET "destdir=U:\destdir"
CALL :makedummydata
FOR /f "delims=" %%a IN ('dir /b /a-d "%sourcedir%\*" ') DO (
FOR /f "delims=-" %%b IN ("%%a") DO (
SET "book=%%b"
ECHO export? ren? "%sourcedir%\%%a" "!book:~0,-1!%%~xa"
ECHO "c:\Program Files\7-Zip\7z.exe" a -r -tzip "%destdir%\!book:~0,-1!.zip" "%sourcedir%\!book:~0,-1!%%~xa"
ECHO "c:\Program Files\7-Zip\7z.exe" a -r -tzip "%destdir%\!book:~0,-1!.zip" "%sourcedir%\%%a"
)
)
GOTO :EOF
:makedummydata
::
:: This part is just to set up the files as specified on my test directory u:\books
::
MD "%sourcedir%" 2>nul
FOR %%b IN (
"The Strange Case of Dr Jekyll and Mr Hyde - Robert Louis Stevenson.doc"
"The Trial - Franz Kafka.doc"
"Les Miserables - Victor Hugo.prc"
"Women In Love - D. H. Lawrence.mobi"
"Kidnapped - Robert Louis Stevenson.prc"
"Pride & Prejudice - Jane Austen.prc"
"Room - Emma Donoghue.pdf"
) DO (
COPY /y NUL "%sourcedir%\%%~b" >nul
)
GOTO :EOF
不确定export
的含义。重命名,也许?
我已经展示了如何执行7z
命令,但我不知道您是想先重命名文件还是只需一步完成。我假设每本书都标题为name - author.ext
,-
的每一边都有一个空格。
命令只是echo
,因此您可以在执行前进行验证。
请注意,如果标题中包含某些字符,这将无法正常工作 - 尤其是"
,尽管&
似乎很高兴 - 这就是我添加内容的原因。< / p>
另一件需要担心的事情是使用-
作为分隔符。包含-
的标题会发生什么情况 - 或者作者姓名是否包含-
- 或两者兼而有之?
哦不!恐惧&#34;它没有工作&#34;回答没有详细说明程序做了什么或没做什么。
这是一个包含所提供额外细节的修订批次。 **小心它在虚拟目录中创建一组虚拟文件,然后重命名并归档它们。 **
@ECHO OFF
SETLOCAL enabledelayedexpansion
SET "sourcedir=U:\books"
SET "destdir=U:\destdir"
CALL :makedummydata
ECHO before...
DIR %sourcedir%
DIR %destdir%
FOR /f "delims=" %%a IN ('dir /b /a-d "%sourcedir%\*" ') DO (
FOR /f "delims=-" %%b IN ("%%a") DO (
SET "book=%%b"
ren "%sourcedir%\%%a" "!book:~0,-1!%%~xa"
"c:\Program Files\7-Zip\7z.exe" a -r -tzip "%destdir%\!book:~0,-1!.zip" "%sourcedir%\!book:~0,-1!%%~xa"
)
)
ECHO after...
DIR %sourcedir%
DIR %destdir%
GOTO :EOF
:makedummydata
::
:: This part is just to set up the files as specified on my test directory u:\books
::
MD "%sourcedir%" 2>nul
MD "%destdir%" 2>nul
REM DEL "%sourcedir%\*?*" 2>nul
REM del "%destdir%\*?*" 2>nul
FOR %%b IN (
"The Strange Case of Dr Jekyll and Mr Hyde - Robert Louis Stevenson.doc"
"The Trial - Franz Kafka.doc"
"Les Miserables - Victor Hugo.prc"
"Women In Love - D. H. Lawrence.mobi"
"Kidnapped - Robert Louis Stevenson.prc"
"Pride & Prejudice - Jane Austen.prc"
"Room - Emma Donoghue.pdf"
) DO (
COPY /y NUL "%sourcedir%\%%~b" >nul
)
GOTO :EOF
这是我运行的结果:
before...
Volume in drive U has no label.
Volume Serial Number is 02E6-1000
Directory of U:\books
28/03/2014 11:40 <DIR> .
28/03/2014 11:40 <DIR> ..
28/03/2014 11:46 0 The Strange Case of Dr Jekyll and Mr Hyde - Robert Louis Stevenson.doc
28/03/2014 11:46 0 The Trial - Franz Kafka.doc
28/03/2014 11:46 0 Les Miserables - Victor Hugo.prc
28/03/2014 11:46 0 Women In Love - D. H. Lawrence.mobi
28/03/2014 11:46 0 Kidnapped - Robert Louis Stevenson.prc
28/03/2014 11:46 0 Pride & Prejudice - Jane Austen.prc
28/03/2014 11:46 0 Room - Emma Donoghue.pdf
7 File(s) 0 bytes
2 Dir(s) 2,145,681,408 bytes free
Volume in drive U has no label.
Volume Serial Number is 02E6-1000
Directory of U:\destdir
28/03/2014 11:40 <DIR> .
28/03/2014 11:40 <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 2,145,681,408 bytes free
7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Scanning
Creating archive U:\destdir\The Strange Case of Dr Jekyll and Mr Hyde.zip
Compressing The Strange Case of Dr Jekyll and Mr Hyde.doc
Everything is Ok
7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Scanning
Creating archive U:\destdir\The Trial.zip
Compressing The Trial.doc
Everything is Ok
7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Scanning
Creating archive U:\destdir\Les Miserables.zip
Compressing Les Miserables.prc
Everything is Ok
7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Scanning
Creating archive U:\destdir\Women In Love.zip
Compressing Women In Love.mobi
Everything is Ok
7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Scanning
Creating archive U:\destdir\Kidnapped.zip
Compressing Kidnapped.prc
Everything is Ok
7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Scanning
Creating archive U:\destdir\Pride & Prejudice.zip
Compressing Pride & Prejudice.prc
Everything is Ok
7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Scanning
Creating archive U:\destdir\Room.zip
Compressing Room.pdf
Everything is Ok
after...
Volume in drive U has no label.
Volume Serial Number is 02E6-1000
Directory of U:\books
28/03/2014 11:40 <DIR> .
28/03/2014 11:40 <DIR> ..
28/03/2014 11:46 0 The Strange Case of Dr Jekyll and Mr Hyde.doc
28/03/2014 11:46 0 The Trial.doc
28/03/2014 11:46 0 Les Miserables.prc
28/03/2014 11:46 0 Women In Love.mobi
28/03/2014 11:46 0 Kidnapped.prc
28/03/2014 11:46 0 Pride & Prejudice.prc
28/03/2014 11:46 0 Room.pdf
7 File(s) 0 bytes
2 Dir(s) 2,145,452,032 bytes free
Volume in drive U has no label.
Volume Serial Number is 02E6-1000
Directory of U:\destdir
28/03/2014 11:40 <DIR> .
28/03/2014 11:40 <DIR> ..
28/03/2014 11:46 224 The Strange Case of Dr Jekyll and Mr Hyde.zip
28/03/2014 11:46 160 The Trial.zip
28/03/2014 11:46 170 Les Miserables.zip
28/03/2014 11:46 170 Women In Love.zip
28/03/2014 11:46 160 Kidnapped.zip
28/03/2014 11:46 176 Pride & Prejudice.zip
28/03/2014 11:46 150 Room.zip
7 File(s) 1,210 bytes
2 Dir(s) 2,145,452,032 bytes free
请注意,现在已激活所需的REN和7Z命令
还要注意有两个DEL
命令REMmed-out 这些将清除两个虚拟目录,但我对发布破坏任何内容的代码感到不舒服 - 以防万一一个错字。 如果您已检查REM
命令是否正确,则只删除DEL
关键字
答案 1 :(得分:0)
这是一个使用正则表达式来解析文件名的解决方案,只有在名称的标题部分中有" - "
时它才会失败,并且会在那里删除名称。外来字符和Unicode也是一个问题。
脚本位于最后6行 - 它上面的行只是创建一些虚拟文件来测试它 - 所以在空文件夹中启动它来测试它。
del
命令会在归档后删除原始文件。
这使用名为repl.bat
的帮助程序批处理文件 - 从https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
将repl.bat
放在与批处理文件相同的文件夹中或放在路径上的文件夹中。
@echo off
for %%a in (
"The Strange Case of Dr Jekyll and Mr Hyde - Robert Louis Stevenson.doc"
"The Trial - Franz Kafka.doc"
"Les Miserables - Victor Hugo.prc"
"Women In Love - D. H. Lawrence.mobi"
"Kidnapped - Robert Louis Stevenson.prc"
"Room - Emma Donoghue.pdf"
) do type nul>"%%a"
:: remove the above after testing
for /f "tokens=1,* delims=|" %%a in ('dir /b /a-d ^|find /v /i "%~nx0" ^|find /v /i "repl.bat" ^|repl "(.*?) - .*(\..*)$" "$&|$1$2" a') do (
ren "%%a" "%%b"
"C:\Program Files\7-Zip\7z.exe" a "%%~nb" "%%b"
del "%%b"
)
pause