我有不同名称的文件...创建文件夹
Tim-01.jpg
Tim-02.jpg
Tim-03.jpg
jack-01.jpg
jack-02.jpg
jack-03.jpg etc in a single folder
我想移动的所有文件添添成文件夹,文件插孔杰克文件夹等
它能否使用.bat文件完成?如果是,请分享它的代码。
谢谢。
我有不同名称的文件...创建文件夹
Tim-01.jpg
Tim-02.jpg
Tim-03.jpg
jack-01.jpg
jack-02.jpg
jack-03.jpg etc in a single folder
我想移动的所有文件添添成文件夹,文件插孔杰克文件夹等
它能否使用.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
EDIT2 - 改变以适应评论
@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
这工作,但几乎没有问题。此代码删除第一个' - '后的所有内容。我只想删除最后3个字符(-01,-02,-03)。你能告诉我怎么做。谢谢 – user3020621
@ 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
@echo off
setlocal
set sourcedir=c:\sourcedir
for /f "tokens=1*delims=-" %%a in ('dir /b /a-d "%sourcedir%\*-*.*") do (
md "%sourcedir%\%%a" 2>nul
echo move "%sourcedir\%%a-%%b" "%sourcedir%\%%a\"
)
注意,2>nul
抑制当试图重新创建现有目录
的MOVE
仅仅是创建的错误消息ECHO
ed。删除ECHO
关键字以激活move
。将>nul
附加到MOVE
语句以抑制“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 /?'。这一切都在 –