基于批处理的文件/文件夹创建的问题
首先,我要感谢任何可以提前提供帮助的人,我已经很多年没有做过任何这样的批处理脚本了,“生锈”是一种轻描淡写的说法。我在映射驱动器上有很多文件,我想对其进行一些处理,而不冒损坏这些文件的风险,因此我尝试创建“假”文件的本地版本,这样如果出现问题也不会造成任何伤害。
这一切都是在 Windows 7 计算机上完成的,因此我首先运行以下命令来获取要操作的整个目录列表:
dir /b M:\ > movies.txt
从那里,如果我运行以下命令,我可以根据以下内容获取单独的 0 字节假文件'movies.txt' 内的行:
for /F "eol=; tokens=1* delims=," %%i in (movies.txt) do @echo.> %%i.avi
或者,如果我运行以下命令,我将得到基于 'movies.txt' 的空目录:
for /F "delims=~" %%f in (movies.txt) DO MD "%%f"
这些命令中的任何一个的问题是,我要么得到一堆文件,要么全部集中到根目录中文件夹(第一个“for”命令)或我得到空目录(第二个“for”命令),但我似乎无法弄清楚如何将假文件放入文件夹中。
示例:如果映射驱动器“M”有这些电影文件:
M:\Las Vegas Trip (2009)\Las Vegas Trip (2009).avi
M:\Las Vegas Trip (2010)\Las Vegas Trip (2010).avi
那么“movies.txt”将显示为:
Las Vegas Trip (2009)
Las Vegas Trip (2010)
第一个命令将导致:
D:\Las Vegas Trip (2009).avi
D:\Las Vegas Trip (2010).avi
或第二个命令将导致:
D:\Las Vegas Trip (2009)\
D:\Las Vegas Trip (2010)\
但我需要的是:
D:\Las Vegas Trip (2009)\Las Vegas Trip (2009).avi
D:\Las Vegas Trip (2010)\Las Vegas Trip (2010).avi
我一直在搜索在互联网上搜索了好几天,似乎找不到任何能给我想要的结果的东西...我确实在其他地方发现了一个稍微类似的“问题”,他们想根据文件名创建文件夹,但他们想自动-重命名最终结果文件名,这不起作用。 (是的,我尝试修改他们的命令以使其工作,但我能想到的任何更改都不会产生除了错误之外的任何内容。) 您可以在此处(现场)查看上述方法。
我尝试对链接中的代码进行修改,包括:
for /f %%f in ('dir *.png /b') do md %%~nf & move %f .\%%~nf\0000.png <- original
for /f %%f in ('dir *.avi /b') do md %%~nf & move %f .\%%~nf\*.avi
for /f %%f in ('dir *.avi /b') do md %%~nf & move %f .\%%~nf\
for /f %%f in ('dir *.avi /b') do md %%~nf & move %f .\%%~nf
在每种情况下,我都会在引用代码“%%~nf*.avi”部分的最后部分时遇到错误。
任何有关如何修改该代码以适合我或其他可行的想法的想法将不胜感激。
--------编辑部分-------- 我尝试更改我的批处理文件以使用下面帖子中提供的命令,但它似乎不起作用:
cd Movies2
dir /b %MovieSource% > movies.txt
for /f "delims=" %%f in ('dir *.avi /b') do md "%%~nf" & move "%~f" ".\%%~nf\*.avi"
rem for /F "eol=; tokens=1* delims=," %%i in (movies.txt) do @echo.> %%i.avi
rem FOR /F "delims=~" %%f in (movies.txt) DO MD "%%f"
我还尝试将第一个“for”命令放在第二个并取消另一个“for”命令,以便它创建*.avi 是预先的,认为这可能有效,但尝试任何一种方式都会产生相同的错误。
The following usage of the path operator in batch-parameter
substitution is invalid: %~f" ".\%%~nf\*.avi"
First, I'd like to thank anyone who can help in advance, I haven't done any batch scripts like this in years and being 'rusty' is an understatement. I have a lot of files on a mapped drive that I want to do some work with without risking damage to those files so I'm trying to create a local version of 'fake' files so no harm if something gets screwed up.
This is all being done on a Windows 7 machine, so I started with running the following to get the entire directory listing to manipulate:
dir /b M:\ > movies.txt
From there, if I run the following, I'm able to get individual 0 byte fake files based on the lines inside 'movies.txt':
for /F "eol=; tokens=1* delims=," %%i in (movies.txt) do @echo.> %%i.avi
Or if I run the following, I'll get empty directories based on 'movies.txt':
for /F "delims=~" %%f in (movies.txt) DO MD "%%f"
The problem with either of these commands is that I either get a pile of files all lumped into the root folder (first 'for' command) or I get empty directories (second 'for' command) but I can't seem to figure out how to get the fake files into the folders.
Examples: If Mapped Drive "M" has these movie files:
M:\Las Vegas Trip (2009)\Las Vegas Trip (2009).avi
M:\Las Vegas Trip (2010)\Las Vegas Trip (2010).avi
then 'movies.txt' will appear as:
Las Vegas Trip (2009)
Las Vegas Trip (2010)
and the first command will result in:
D:\Las Vegas Trip (2009).avi
D:\Las Vegas Trip (2010).avi
or the second command will result in:
D:\Las Vegas Trip (2009)\
D:\Las Vegas Trip (2010)\
But what I need is:
D:\Las Vegas Trip (2009)\Las Vegas Trip (2009).avi
D:\Las Vegas Trip (2010)\Las Vegas Trip (2010).avi
I've been searching the internet for days and can't seem to find anything that will give me the desired results... I did find a slightly similar 'issue' elsewhere where they wanted to create the folders based on file name, but they wanted to auto-rename the end result filename which wont work. (Yes I tried to modify their command to work, but no changes I could come up with would produce anything but errors.)
You can see the above mentioned method here (on site).
Modifications I attempted to the code from the link included:
for /f %%f in ('dir *.png /b') do md %%~nf & move %f .\%%~nf\0000.png <- original
for /f %%f in ('dir *.avi /b') do md %%~nf & move %f .\%%~nf\*.avi
for /f %%f in ('dir *.avi /b') do md %%~nf & move %f .\%%~nf\
for /f %%f in ('dir *.avi /b') do md %%~nf & move %f .\%%~nf
In each case I was given an error in reference to the final part of the code "%%~nf*.avi" section.
Any ideas of how to either modify that code to work for me or something else that would work would be greatly appreciated.
-------Edited Part-------
I tried changing my batch file to use the command provided in the post below and it didn't seem to work:
cd Movies2
dir /b %MovieSource% > movies.txt
for /f "delims=" %%f in ('dir *.avi /b') do md "%%~nf" & move "%~f" ".\%%~nf\*.avi"
rem for /F "eol=; tokens=1* delims=," %%i in (movies.txt) do @echo.> %%i.avi
rem FOR /F "delims=~" %%f in (movies.txt) DO MD "%%f"
I also tried putting the first 'for' command second and unREM'd the other 'for' command so that it would create the *.avi's beforehand, thinking that might work, but trying it either way gives the same error.
The following usage of the path operator in batch-parameter
substitution is invalid: %~f" ".\%%~nf\*.avi"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
此脚本按
oldroot
中指定的路径迭代所有文件。每个完全限定文件名中的旧根路径都将替换为新根路径(
newroot
变量),从而生成另一个完全限定文件名(存储在newfile 中)
变量)。检查新文件的路径:如果不存在,则创建。
最后,生成一个具有新名称的空文件。
EDIT
ECHO.>"%newfile%"
行实际上生成一个包含 EOL 字符 (0x0D0A
) 的文件,因此大小这样一个文件的值是 2,而不是 0。如果您希望它是 0,您可以用以下结构替换ECHO
位:Try this:
This script iterates through all the files by the path specified in
oldroot
.The old root path in each fully qualified file name is replaced with the path to the new root (the
newroot
variable), resulting in another fully qualified file name (stored in thenewfile
variable).The new file's path is checked: if it does not exist, it is created.
Finally, an empty file with the new name is generated.
EDIT
The
ECHO.>"%newfile%"
line actually produces a file with an EOL character (0x0D0A
) in it, so the size of such a file is 2, not 0. If you wish it to be 0, you can replace theECHO
bit with this construct:这似乎是
FOR /F
语句中的问题,因为它使用标准 delim
和
。它应该与类似的东西一起工作
It's seems to be the problem in the
FOR /F
statement, as it uses as standard delim<space>
and<TAB>
.It should work with something like