Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Mass moving of files into folders based on file name

Posted on 2006-06-21
4
Medium Priority
?
2,468 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

If your system is showing symptoms of browser hijacks or 'google search redirects' check out my other article (http://rdsrc.us/u3GP7A) first and run the tool TDSSKiller (http://rdsrc.us/GDBBs4) to get rid of the infection. Once done, and if the …
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

885 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