Solved

Mass moving of files into folders based on file name

Posted on 2006-06-21
4
2,453 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Ok I have been working on this for some time having learned and gained certification in XenDesktop 4 along came version 5 which was released last month. Since then I have been working to deploy XenDesktop 5 in a small environment with only 2 virt…
It is only natural that we all want our PCs to be in good working order, improved system performance, so that is exactly how programs are advertised to entice. They say things like:            •      PC crashes? Get registry cleaner to repair it!    …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

752 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