Solved

Mass moving of files into folders based on file name

Posted on 2006-06-21
4
2,448 Views
Last Modified: 2008-01-09
I have a few thousand files that due to a poor attempt at orginization by myself were dumped into a single directory.

The file names are on the order of wwXXXX-YY-ZZZZZZZ-.ZIP where

ww is the same
XXXX is a four digit number
YY is a two digit number
zzzzz is a varible length descriptor.

I need the script to do

1) Make a directory called wwXXXX
2) Move everything that matches wwXXXX* to the just made directory
3) increase XXXX by 1
4) Repeat until everything is moved correctly.

I started writing a batch file where I could specify the wwXXXX but I realized it would take me a few days to do this by hand and there has to be an easier way

The .CMD script I am using is


mkdir no%1
cd no%1
dir ..\..\no%1-*.*
move ..\..\no%1-*.* .
cd ..


0
Comment
Question by:MannyLNJ
  • 2
  • 2
4 Comments
 
LVL 30

Accepted Solution

by:
callrs earned 500 total points
ID: 16957306
:: MS-DOS script to move files of format prefixDDDD*.ext to folders named DDDD,
::   where DDDD is a number between 0 & 9999
::
:: By Ravinder Singh ('wiz' @ quickmacros forums), 2006-06-22
::
:: Test/Run:
::    - xcopy/y auto-overwrites existing files, xcopy/-y confirms
::    - maxcount = 10 to test; 9999 to do all
::    - ext = file extension
::    - prefix = file name prefix
::    - change "echo del ..." to "del ..." to delete originals
::  
:: Note: using 'copy' instead of 'move' because:
:: --If file exists at move-to location, move (but not copy) gives "access is denied"
:: --Is easier to repeat this script for testing
::
@echo off
setlocal
set prefix=ww
set ext=zip
set maxcount=10
set count=0&set D=

:loop
set D=000%count%
set D=%D:~-4%
if %count%.==%maxcount%. goto :eof
if %count%.==10000. goto :eof
if exist %prefix%%D%*.%ext% (
      if not exist %D%\ md %D%&echo md %D%
      xcopy /y %prefix%%D%*.%ext% %D%\
      echo del %prefix%%D%*.%ext%
      ) else echo %D%
set /a count+=1
goto :loop
::
0
 

Author Comment

by:MannyLNJ
ID: 16959034
Thanks. I'll give that script a shot. If I save it as fixit.bat  I should just type fixit when I cd into the directory my files are in correct?
0
 

Author Comment

by:MannyLNJ
ID: 16959113
Looks good I just need to change it so instead of creating the directory with just the number that is count it uses the prefix plus the number do I change

if exist %prefix%%D%*.%ext% (
     if not exist %D%\ md %D%&echo md %D%
     xcopy /y %prefix%%D%*.%ext% %D%\

to

if exist %prefix%%D%*.%ext% (
     if not exist %D%\ md %D%&echo md %prefix%%D%
     xcopy /y %prefix%%D%*.%ext% %%prefix%D%\
0
 
LVL 30

Expert Comment

by:callrs
ID: 16960047
Try this first (will only echo commands & won't copy anything):

if exist %prefix%%D%*.%ext% (
     if not exist %prefix%%D%\ echo md %prefix%%D%&rem echo md %prefix%%D%
     echo xcopy /y %prefix%%D%*.%ext% %prefix%%D%\

Then when you're happy with the copy commands you see, make it this:

if exist %prefix%%D%*.%ext% (
     if not exist %prefix%%D%\  md %prefix%%D%&echo md %prefix%%D%
     xcopy /y %prefix%%D%*.%ext% %prefix%%D%\
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Migration of Exchange mailbox can be done with the ExProfre.exe tool. But at times, when the ExProfre.exe tool migrates the Exchange Server user profile, it results in numerous synchronization problems. Synchronization error messages appear in the e…
Sometimes people don't understand why download speed shows differently for Windows than Linux.Specially, this article covers and shows the solution for throughput difference for Windows than a Linux machine. For this, I arranged a test scenario.I…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now