Reputation: 11
I have couple of files with names like "Axis_Master Group_First_Report_201312.xlsx", Axis_Master Group_second_Report_201312.xlsx. I need a script to read the file name and based on the file name, create folders and move files into that folder. In the above example, i need the first file to be moved into a folder with the name Axis and with in Axis, it should be moved to a folder First . For the second file, it need to be moved to the same Axis folder but it should be moved into a new folder "second". please help. Thanks in advance.
Upvotes: 1
Views: 112
Reputation: 130839
I assume all files match *_*_*_*.xlsx
, and you want the first and third tokens using _
as a delimiter.
The first FOR simply iterates all the matching files. The subsequent FOR /F parses out the first and third tokens. It never hurts to repeatedly attempt to create the folder(s) multiple times. I simply hide any error message by redirecting stderr to nul.
@echo off
for %%F in (*_*_*_*.xlsx) do for /f "tokens=1,3 delims=_" %%A in ("%%F") do (
md "%%A\%%B" 2>nul
move "%%F" "%%A\%%B"
)
Upvotes: 2
Reputation: 1832
This code moves all files matching the pattern "Axis_Master Group_$1_$2.$3"
into a folder axis\$1
and renames it to $2.$3
.
@echo off
md axis > Nul <&2
for %%a in ("Axis_Master Group_*_*.*") do (
call :refolder "%%a"
)
exit /b
:refolder
set fileName=%~n1
set fileName=%fileName:Axis_Master Group_=%
for /f "delims=_ tokens=1*" %%a in ("%fileName%") do (
md axis\%%a > Nul <&2
move /y %1 axis\%%a\%%b%~x1 > Nul <&2
)
exit /b
Upvotes: 0