I’m looking for batch to make directory with subdirectories from files names and copy files with the same names to direct directory.
I have multiple files with names like :032557.txt ,032557.csv, 032557.log , 054784.csv , 054784.txt, 054784.log etc. I have batch which makes directory with name from file and copy all files with the same names to content directory. In result I have directory 054784 with file 054784.csv, 054784.txt, 054784.log. I need in result directory 054784 with subdirectories: CSV (where I need file 054784.csv) , TXT (where I need file 054784.txt) and LOG (where I need file 054784.log) etc.
@echo off
for /f "delims=" %%a in ('dir /b /a-d') do (
if not "%%~fa"=="%~f0" (
md "%%~na" 2>nul
if exist "%%a" move "%%~na.*" "%%~na"
)
)
Any suggestions ?
%%~xa will give the extension of the file. The only slightly tricky bit is elimination of the leading dot from the extension.
All of the following are untested. They should work unless I have some silly bugs.
If a file name might contain
!or^then the above will not work because delayed expansion will corrupt the expansion of %%A. The following variation will solve this problem:Alternatively you could use this to get around the
!and^problem: