Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 将文件移动到基于文件名批处理脚本创建的新位置_Batch File - Fatal编程技术网

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

Batch file 将文件移动到基于文件名批处理脚本创建的新位置,batch-file,Batch File,我有几个文件需要按月组织到由包含字符串和数字的文件名创建的新文件夹中 示例文件: 自行车2006_p.zip 自行车2307_P.zip 自行车2410_P.zip 结果: 2006年6月\Cycle2006\u p.zip 7月\Cycle2307\u P.zip 10月\Cycle2410\u P.zip 这是我试过的。但结果是不同的。脚本仅捕获Cycle2410_P.zip并仅创建Oct文件夹 结果: 2006年10月\Cycle2006\u p.zip \自行车2307_P.zip \自

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

示例文件:

自行车2006_p.zip 自行车2307_P.zip 自行车2410_P.zip

结果:

2006年6月\Cycle2006\u p.zip 7月\Cycle2307\u P.zip 10月\Cycle2410\u P.zip

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

结果:

2006年10月\Cycle2006\u p.zip \自行车2307_P.zip \自行车2410_P.zip

我已经试着修正你的常规——修改全大写字母。如果被替换,您的代码将被删除

变化:

添加SETLOCAL以在例程完成时放弃环境更改

将name设置为nothing允许test after for指示是否找到匹配的文件

检测是否找到文件。信息,如果没有,请转到COPI

将一个文件移动到新位置-注意从%FOLDER%和%name%重建的名称

成功后,再次转到,而不是COPI以处理下一个文件


谢谢@magoo。这就是我需要的。我只需要从移动中删除%FOLDER%,因为已经指定了位置,或者使用移动%FOLDER%\%NAME%,一切正常。
@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