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
Solved

Mass moving of files into folders based on file name

Posted on 2006-06-21
4
2,451 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
uploading CentOS to Azure (no root access) 3 79
XP won't boot 27 155
XP Install woes 17 111
"This device cannot find enough free resources that it can use" after virtualization... 10 83
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 …
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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