Batch File to create new directories

May 31, 2010 at 11:18:19
Specs: Windows XP
There would be main folder with some name "Main". In this folder we will have n number of files with different names.

The sample filenames would be as follows:
1. BIFLL4_WEST BENGAL-700001_932795867__Merged
2. BIFLL3_WEST BENGAL-700001_932795867__Merged
3. BIFNP1_WEST BENGAL-700001_932795867__Merged
4. BIFNP4_WEST BENGAL-700001_932795867_270510 _Merged

As you can see in the above filenames "932795867" is same and assume that as account no. There will be number of files with same account number. And there will be no of accounts of same kind.

Now I need to sort out these duplicates and keep them into different folders by creating "Main_1", "Main_2", "Main_3" ..... In this folders I need to keep the files where in a particular directoy no same account files should exists. In the "Main" directory we need to contain all the uniques files what we have at the starting.

Please help me how to solve this in batch processing. I have tried but I'm stuck at how to take the account number from the filename and process it.

See More: Batch File to create new directories

Report •

May 31, 2010 at 13:10:02
I would use acct# as dir, like this:
@echo off &>test setlocal enabledelayedexpansion
pushd c:\main
set prev=
For /F "tokens=1-3* Delims=_" %%a in ('dir /b /a-d *merged.*') Do (
if %%c neq prev (
if not exist "c:\main\%%c" >> test echo md "c:\main\%%c"
set prev=%%c)
>> test echo copy /y "c:\main\%%a_%%b_%%c_%%d" "c:\main\%%c\%%a_%%b_%%c_%%d")
::----- end
run the test and see if it's what you wanted.

Report •

May 31, 2010 at 13:33:44
So would I use A/C nums.

@echo off & setLocal EnableDELAYedeXpansion

::pushd c:\main
for /f "tokens=* delims= " %%f in ('dir/b/a-d') do (
for /f "tokens=5 delims=-_ " %%a in ("%%~Nf") do (
echo if not exist %%a md %%a
echo move "%%f" %%a

Helping others achieve escape felicity


Report •

May 31, 2010 at 20:05:26
Firstly, I don't want the account numbers as directories as it would create lots of mess for me. If it is the case if suppose there are 100 accounts with two times repeated then according to the above programs it would create 100 directories.

What I require is I want Main_1 to be created and all the files duplicated to be sent into that Main_1 directory, as a result we have unique files in both the "Main" and "Main_1" directorires.

When I have run your program its popping up "The system
cannot find the file *.*."

Please check it once

Report •

Related Solutions

May 31, 2010 at 20:15:16
my apologies, my dumb mistake. got for /f crossed up.
change this line:
For /F "tokens=1-3* Delims=_" %%a in (*.*) Do (
For /F "tokens=1-3* Delims=_" %%a in ('dir /b /a-d *.*') Do (

Report •

June 3, 2010 at 03:18:24
Can anyone help me with the above query.. Please see the
Response no:3 for clarification in question.

If this is not possible using batch programming is it possible with
any scripting language?

Report •

June 3, 2010 at 13:29:25
is this what you mean?:
A_926 --> main1
B_926 --> main2
C_926 --> main3
X_920 --> main4
Y_920 --> main5
A_921 --> main1
A_922 --> main1
D_938 --> main6
B_921 --> main2
where A,B,C are "accounts" and 9nn is the account no.
then you want each unique prefix to have its own subdir
with all the account no.s having that prefix inside that subdir.:
main1: A_921 A_922 A_926
main2: B_921 B_926
main4: X_920
and so forth.
your description is kind of confusing is reason i'm asking again. Maybe if you can present a more robustly populated
sampling and how you want them to go.
The problem is not the problem, the description is the problem. (no offense, it happens often in here.)
Also need an idea about how conistant the filename patterns
are (no. of digits/chars in sub-segments, placements of _, )

Report •

Ask Question