Moving subdirectory contents up one level

synce

Distinguished
Apr 27, 2011
42
0
18,540
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?
 

synce

Distinguished
Apr 27, 2011
42
0
18,540
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...
 

chugot9218

Honorable
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*
 

chugot9218

Honorable
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.
 

dalaran

Distinguished
Jun 7, 2011
180
0
18,710
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}


[cpp]
@echo on

:: ADJUST THIS PATH IF NEEDED
f:
cd F:\pc98\

for /f %%i in ('dir /ad /b') do (
CALL :Level1 "%%i"
)

Goto end

::--------------------------------------------
:: LEVEL 1
:: SKIP THIS LEVEL, JUST ENTER EVERY DIRECTORY
::--------------------------------------------
:Level1
pushd "%1"
for /f %%i in ('dir /ad /b') do (
CALL :Level2 "%%i"
)
popd
Goto end


::--------------------------------------------
:: LEVEL 2 (or more)
:: MOVE EVERY FILE ONE LEVEL DOWN
::--------------------------------------------
:Level2
pushd "%1"

move *.* ..

for /f %%j in ('dir /ad /b') do (
CALL :Level2 "%%j"
)

popd
Goto end

::--------------------------------------------
::END
::--------------------------------------------
:End
[/cpp]