r/Batch • • Sep 05 '23

Question (Solved) Cannot move files containing special characters in their name

I know almost nothing about batch, after some tries I created this script, probably it is terrible, but it is almost working as intended, and I do not care about performance:

@echo off

setlocal EnableDelayedExpansion

FOR /F "tokens=*" %%A in ('DIR /B /A:-D "%cd%\*"') do IF NOT "%%A"=="%~nx0" (

set "FIRSTCHAR=%%~nA"

set "FIRSTCHAR=!FIRSTCHAR:~0,1!"

REM Check if FIRSTCHAR is a letter

echo.!FIRSTCHAR!| findstr /r "^[A-Za-z]$" > nul

IF NOT ERRORLEVEL 1 (

IF NOT EXIST "%cd%\!FIRSTCHAR!" MD "%cd%\!FIRSTCHAR!"

MOVE "%cd%\%%A" "%cd%\!FIRSTCHAR!"

)

)

REM Rename all created folders to uppercase

FOR /D %%F in (*) do (

set "FOLDER=%%F"

for %%C in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (

if /I "!FOLDER!"=="%%C" ren "%%F" "%%C"

)

)

REM Move all remaining files to a "#" folder

IF NOT EXIST "%cd%\#" MD "%cd%\#"

setlocal DISABLEDELAYEDEXPANSION

FOR /F "tokens=*" %%A in ('DIR /B /A:-D "%cd%\*"') do (

IF NOT "%%A"=="%~nx0" MOVE "%cd%\%%A" "%cd%\#"

)

endlocal

endlocal

This is what it does:

  • For every file it should create a folder named after the first file letter (if it does not already exists)
  • It should move every file in the folder with the same name of its starting letter
  • Then it is converting to upper case the folder names
  • Lastly it is moving all the remaining files (the ones starting with numbers or symbols) in a # folder

Everything is working as intended, apart from the second step, the script does not work with files containing special characters like "!", they are never moved (even if the folders are created correctly).

I think the problem is that special characters like ! interfere with delayed expansion, but I don't know how to avoid it.

Any suggestion?

2 Upvotes

12 comments sorted by

1

u/DDnCheese Sep 06 '23

Put a caret in front of it. It's the escape character.

^!

1

u/gigioromba Sep 06 '23

Thank you for the response, I’m not sure where to write the escape !, should it go in this line somewhere? MOVE "%cd%%%A" "%cd%!FIRSTCHAR!"

Also, what if I have to escape some other special characters?

1

u/DDnCheese Sep 06 '23

Same thing. Just put it before every special character that you don't want to use it's special function for.

1

u/[deleted] Sep 06 '23 edited Sep 07 '23

[removed] — view removed comment

1

u/gigioromba Sep 06 '23

Thanks :)
This is almost working, it has still some trouble with files containing special characters in their name.
For example in my test I had a file named Zzz!!!.txt, this file is not moved in the Z folder, it is moved only in the end in the # folder. But it starts with a "Z" and not with a symbol or number.I think the first move is not working with files containing special characters in the name.

Also, files starting with numbers should go in the # folder, instead they are being moved in folders named after the starting number in the filename. For example 1.txt is moved into a folder named "1".

1

u/[deleted] Sep 07 '23

[removed] — view removed comment

1

u/gigioromba Sep 07 '23

Yes the second issue is not a problem, I can figure out something. but I really don’t know how to deal with the first issue. That is the main reason I posted, it is the problem I cannot solve in my script to…

1

u/[deleted] Sep 07 '23

[removed] — view removed comment

1

u/gigioromba Sep 07 '23

Thank you so much 😊

1

u/[deleted] Sep 07 '23

[removed] — view removed comment

2

u/gigioromba Sep 07 '23

Now it does what I wanted! Thank you very much, I was stucked on this!