Sign in with
Sign up | Sign in
Your question

Moving subdirectory contents up one level

Last response: in Windows 7
Share
July 24, 2012 10:36:51 AM

I found some scripts that can move the sub-folders or files to a specific location but that's not what I'm after. Here's my situation:

Folder 1\Subfolder A\files.xxx
Folder 2\Subfolder B\files.yyy
Folder 3\filez.zzz
etc etc

I'd like to move files.xxx to Folder 1, files.yyy to Folder 2, and leave all files.zzz alone. There's no consistent naming pattern in the subfolder names or contents and the file formats vary. Is it possible to script this?
a c 228 $ Windows 7
July 24, 2012 12:05:18 PM

How about (relative path):
  1. cd c:\folder1\subfoldera
  2. move *.* ..
  3. cd c:\folder2\subfolderb
  4. move *.* ..


substituting c: for your drive letter.
m
0
l
July 24, 2012 2:50:30 PM

I have no experience with any sort of coding :(  But here's a real world example of what I'm trying to accomplish:

Turn this:
F:\pc98\maho\mahou1.d88
F:\pc98\maho\mahou2.d88
F:\pc98\tetraquiz\tetra\tq1.d88
F:\pc98\tetraquiz\tetra\tq2.d88

Into this:
F:\pc98\maho\mahou.d88
F:\pc98\maho\mahou.d88
F:\pc98\tetraquiz\tq1.d88
F:\pc98\tetraquiz\tq2.d88

several hundred or thousand times over


Basically I need something that will move all 3rd level files one level up, regardless of name or extension (so I guess regex might not help). The files already on the 2nd level should remain where they are, which makes it tricky...
m
0
l
Related resources
a b $ Windows 7
July 24, 2012 2:57:17 PM

You could code up a quick program to do this, I have done things very similar and it doesnt take more then 20-30 lines of code. However, I do not understand some of the details of what you desire to do, is it all files from those folders you want to move or just specific ones? Could you create a REGEX pattern to match the ones you want?

*Edit* Actually I kind of understand your process now, can you provide any other information on the directory structure? Are all subfolders contained in a folder off the root? *Edit*
m
0
l
July 24, 2012 3:19:39 PM

Weird, it posted my response above yours. I appreciate your offer to help :) 
m
0
l
a b $ Windows 7
July 24, 2012 4:11:31 PM

Hmm, it aint pretty but I think it works for what you want. You can set the target directory, it will open all folders inside of that and move the files one level higher. So for example, change c:\\ to F:\pc98\tetraquiz\ and it will copy any file from any folder inside that directory into that one if that makes sense? I think I could add another level so you did not have to alter the root directory but Ill have to think about it for a bit. Does this at all do what you wanted?

public void getDir()
{
//Substitute c:\\ string for root directory of folders
string[] fileEntries = Directory.GetDirectories(@"C:\\");
//Total number of directories inside that folder
int total = fileEntries.Length;
string[] targetDirectories = new string[total];
// MessageBox.Show(total.ToString());
for (int i = 0; i < total; i++)
{
int subIndex = fileEntries.LastIndexOf("\\");
targetDirectories = fileEntries.Substring(0, subIndex);
}
for (int i = 0; i < total; i++)
{
string[] fileNames = Directory.GetFiles(fileEntries);
foreach (string t in fileNames)
{
// MessageBox.Show(t);
// MessageBox.Show(targetDirectories);
int fileIndex = t.LastIndexOf("\\");
string fileName = t.Substring(fileIndex);
string fullPath = targetDirectories + fileName;
File.Move(t, fullPath);
}
}
}

It may be difficult to pick up all the root directories if there are certain ones you do not want to move from. Also I am not responsible for any damage that may becaused by this code lol (I did not provide error handling for overwriting of files). If you could provide a list of folder names to operate it could be programmed to read from that, or even a list of folders you do not want it operating on if that were smaller.

PS. If you want to operate on ALL folders inside of a single folder (PC98 in your example), it would be easy to modify it to do that.
m
0
l
July 24, 2012 5:18:14 PM

Assuming you're using windows XP or more
This should work (create a .cmd file with this)

Use a copy of your files if possible in case I screwed up

It should move every file one level down starting from the 2nd level

Assuming this being level one
F:\pc98\{level1}\

And this being level 2 or more
F:\pc98\{level1}\{level2}
F:\pc98\{level1}\{level2}\{level3}


  1. @echo on
  2.  
  3. :: ADJUST THIS PATH IF NEEDED
  4. f:
  5. cd F:\pc98\
  6.  
  7. for /f %%i in ('dir /ad /b') do (
  8. CALL :Level1 "%%i"
  9. )
  10.  
  11. Goto end
  12.  
  13. ::--------------------------------------------
  14. :: LEVEL 1
  15. :: SKIP THIS LEVEL, JUST ENTER EVERY DIRECTORY
  16. ::--------------------------------------------
  17. :Level1
  18. pushd "%1"
  19. for /f %%i in ('dir /ad /b') do (
  20. CALL :Level2 "%%i"
  21. )
  22. popd
  23. Goto end
  24.  
  25.  
  26. ::--------------------------------------------
  27. :: LEVEL 2 (or more)
  28. :: MOVE EVERY FILE ONE LEVEL DOWN
  29. ::--------------------------------------------
  30. :Level2
  31. pushd "%1"
  32.  
  33. move *.* ..
  34.  
  35. for /f %%j in ('dir /ad /b') do (
  36. CALL :Level2 "%%j"
  37. )
  38.  
  39. popd
  40. Goto end
  41.  
  42. ::--------------------------------------------
  43. ::END
  44. ::--------------------------------------------
  45. :End
m
0
l
!