Solved

Mass moving of files into folders based on file name

Posted on 2006-06-21
4
2,446 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
Comment Utility
:: 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Are you unable to synchronize your OST (Offline Storage Table) file with Microsoft Exchange Server? Is your OST file exceeding 2 GB size limit? In Microsoft Outlook 2002 and earlier versions, there is a 2 GB size limit for the OST file. If the file …
cPanel is a Unix based web hosting control panel that provides a graphical interface and automation tools designed to simplify the process of hosting a web site. cPanel utilizes a 3 tier structure that provides functionality for administrators, rese…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

17 Experts available now in Live!

Get 1:1 Help Now