0

I have few file that need to be organize by month into new folder that created by the filename that contain string & number.

example file :

Cycle2006_P.zip Cycle2307_P.zip Cycle2410_P.zip

Result:

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

Here what I tried. But the result are different. The script only capture Cycle2410_P.zip and create Oct folder only.

Result :

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 Answer 1

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

I've tried to fix your routine - modifications in ALL-CAPS. Your code REMmed out if replaced.

Changes:

  1. Add SETLOCAL to discard environment changes when routine finishes.

  2. Setting name to nothing allows test after for to indicate whether a matching file was found

  3. Detect whether a file was found. Message and go to COPI if not.

  4. Move the ONE file to new location - note name reconstructed from %FOLDER% and %NAME%.

  5. On success, go to AGAIN, not COPI to process next file.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @magoo. this what i need. I just need to remove %FOLDER% from Move since the location already stated or use Move %FOLDER%\%NAME% , everything work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.