将文件移动到具有部分名称的文件夹 [英] Move files to folders with partial names

查看:83
本文介绍了将文件移动到具有部分名称的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大约有250个文件需要移至特定文件夹.问题在于文件夹仅具有文件的部分名称.

I have about 250 files that I need to move to a specific folder. The problem is that folder only have the partial name of the files.

例如,我需要将文件"12345.txt"移动到文件夹"12345-hello",因为每个文件夹均以实际文件名开头.

For example, I need to move file "12345.txt" to folder "12345 - hello" as each folder starts by the actual file name.

我可以在DOS中的批处理文件中执行此操作吗?

Can I do this in a batch file in DOS?

谢谢.

推荐答案

假设使用Windows,实际上并不难:

Assuming Windows, it's actually not hard:

@echo off
rem loop over all files
for %%f in (*) do call :process "%%f"

rem this is necessary to avoid running the subroutine below
rem after the loop above ended
goto :eof

rem subroutine that gets called for every file
rem this finds the first matching folder and moves the file there
:process
rem the /d loops over all directories - the mask ensures that
rem the directory name starts with the given file name (without
rem extension)
for /d %%d in ("%~n1*") do (
    echo Moving "%~1" to "%%d" ...
    move "%~1" "%%d"
    rem Return since the file was moved already
    goto :EOF
)

也可以在我的SVN存储库中找到.

这篇关于将文件移动到具有部分名称的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆