Computer Hope

Microsoft => Microsoft Windows => Windows 10 and 11 => Topic started by: chaosmstr on May 09, 2019, 11:51:03 PM

Title: batch move files with special chars
Post by: chaosmstr on May 09, 2019, 11:51:03 PM
So, I found this one previously:
https://www.computerhope.com/forum/index.php?topic=152980.0

Code: [Select]
@echo off
setlocal enabledelayedexpansion
for %%A in (*.*) do (
   echo file found  %%A
   for /f "delims=" %%B in ("%%A") do set fname=%%~nB
   for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
   for /f "tokens=1* delims=-" %%D in ("!fname!") do set folname=%%D
   echo folder name !folname!
   pause
   if not exist "!folname!" (
      echo Folder !folname! does not exist, creating
      md "!folname!"
   ) else (
rem       echo Folder !folname! exists
   )
   echo Moving file %%A to folder !folname!
   move "%%A" "!folname!"
   )
echo Finished
pause

The batch file works great, except for one thing.

I'm working with MP3 files, and there's a lot of titles with ! in the name...
The batch commands aren't catching the special char.

For instance...
     P!nk - So What.mp3
The directory name created is
     Pnk
then there's an error that the file doesn't exist.

Sooo.. how can I tell it to ignore special chars that can occur anywhere in the filename?