Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I work in document control and have a large number of documents saved in one folder, that folder also contains a folder called "old".

What I need to do is filter through the 1000's of documents in the main folder and place old revisions into the old folder (cut, not copy!). Is it possible to do this using a CMD Command?

My documents all have the same name structure:

xxx_01.pdf for revision 01

xxx_02.pdf for revision 02 etc (the format is not exclusively pdf).

share|improve this question
1  
Do you have to do this once? Cut/paste all documents to the old subfolder, select the _01s in it and move them back one folder up. No need for the command line. But since you're asking for that, it looks as if you want to do this repetitively. If so, when and why? Please update your question and also tell us which operating system. –  Jan Doggen Jun 11 '13 at 12:55
1  
Also, Windows? Powershell OK? Or pure batch / commands? –  AthomSfere Jun 11 '13 at 12:55
    
I am using Windows 7, I have full administrative rights but am limited as to what I can instal due to my companies anti-virus, hence why I would prefer to do this using CMD as opposed to a 3rd party software. If the problem was just with revision 01 documents then this would be easy, however I have literally 1000's of documents going up to revision 10 and want to move all old revisions to the old folder keeping only the latest revisions in the main folder. –  Kevin Anthony Oppegaard Rose Jun 11 '13 at 13:01
    
Kevin, you're still not answering all questions. And edit your question, don't answer in comments. And Windows Explorer is not 3rd party software. –  Jan Doggen Jun 12 '13 at 6:29
add comment

1 Answer 1

up vote 2 down vote accepted

The following batch script does what you want. Copy the code into a file and call it moveOld.bat, and place it in your folder.

@echo off
setlocal enableDelayedExpansion

pushd "%~dp0"
set "prev="
for /f "eol=: delims=" %%F in (
  'dir /b /a-d /oe-n *^|findstr /r "_[0-9][0-9]\.[^.]*$"'
) do (
  set "next=%%~nF"
  set "next=!next:~0,-2!%%~xF"
  if !next! neq !prev! (
    set "prev=!next!"
  ) else (
    move /y "%%F" old >nul
  )
)
popd

You can run the script from a command prompt, or you can simply double click on the script within Windows Explorer.

The script works as long as none of the names contain the ! character - this limitation can be removed with extra code.

If the script finds an outdated file in your folder that already exists in the old folder, then it will simply over-write the existing file in old.

Explanation of how it works

The script lists all files in the directory, sorted by extension, then by name descending. The list is then filtered by FINDSTR to preserve only base names that end with "_nn" where nn is a 2 digit number.

The resultant file list is processed by FOR /F, one at a time. For each file, the 2 digit name suffix is removed and the result compared against the previous file. If it is a new value, then the previous value is updated and the file is preserved. If the value matches the previous value, then the file is moved.

share|improve this answer
    
EDIT - Fixed a bug: must first sort by extension, then by name descending –  dbenham Jun 11 '13 at 14:28
    
Thank you for your answer. I will check first thing tomorrow morning and mark as excepted answer if it works, but +1 for such a detailed answer :) –  Kevin Anthony Oppegaard Rose Jun 11 '13 at 14:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.