Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 基于文件组创建文件夹_Batch File - Fatal编程技术网

Batch file 基于文件组创建文件夹

Batch file 基于文件组创建文件夹,batch-file,Batch File,我有不同名称的文件 Tim-01.jpg Tim-02.jpg Tim-03.jpg jack-01.jpg jack-02.jpg jack-03.jpg etc in a single folder 我想将所有tim文件移到tim文件夹中,将jack文件移到jack文件夹中,等等 可以使用.bat文件完成吗?如果是,请共享其代码 谢谢。编辑-更改代码以减少文件移动次数。现在,具有相同前缀的所有文件在一个命令中移动 @echo off setlocal enableextensio

我有不同名称的文件

Tim-01.jpg
Tim-02.jpg
Tim-03.jpg
jack-01.jpg
jack-02.jpg
jack-03.jpg etc in a single folder
我想将所有tim文件移到tim文件夹中,将jack文件移到jack文件夹中,等等

可以使用.bat文件完成吗?如果是,请共享其代码


谢谢。

编辑-更改代码以减少文件移动次数。现在,具有相同前缀的所有文件在一个命令中移动

@echo off

    setlocal enableextensions

    rem source of images
    set "_dir=."

    rem for each jpg with a dash in its name
    for %%f in ("%_dir%\*-*.jpg") do (

        rem if the file still exists (maybe it has been moved) 
        rem then split the file name with the dash as delimiter
        if exist "%%~ff" for /F "tokens=1 delims=-" %%s in ("%%~nf") do (

            rem and if we get a folder target, move the all the files 
            rem with same prefix to proper folder
            if not "%%~s"=="" (
                robocopy "%_dir%" "%_dir%\%%~s" "%%~s-*.jpg" /mov /njs /njh
            )
        )
    )

    endlocal
编辑2-更改为适应评论

@echo off

    setlocal enableextensions enabledelayedexpansion

    rem source of images
    set "_dir=."

    rem for each jpg with a dash in its name
    for %%f in ("%_dir%\*-??.jpg") do (

        rem if the file still exists (maybe it has been moved) 
        if exist "%%~ff" (

            rem get the filename without the 3 last characters
            set "_target=%%~nf"
            set "_target=!_target:~0,-3!"

            rem and if we get a folder target, move the all the files 
            rem with same prefix to proper folder
            if not "!_target!"=="" (
                robocopy "%_dir%" "%_dir%\!_target!" "!_target!-??.jpg" /mov /njs /njh
            )
        )
    )

    endlocal
请注意,
2>nul
会抑制在尝试重新创建现有目录时创建的错误消息


MOVE
只是
ECHO
ed。删除
ECHO
关键字以激活
MOVE
。在
MOVE
语句中添加
>nul
以抑制“1个文件已移动”消息也可能是谨慎的做法。

如您所知,批处理中没有数组。让我们用一个

@ECHO OFF &SETLOCAL
for /f "tokens=1*delims=-" %%a in ('dir /b /a-d *-*.jpg') do set "$%%a=%%a"
for /f "tokens=1*delims==" %%a in ('set $') do robocopy "%cd%" "%cd%\%%b" "%%b*" /mov /l

robocopy
中删除
/l
,使其工作。

在命令提示下键入
copy/?
。这一切都奏效了,但没有什么问题。此代码删除第一个“-”之后的所有内容。我只想删除最后3个字符(-01,-02,-03)。你能告诉我怎么做吗。感谢You@user3020621:文件名称如何?感谢您的即时回复。。。这些文件名为jack-home-01.jpg、jack-home-02.jpg、jack-college-party-01.jpg、jack-college-party-02.jpg等。我想要像jack-home、jack-college-party等文件夹@user3020621:Adapted。
@ECHO OFF &SETLOCAL
for /f "tokens=1*delims=-" %%a in ('dir /b /a-d *-*.jpg') do set "$%%a=%%a"
for /f "tokens=1*delims==" %%a in ('set $') do robocopy "%cd%" "%cd%\%%b" "%%b*" /mov /l