2013-11-22 241 views
0

我有不同名稱的文件...創建文件夾

Tim-01.jpg 
Tim-02.jpg 
Tim-03.jpg 
jack-01.jpg 
jack-02.jpg 
jack-03.jpg etc in a single folder 

我想移動的所有文件添添成文件夾,文件插孔傑克文件夾等

它能否使用.bat文件完成?如果是,請分享它的代碼。

謝謝。

+0

在命令提示符下鍵入'copy /?'。這一切都在 –

回答

0

編輯 - 更改了代碼以減少文件移動次數。現在,具有相同前綴的所有文件都將移入一個命令中。

@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 
+0

這工作,但幾乎沒有問題。此代碼刪除第一個' - '後的所有內容。我只想刪除最後3個字符(-01,-02,-03)。你能告訴我怎麼做。謝謝 – user3020621

+0

@ user3020621:如何命名這些文件? –

+0

感謝您的即時回覆...這些文件被命名爲jack-home-01.jpg,jack-home-02.jpg,jack-college-party-01.jpg,jack-college-party-02.jpg等。我想要jack-home,jack-college-party等文件夾。 – user3020621

1
@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個文件被移動」消息也是謹慎的。

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,使其工作。