0

I have a C:\CurrentProjects directory and a C:\HistoricProjects directory

The current projects directory is laid out C:\CurrentProjects\LocationName\ProjectYear####\ProjectTitle\etc...

I would like to do a search of a specific Project Year and move it and all subfolders under to the:

C:\HistoricProjects\LocationName\etc...

with the same location name used without moving any other project years.

So for example:

C:\CurrentProjects\Chicago\ProjectYear2013\etc...

C:\CurrentProjects\Chicago\ProjectYear2014\etc...

C:\CurrentProjects\Chicago\ProjectYear2015\etc...

C:\CurrentProjects\Seattle\ProjectYear2013\etc...

C:\CurrentProjects\Seattle\ProjectYear2014\etc...

C:\CurrentProjects\Seattle\ProjectYear2015\etc...

VVVV

C:\HistoricProjects\Chicago\ProjectYear2013\etc..

C:\HIstoricProjects\Seattle\ProjectYear2013\etc..

Eric
  • 3
  • 1
  • Okay, so what's your question? StackOverflow is a question and answer site, not a "make a program for me" site (wouldn't that be great for students if we were! free homework!) . What have you tried already? Did you want to use DOS or KSH, etc? – rlb.usa Jun 30 '15 at 16:00
  • Sorry new to this. Self taught the very basics of batch coding in DOS yesterday and still getting a hang of this. So far I have got @echo off down :). – Eric Jun 30 '15 at 16:05
  • gotcha. Can you put in what you have so far? – rlb.usa Jun 30 '15 at 16:06
  • 1
    (shift-enter on here I see) @echo off Set /P YEAR=Project Year to move? FOR %%A IN (ATTRIB C:\ActiveTest\*\"ProjectYear %YEAR%") DO I can't figure out how to make DIR look back and only pull the 2nd level into a variable. If I can get that I figure I can work it into a ROBOCOPY somehow. – Eric Jun 30 '15 at 16:09
  • http://stackoverflow.com/questions/31144045/how-do-i-re-use-my-wildcard-match Hoping it's possible to use wildcard matches so you could potentially do `move C:\CurrentProjects\(*)\(*)2014 C:\HistoricProjects\(\1)\(\2)2014` . If it isn't possible and such a simple solution appeals to you, you may want to check out CYGWIN, a UNIX shell for windows, especially if you do a lot of "batch" things like this. – rlb.usa Jun 30 '15 at 16:57
  • Good work with your `YEAR=` so that your variable is `%YEAR%` instead of writing `YEAR =` so that your variable would be `%YEAR %` – rlb.usa Jun 30 '15 at 17:28

1 Answers1

0

Try something like this in your testing directory:

 FOR /D %d IN (*\ProjectYear2014\) DO (MOVE %d\ProjectYear2014\ C:\HistoricProjects\%d\ProjectYear2014 )

The basics of that command is :

 FOR /D %d IN             :: for directories, store matches in variable %d
 (*\ProjectYear2014\)     :: that are located in (anything)\ProjectYear2014
 DO (MOVE %d\ProjectYear2014\               :: Move target (match)\... 
 C:\HistoricProjects\%d\ProjectYear2014 )   :: To new directory ...

And I just wanted to explain that :: is the DOS equivalent of a comment, but it's extremely finicky about them. Comments should never be used inside a code block (say, inside a FOR block), and CMD will barf if you try to make the command span multiple lines like second example. I just wrote it that way to help explain what was going on.

This is the code you had already:

@echo off 
Set /P YEAR=Project Year to move? 
FOR %%A IN (ATTRIB C:\ActiveTest*\"ProjectYear %YEAR%") DO 
::I can't figure out how to make DIR look back and only pull the 2nd level into a variable.
::If I can get that I figure I can work it into a ROBOCOPY somehow.

Combining them, your final result will be something LIKE this, but probably not exactly this:

@echo off 
Set /P YEAR=Project Year to move? 
FOR /D %d IN (C:\CurrentProjects\*\ProjectYear%YEAR%\) DO (MOVE C:\CurrentProjects\%d\ProjectYear%YEAR%\ C:\HistoricProjects\%d\ProjectYear%YEAR%\ )

Phew, this was a learning process for me.

rlb.usa
  • 14,942
  • 16
  • 80
  • 128
  • Related to Q : http://stackoverflow.com/questions/31144045/how-do-i-re-use-my-wildcard-match – rlb.usa Jun 30 '15 at 17:29
  • 1
    Thanks for the help! This is running smoothly but still not moving the documents. You pointed out that it is creating a parameter to store the directories information and then running the command at the end based on each parameter, this I think explains a lot. I think what I need to do is create another parameter that is pulling the location level names instead of treating it as a variable. That way I can use a absolute path to those in the second part. I will try putting it all together and see if it works! – Eric Jun 30 '15 at 17:58
  • Solution: `@ECHO OFF SETLOCAL ENABLEDELAYEDEXPANSION Robocopy C:\CurrentProjects C:\HistoricProjects /e /xf * /lev:2 /ns /nc /nfl /ndl /njh /njs :Start SET /p YEAR=What is the Project Year to move? for /d %%G in (*) do ( Echo %%G Move "C:CurrentProjects\%%G\ProjectYear%YEAR%" "C:\HistoricProjects\%%G" ) Pause Echo Move another Project Year? Choice /c YN IF errorlevel 2 goto :End IF errorlevel 1 goto :Start :End for /f "delims=" %%d in ('dir C:\HistoricProjects /s /b /ad ^| sort /r') do (rd "%%d")` – Eric Jul 02 '15 at 14:07
  • @Eric make your own answer so you can accept it as the solution : ) Then anyone else who has a question like yours will see that your answer is the answer to the problem. : ) – rlb.usa Jul 02 '15 at 15:33