Batch copy commands to match filename to fold

September 22, 2011 at 20:21:44
Specs: Windows 7
I have files in the following format 123~12345.aaa - I need to extract files from their C:\ location into a corresponding folder based on filename ie copy C:\123~*.aaa to C:\123\123~*.aaa

See More: Batch copy commands to match filename to fold

Report •

#1
September 23, 2011 at 14:40:50
THIS IS UNTESTED. I set this up to create a folder called "testfolder", and also to copy and not move in case something is wrong with it.

@echo off

REM outputting list of files to temp file
dir /b c:\*.aaa > filelist.tmp
REM looping through list to cut the filenames down to their first three digits
for /F "tokens=*" %%A in (filelist.tmp) do (
set dir=%A:~0,3%
REM creating a list of future directory names
echo %dir% >> dirlist.tmp
)
REM looping through dirlist.tmp to make directories
for /F "tokens=*" %%A in (dirlist.tmp) do (
if not exist c:\testfolder\%%A md c:\testfolder\%%A

REM looping through dirlist.tmp to move files into new directories
for /F "tokens=*" %%A in (dirlist.tmp) do (
copy c:\%%A*.* c:\%%A\

REM deleting temp files
del /q filelist.tmp
del /q dirlist.tmp


Report •
Related Solutions


Ask Question