1

I wrote this very simples batch file to organize the files in a directory by moving them into folders by date. So, a file whose latest modified date is 31-03-2018 goes to folder "2018-03-31 - ".

The script is as follows:

@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%a IN (*) DO (
    IF NOT "%%a"=="%~nx0" (
        FOR %%b IN (%%a) DO (SET FileDate=%%~tb)
        SET day=!FileDate:~0,2!
        SET month=!FileDate:~3,2!
        SET year=!FileDate:~6,4!
        SET filename=!year!-!month!-!day! - 
        IF NOT EXIST "!filename!" (MD "!filename!")
        MOVE /-Y "%%a" "!filename!"
    )
)
ENDLOCAL
PAUSE

Given this directory

This happens. See that weird folder on the top? Why is that happening. It shouldn't be happening since I have EnableDelayedExpansion.

This is what's inside that folder. Just normal files like all the other ones.

Can someone shed a light on why this is happening and maybe kindly provide a fix?

EDIT 1:

This is the console output for a directory with 3 files:

D:\Leonor\Fotos\Família\2005>´╗┐@ECHO off
'´╗┐@ECHO' is not recognized as an internal or external command,
operable program or batch file.

D:\Leonor\Fotos\Família\2005>SETLOCAL ENABLEDELAYEDEXPANSION

D:\Leonor\Fotos\Família\2005>FOR %a IN (*) DO (
Rem @ECHO %a>test.txt
 Rem @ECHO Organizador de Fotos_old.bat>>test.txt
 IF NOT "%a" == "Organizador de Fotos_old.bat" (
Rem Se o ficheiro não for este batch...
 FOR %b IN (%a) DO (SET FileDate=%~tb )
 ECHO !FileDate!
 SET day=!FileDate:~0,2!
 ECHO !day!
 SET month=!FileDate:~3,2!
 ECHO !month!
 SET year=!FileDate:~6,4!
 ECHO !year!
 SET filename=!year!-!month!-!day! -
 ECHO !filename!
 IF NOT EXIST "!filename!" (MD "!filename!" )
 Rem MOVE /-Y "%a" "!filename!"
)
)

D:\Leonor\Fotos\Família\2005>(
Rem @ECHO Imagem - Carnaval+Pai Nosso - 2005 001.jpg>test.txt
 Rem @ECHO Organizador de Fotos_old.bat>>test.txt
 IF NOT "Imagem - Carnaval+Pai Nosso - 2005 001.jpg" == "Organizador de Fotos_old.bat" (
Rem Se o ficheiro não for este batch...
 FOR %b IN (Imagem - Carnaval+Pai Nosso - 2005 001.jpg) DO (SET FileDate=%~tb )
 ECHO !FileDate!
 SET day=!FileDate:~0,2!
 ECHO !day!
 SET month=!FileDate:~3,2!
 ECHO !month!
 SET year=!FileDate:~6,4!
 ECHO !year!
 SET filename=!year!-!month!-!day! -
 ECHO !filename!
 IF NOT EXIST "!filename!" (MD "!filename!" )
 Rem MOVE /-Y "Imagem - Carnaval+Pai Nosso - 2005 001.jpg" "!filename!"
)
)

D:\Leonor\Fotos\Família\2005>(SET FileDate= )

D:\Leonor\Fotos\Família\2005>(SET FileDate= )

D:\Leonor\Fotos\Família\2005>(SET FileDate= )

D:\Leonor\Fotos\Família\2005>(SET FileDate= )

D:\Leonor\Fotos\Família\2005>(SET FileDate= )

D:\Leonor\Fotos\Família\2005>(SET FileDate= )

D:\Leonor\Fotos\Família\2005>(SET FileDate= )
ECHO is on.
~0,2
~3,2
~6,4
~6,4-~3,2-~0,2 -

D:\Leonor\Fotos\Família\2005>(
Rem @ECHO Organizador de Fotos.bat>test.txt
 Rem @ECHO Organizador de Fotos_old.bat>>test.txt
 IF NOT "Organizador de Fotos.bat" == "Organizador de Fotos_old.bat" (
Rem Se o ficheiro não for este batch...
 FOR %b IN (Organizador de Fotos.bat) DO (SET FileDate=%~tb )
 ECHO !FileDate!
 SET day=!FileDate:~0,2!
 ECHO !day!
 SET month=!FileDate:~3,2!
 ECHO !month!
 SET year=!FileDate:~6,4!
 ECHO !year!
 SET filename=!year!-!month!-!day! -
 ECHO !filename!
 IF NOT EXIST "!filename!" (MD "!filename!" )
 Rem MOVE /-Y "Organizador de Fotos.bat" "!filename!"
)
)

D:\Leonor\Fotos\Família\2005>(SET FileDate= )

D:\Leonor\Fotos\Família\2005>(SET FileDate= )

D:\Leonor\Fotos\Família\2005>(SET FileDate= )
ECHO is on.
~0,2
~3,2
~6,4
~6,4-~3,2-~0,2 -

D:\Leonor\Fotos\Família\2005>(
Rem @ECHO Organizador de Fotos_old.bat>test.txt
 Rem @ECHO Organizador de Fotos_old.bat>>test.txt
 IF NOT "Organizador de Fotos_old.bat" == "Organizador de Fotos_old.bat" (
Rem Se o ficheiro não for este batch...
 FOR %b IN (Organizador de Fotos_old.bat) DO (SET FileDate=%~tb )
 ECHO !FileDate!
 SET day=!FileDate:~0,2!
 ECHO !day!
 SET month=!FileDate:~3,2!
 ECHO !month!
 SET year=!FileDate:~6,4!
 ECHO !year!
 SET filename=!year!-!month!-!day! -
 ECHO !filename!
 IF NOT EXIST "!filename!" (MD "!filename!" )
 Rem MOVE /-Y "Organizador de Fotos_old.bat" "!filename!"
)
)

D:\Leonor\Fotos\Família\2005>ENDLOCAL

D:\Leonor\Fotos\Família\2005>PAUSE
Press any key to continue . . .
4
  • You should debug batch-files in an open cmd window and post text instead of pictures. Before creating folders or moving files echo the commands, so you can see beforehand what's working or not. The for %%b is unneccessary, %%~ta will already return the FileDate.
    – LotPings
    Jun 29, 2018 at 21:57
  • 2
    @LotPings Thanks for the info, this is actually the first time I wrote a batch so I'm still getting the hang of it. I removed that for %%b and it's working properly now for some reason! I will edit the post to comply with you tips
    – Michael
    Jun 29, 2018 at 22:09
  • If you now have a working solution, don't post the uncut output of the failing one.
    – LotPings
    Jun 30, 2018 at 5:53
  • Please don't edit the answer into your question. Super User is a question and answer site and answers should be separate from questions. You can answer your own question instead.
    – DavidPostill
    Jul 7, 2018 at 22:01

1 Answer 1

1

I got it fixed by changing the code to

@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%a IN (*) DO (
    IF NOT "%%a"=="%~nx0" (
        SET FileDate=%%~ta
        SET day=!FileDate:~0,2!
        SET month=!FileDate:~3,2!
        SET year=!FileDate:~6,4!
        SET filename=!year!-!month!-!day! - 
        IF NOT EXIST "!filename!" (MD "!filename!")
        MOVE /-Y "%%a" "!filename!"
    )
)
ENDLOCAL
PAUSE

(removed the nested loop)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .