为每个文件名创建文件夹并移动文件

时间:2018-05-29 07:24:10

标签: windows batch-file

我有批处理文件,需要根据csv文件的前几个数字对csv文件进行排序。这些文件位于一个文件夹中,在检测到下划线之前的前几个数字后,它应该为它们分配并设置一个名称,例如

  1. C:\ FDM \ FDMConversion \ 890_AMMC1_Recording_2012.csv
  2. C:\ FDM \ FDMConversion \ 898000_AMMC1_Recording_2012.csv
  3. 基本上,我希望脚本选择每个文件并创建一个文件夹。 例如,要移动890_AMMC1_Recording_2012.csv,脚本将提取890并创建文件夹sample1,然后将文件移动到sample1中。 并移动898000_AMMC1_Recording_2012.csv脚本将提取898000并创建文件夹sample2并将文件移动到sample2文件夹

    代码如下:

        set filename="C:\FDM\FDMConversion\*.csv"
    
    for /f %%i in ('dir %filename% /b /a-d ') do (
        set "filename=%%~i"
        for /f "delims=_ tokens=1" %%a in ('echo !filename!') do (  
    
    rem rem    set folder=%%a 
    
     If "%%a"=="890" ( 
    set sample1=%%a
    mkdir "C:\FDM\FDMConversion\!sample1!"   2>nul
    for /f "delims=_ tokens=1" %%a in ('echo !filename!') do set ade=%%a
    
     pushd "C:\FDM\FDMConversion"
    for /r %%a in ("\!ade!*.csv" ) do (
       move /Y "%%a" "C:\FDM\FDMConversion\!ade!\%%~nxa"
    
       )
    popd
    )
    
    
    If "%%a"=="898000" ( 
    set sample2=%%a
    mkdir "C:\FDM\FDMConversion\!sample2!"   2>nul
    for /f "delims=_ tokens=1" %%a in ('echo !filename!') do set ade=%%a
     pushd "C:\FDM\FDMConversion"
    for /r %%a in ("\!ade!*.csv" ) do (
       move /Y "%%a" "C:\FDM\FDMConversion\!ade!\%%~nxa"
    
       )
    popD
    )
    
    )
    

1 个答案:

答案 0 :(得分:0)

    echo off 
SETLOCAL ENABLEDELAYEDEXPANSION



set filename="C:\FDM\FDMConversion\*.csv"

for /f %%i in ('dir %filename% /b /a-d ') do (
    set "filename=%%~i"
    for /f "delims=_ tokens=1" %%a in ('echo !filename!') do (  

rem rem    set folder=%%a 

 If "%%a"=="890" ( 

mkdir "C:\FDM\FDMConversion\sample1"   2>nul
for /f "delims=_ tokens=1" %%a in ('echo !filename!') do set ade=%%a

 pushd "C:\FDM\FDMConversion"
for /r %%a in ("\!ade!*.csv" ) do (
   move /Y "%%a" "C:\FDM\FDMConversion\sample1\%%~nxa"

   )
popd
)


If "%%a"=="898000" ( 

mkdir "C:\FDM\FDMConversion\sample2"   2>nul
for /f "delims=_ tokens=1" %%a in ('echo !filename!') do set ade=%%a
 pushd "C:\FDM\FDMConversion"
for /r %%a in ("\!ade!*.csv" ) do (
   move /Y "%%a" "C:\FDM\FDMConversion\sample2\%%~nxa"

   )
popD

)


)




   )