我有几个文件需要按月组织到由包含字符串和数字的文件名创建的新文件夹中。
示例文件:
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
@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
我已经尝试修复您的例程 - 全大写的修改。REM
如果更换,您的代码就会失效。
变化:
添加SETLOCAL
以在例程完成时丢弃环境更改。
设置name
为nothing允许 test afterfor
指示是否找到匹配的文件
检测是否找到文件。COPI
如果没有,请留言并前往。
将 ONE 文件移动到新位置 - 从%FOLDER%
和重建的音符名称%NAME%
。
成功后,转到AGAIN
,不COPI
处理下一个文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句