What PS move-item script to move files and NOT flatten directory structure?

Doppel

Lifer
Feb 5, 2011
13,306
3
0
I have this folder structure with various files within various levels:

LEV1
LEV1\LEV2
LEV1\LEV2\FOLDER A
LEV1\LEV2\FOLDER B
..etc.

Also a destination folder DESTLEV1

A simple move-item will move these files but flattens the structure. It's necessary to maintain it, so that every time I run it I move folders and files, maintaining their order--anything from LEV1.

I can't find a good example online that actually works. Thanks for any ideas :)
 

MerlinRML

Senior member
Sep 9, 2005
207
0
71
Maybe I don't understand what you're trying to accomplish, but I used move-item and it did what you seem to want to do.

Code:
move-item -path c:\LEV1 -destination d:\DESTLEV1

resulted in
Code:
d:\DESTLEV1\LEV1\
d:\DESTLEV1\LEV1\FOLDER A
d:\DESTLEV1\LEV1\FOLDER A\file.txt
d:\DESTLEV1\LEV1\FOLDER B
...
 

Doppel

Lifer
Feb 5, 2011
13,306
3
0
Thanks, merlin, and you do understand. In fact, what you write works as I asked, which was my bad.

To my amazement, although your script works to move the LEV1 and all its folders into DESTLEV1, what doesn't work is if instead of LEV1 I want to move LEV2 (and in fact that is the case).

If I only want to move LEV2 and its subfolders and files and leave LEV1 where it is I get

"Access to the path 'c:\LEV1\LEV2' is denied

SO I can move LEV1 and its contents, but not if I get a little more narrow and only LEV2 and its contents.
 

mv2devnull

Golden Member
Apr 13, 2010
1,519
154
106
Does this "PS" (I assume it is not PostScript?) have a concept of "current working directory"? If yes, then is should probably support relative paths as well:
Code:
cd c:\LEV1
move-item -path LEV2 ...
 

Doppel

Lifer
Feb 5, 2011
13,306
3
0
Hi mv2devnull, I meant PowerShell. I don't believe it has the concept of a current working directory. At least for the move-item command it doesn't...
 

MerlinRML

Senior member
Sep 9, 2005
207
0
71
Your access denied error is probably not related to the move-item command and more to do with your environment.

Try running the powershell script as Administrator you're running the script directly, or if you're running from a command prompt make sure that the command prompt is running as administrator before you launch powershell to run the script.
 

Doppel

Lifer
Feb 5, 2011
13,306
3
0
Your access denied error is probably not related to the move-item command and more to do with your environment.

Try running the powershell script as Administrator you're running the script directly, or if you're running from a command prompt make sure that the command prompt is running as administrator before you launch powershell to run the script.
I tried that :(I can run it with c:\LEV1 and no issue and immediately after if I try c:\LEV1\LEV2 it fails me out.

EDIT: Agh, it's an annoying permissions thing. I just ran it with the c:\lev1\lev2 and it worked fine, then ran it again with that directory open in windows explorer and it failed; guessing the OS still thinks some other process has that folder...Yep, this is it, I need to close out explorer for it to totally release that folder.

Thanks for the help!
 
Last edited:

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
I tried that :(I can run it with c:\LEV1 and no issue and immediately after if I try c:\LEV1\LEV2 it fails me out.

EDIT: Agh, it's an annoying permissions thing. I just ran it with the c:\lev1\lev2 and it worked fine, then ran it again with that directory open in windows explorer and it failed; guessing the OS still thinks some other process has that folder...Yep, this is it, I need to close out explorer for it to totally release that folder.

Thanks for the help!

Yeah, Explorer can be a real pain in the ass in that regard.