I'm a bit of a novice when it comes to .BAT files. Please, I'm in need of a batch file that will create folders based on the names of files in the local folder, and then move those files into their corresponding folders. The folder names need to be determined by the characters before the delimiter in the file names, in this case an underscore.
For example, it would take these files
june_jahdfjlkaluie2.xlsx
june_jahdfjlkaluie.xlsx
august_nnnvbcnkziery2.xlsx
august_nnnvbcnkziery.xlsx
december_bagjd_kasdgf.xlsx
december_byuueyiuyoi.xlsx
Create these folders
june
august
december
And move the files into those folders based on the characters before the underscore.
This is the code I have so far
@echo off &setlocal
for /f "delims=_" %%i in ('dir /b /a-d *.xls') do (
set "file=%%~i"
setlocal enabledelayedexpansion
set "folder=!file!"
mkdir "!folder!" 2>nul
move "!file!" "!folder!" >nul
)
endlocal
echo Did it work?
Pause
The batch file is able to make the folders based on the file names. However, when it attempts to move the files, it produces an error stating 'The process cannot access the file because it is being used by another process.' I've attempted several fixes, including creating a separate for
command for moving the files, and nothing seems to work.
Any advice is greatly appreciated. Thank you.
would likely work and be easier.
Note that I've changed the target filemask to
….xlsX
because the short filename for a.xslx
file does not necessarily retain the*_*.xls
format.