Batch file to create and move files

October 10, 2011 at 15:52:28
Specs: Windows XP
I hope someone can help. I have about more than 700 folders in 9xxxxx format. In each folder, there are multiple text files. We want to go into each folder, create sub-folders based on file names and move the text files into the sub-folders. So far, for that part, I have created the script below to create and move files. However, I want to repeat that in multiple 9xxxx folders. How?

@echo off
for /f %%F in ('dir/b/a-d *.TXT') do call :sub1 %%F
goto :eof
:sub1
set name=%1
md %name:~0,6%
move %* %name:~0,6%

Example:

now:
\912345\394948-1.txt
\912345\394948-2.txt
\912345\400000-1.txt
\900000\12345-1.txt
\900000\12345-2.txt

to
\912345\394948\394948-1.txt
\912345\394948\394948-2.txt
\912345\400000\400000-1.txt
\900000\12345\12345-1.txt
\900000\12345\12345-2.txt


See More: Batch file to create and move files

Report •


#1
October 11, 2011 at 02:45:44
@echo off
setLocal EnableDelayedExpansion
set root=%CD%
cls
for /f "tokens=* delims=" %%a in ('dir /AD /B') do (
set /a q+=1
set folder_name!q!=%%a
)
for /l %%a in (1,1,%q%) do (
cd "!folder_name%%a!"
for /f "tokens=* delims=" %%b in ('dir /B *.txt') do (
set name=%%~nb
set name=!name:~0,6!
if not exist !name! (
md !name!
)
move !name!*.txt !name!
)
cd "%root%"
)
cls

Report •

#2
October 11, 2011 at 15:12:45
Thank you MoneyMoney for your help. Your script works like a charm!

Report •
Related Solutions


Ask Question