将文件移动到基​​于文件名批处理脚本创建的新位置

时间:2019-07-16 03:09:44

标签: batch-file

我只有几个文件需要按月组织到由包含字符串和数字的文件名创建的新文件夹中。

示例文件:

Cycle2006_P.zip Cycle2307_P.zip Cycle2410_P.zip

结果:

Jun\Cycle2006_P.zip Jul\Cycle2307_P.zip Oct\Cycle2410_P.zip

这是我尝试过的。但是结果是不同的。该脚本仅捕获Cycle2410_P.zip并仅创建Oct文件夹。

结果:

Oct\Cycle2006_P.zip \Cycle2307_P.zip \Cycle2410_P.zip

@echo off

Set filename=D:\Users\AALADELA\Desktop\pbilsr01\*.zip*
For %%A in ("%filename%") do (
    Set Folder=%%~dpA
    Set Name=%%~nxA
)

REM get the 7th string from filename and set into %month% e.g. 06 = Jun
set month=%Name:~7,2%


if %month%==01 set currentmonthfolder=Jan
if %month%==02 set currentmonthfolder=Feb
if %month%==03 set currentmonthfolder=Mar
if %month%==04 set currentmonthfolder=Apr
if %month%==05 set currentmonthfolder=May
if %month%==06 set currentmonthfolder=Jun
if %month%==07 set currentmonthfolder=Jul
if %month%==08 set currentmonthfolder=Aug
if %month%==09 set currentmonthfolder=Sep
if %month%==10 set currentmonthfolder=Oct
if %month%==11 set currentmonthfolder=Nov
if %month%==12 set currentmonthfolder=Dec

:Move
@echo off
echo.
echo Move File to New Location
mkdir "D:\Users\AALADELA\Desktop\Backup\%currentmonthfolder%"
Move "D:\Users\AALADELA\Desktop\pbilsr01\*.zip*" "D:\Users\AALADELA\Desktop\Backup\%currentmonthfolder%" 

set RESULT=%ERRORLEVEL%
if %RESULT% equ 0 (
  echo.
  echo Success Move
  GOTO Copi
) else (
  echo Error. Retry. . . .
  TIMEOUT /T 5 >nul
  GOTO Move
)
pause

1 个答案:

答案 0 :(得分:1)

@echo off

SETLOCAL
:AGAIN
SET "NAME="

Set filename=D:\Users\AALADELA\Desktop\pbilsr01\*.zip*
For %%A in ("%filename%") do (
    Set Folder=%%~dpA
    Set Name=%%~nxA
)

IF NOT DEFINED NAME ECHO No files found&GOTO COPI

REM get the 7th string from filename and set into %month% e.g. 06 = Jun
set month=%Name:~7,2%


if %month%==01 set currentmonthfolder=Jan
if %month%==02 set currentmonthfolder=Feb
if %month%==03 set currentmonthfolder=Mar
if %month%==04 set currentmonthfolder=Apr
if %month%==05 set currentmonthfolder=May
if %month%==06 set currentmonthfolder=Jun
if %month%==07 set currentmonthfolder=Jul
if %month%==08 set currentmonthfolder=Aug
if %month%==09 set currentmonthfolder=Sep
if %month%==10 set currentmonthfolder=Oct
if %month%==11 set currentmonthfolder=Nov
if %month%==12 set currentmonthfolder=Dec

:Move
@echo off
echo.
echo Move File to New Location
mkdir "D:\Users\AALADELA\Desktop\Backup\%currentmonthfolder%"

REM    Move "D:\Users\AALADELA\Desktop\pbilsr01\*.zip*" "D:\Users\AALADELA\Desktop\Backup\%currentmonthfolder%" 
Move "D:\Users\AALADELA\Desktop\pbilsr01\%FOLDER%%NAME%" "D:\Users\AALADELA\Desktop\Backup\%currentmonthfolder%" 


set RESULT=%ERRORLEVEL%
if %RESULT% equ 0 (
  echo.
  echo Success Move

  REM      GOTO Copi
  GOTO AGAIN

) else (
  echo Error. Retry. . . .
  TIMEOUT /T 5 >nul
  GOTO Move
)
pause

我已尝试解决您的常规问题-修改ALL-CAPS。如果您的代码REM被替换,则会被清除。

更改:

  1. 添加SETLOCAL以在例程结束后放弃环境更改。

  2. name设置为 nothing 不会在for之后进行测试,以表明是否找到了匹配的文件

  3. 检测是否找到文件。留言,然后转到COPI

  4. 将ONE文件移动到新位置-从%FOLDER%%NAME%重建的笔记名称。

  5. 成功后,转到AGAIN,而不是COPI处理下一个文件。