locked
Move folders and all contents from one location to another RRS feed

  • Question

  • I have a .CSV that has a list of Full folder paths. I need to move every folder in this list into an Archive folder and am having trouble with working out the script. The CSV has 1 column in it in the following format

    \\dfs_root\dfs_share\top_level_folder\second_level_folder\third_level_folder\user_folder

    The dfs_root, dfs_share, top_level_folder, and second_level_folder stays the same for all entries, the third_level_folder and user_folder changes.  The goal is to move the user_folders from their current location to another location.

     My current script I am trying to modify looks like this

    Get-Content \\path to .csv file|
     Foreach-Object {
                     move-item -destination \\destination path -WhatIf
             }

    So, I realize that the "move-item" is lacking, that is the part I am having problems with.  I know that it should look more like

    move-item -path "E:\dir1\$_" -destination "E:\dir2\$_" 

    However since the .CSV already has the entire source path, I am not sure how to put it in the script.  The script already has the source path in the imported .CSV.

    Tuesday, October 23, 2018 3:03 PM

All replies

  • Move-Item takes two required values.

    help move-item -online

    You do not have a CSV.  A CSV is not defined by the extension but by the contents.

    When using a pipeline with ForEach-Object you need to specify the pipeline variable to access the object in the pipeline.


    \_(ツ)_/

    Tuesday, October 23, 2018 3:15 PM
  • JRV, I do not understand what you are getting at when you say I do not have a CSV.  When I run the first line by itself, I get the expected results.  You are not being helpful.  I remember you from the last time I came here and asked a script question and you were rather rude and gave me incorrect information.  Please allow someone else to respond, someone that might be able to actually help me out in how to put this script into proper syntax.
    Tuesday, October 23, 2018 3:44 PM
  • A CSV file is loaded with "Import-Csv".  "Get-Content" reads a text file line by line.  These are two very critical and basic things about computers and PowerShell.  You need to stop, go back and learn the basics.

    "ForEach-Object" is a pipeline command.  It requires the use of the pipeline variable to work correctly.  Again, very basic PowerShell that you would learn in lesson one.  Go back and learn basic PowerShell to save yourself a lot of headaches.

    help import-csv -online
    help Get-Content -online
    help Foreach-Object -online.

    You need to reference the pipeline variable for a loop to work.  The values do not just magically appear where they are needed.

    A short form that does not require explicit use of the pipeline variable would be:

    Get-Content'\\path to textfile' | move-item -destination \\destination -WhatIf


    \_(ツ)_/

    Tuesday, October 23, 2018 4:03 PM
  • what if you try 

    Gc C:\path.csv|Foreach {Move-Item  $_ D:\destination -WhatIf}

    Tuesday, October 23, 2018 5:20 PM
  • JRV,

    I am sick and tired of your condescending, rude comments. I am sure that you are some sort of Powershell god in your world, that does not give you the right to come here and talk to me as though I am some idiot without any knowledge of PowerShell, or how it works.

    When I search these forums and I see others ask questions, request help with their scripts, I do not see others give condescending answers telling them that the answer is basic, and tell them to go back and learn basic, I see helpful answers and suggested changes to their script.

    While not a master at scripting as you so obviously are, I do understand the basics.  I manage an organization with over 4000 users and have written well over 100 scripts that work perfectly fine.  In the last couple of years, I have found need to come here and ask a question 3 or 4 times, usually I can find the answer by reading help files or searching TechNet to find others that have already solved the very problem I am facing.  I can see where Import here may actually work better than Get-Content, however as you point out, Get-Content reads a text file line by line, and that is what the first line of my code does.  It reads each line of the CSV file, which is the full path to a folder that I want moved.

    In any case, I have changed from looking at trying to use Get-Content to work and changed to importing, I also did away with Foreach-Object and simply went with for each.  The script now looks something like this

    $file1 = import-csv \\path\Test_move.csv | 
    foreach ($line in $file1)
             {
                     move-item -path "\\dfs\share_root\level_1\level_2\level_3" -destination "\\destination path"
             }
    

    This works, to an extent.  I may not be able to do what I really want to do though.  in the .CSV, there are 2 things that change, obviously the folder names themselves, but also the "level_3" changes.  There are 26 folders at that level, one for each letter in the alphabet.  I have over 5000 folders that need to be moved spread throughout all of the 26 folders.  If I have to, I can run this script 26 times, once for each folder, but I was hoping there was a way I could simply have 1 script do it all.  Any suggestions from anyone?
    Tuesday, October 23, 2018 5:44 PM
  • Your code still doesn't work as expected because you are never accessing the variable so the code will move every file in the source folder.

    I am trying to get you to take some time to actually learn the basics of PowerShell.  You constantly fail to understand the simplest things that any new user whom takes a good tutorial would know in the first day or two of using PowerShell.  Getting mad because you are not getting an answer you can understand will not help you to learn PowerShell.

    Take the time to learn the basics. Learn PowerShell  

    You will find that this is easier than you think but you will never see that without putting is some effort to learn the technology.

    You cannot understand the answers if you do not understand the technology.


    \_(ツ)_/


    • Edited by jrv Tuesday, October 23, 2018 5:52 PM
    Tuesday, October 23, 2018 5:51 PM
  • Jiten SH,
    Unfortunately that does not work.  the Move-Item requires the -Path parameter, leaving it out simply prompts for the -path parameter to be filled in during the running of the script if it is not there.  I was trying to avoid having to do it this way because I have 5k folders spread through 26 different folders that I am trying to move into a single archive.  I think I am going to have to create a baseline script and create 26 separate scripts, one for each folder containing some of the 5k folders I need moved, and run each script separately.  A little more work, but still a lot less than trying to manually move over 5k folders.

    Tuesday, October 23, 2018 5:56 PM
  • You have to think about what is happening.  The path parameter takes a file path name.  This is a formally defined item in Windows.  It is referred to as "FilePath".  If you read the documentation for "Move-Item" it will tell you what this parameter is for.  Why guess incorrectly as you have. Look up the definition.

    PS D:\scripts> help move-item -Parameter path
    
    -Path <String[]>
        Specifies the path to the current location of the items. The default is the current directory. Wildcards are permitted.
    
        Required?                    true
        Position?                    0
        Default value                None
        Accept pipeline input?       True (ByPropertyName, ByValue)
        Accept wildcard characters?  false
    
    

    Note that it is a [string[]] which also gives us another flavor of a batch move.

    $files = Get-Content \\path\Test_move.csv
    Move-Item -Path $files -Destination \\computer\share\folder

    As noted above, adding CSV to the end of a file name does not make it a CSV file.

    https://en.wikipedia.org/wiki/Comma-separated_values

    help import-csv -online

    Read the help and background on these things to learn how and why you are doing it wrong.


    \_(ツ)_/

    Tuesday, October 23, 2018 6:05 PM
  • Hmm, It was working for me cos I used <g class="gr_ gr_64 gr-alert gr_tiny gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="64" id="64">$</g>_ parameter as a source.

    @JRV Nobody gives a shit, you keep saying, people, Learn Powershell basic. I believe OP knows more than basic.

    Tuesday, October 23, 2018 7:32 PM
  • You have to think about what is happening.  The path parameter takes a file path name.  This is a formally defined item in Windows.  It is referred to as "FilePath".  If you read the documentation for "Move-Item" it will tell you what this parameter is for.  Why guess incorrectly as you have. Look up the definition.

    PS D:\scripts> help move-item -Parameter path
    
    -Path <String[]>
        Specifies the path to the current location of the items. The default is the current directory. Wildcards are permitted.
    
        Required?                    true
        Position?                    0
        Default value                None
        Accept pipeline input?       True (ByPropertyName, ByValue)
        Accept wildcard characters?  false
    

    Note that it is a [string[]] which also gives us another flavor of a batch move.

    $files = Get-Content \\path\Test_move.csv
    Move-Item -Path $files -Destination \\computer\share\folder

    As noted above, adding CSV to the end of a file name does not make it a CSV file.

    https://en.wikipedia.org/wiki/Comma-separated_values

    help import-csv -online

    Read the help and background on these things to learn how and why you are doing it wrong.


    \_(ツ)_/


    @JRV  I understand what move-item is doing and what it does.  I was asking for help on how to get around having to use a single path as the source.  Like I said above, I am not some newbie that opened powershell yesterday and think I can write master scripts.  I also understand .CSV\.TXT files, I have been using them to manipulate data and compare data for a very long time now.  I am using a .CSV because that is the product I end up producing through 3 other scripts to get to this point.  The .CSV I am producing gets modified and manipulated for several other functions, just so happens that for this function it is stripped of everything but a single "column".  For what I have to do to modify and manipulate data, a .CSV serves my purpose much better than a .TXT, granted, once I produce the final product I could easily change it to a .TXT, but why?  They both would serve the exact same purpose.

    You tell me to go learn basic PowerShell to save myself a lot of headaches, yet in 5 years and well over 100 scripts, I have been completely stumped and come here asking for help 3, maybe 4 times.  Yes, a lot of headaches indeed.....

    Interestingly enough, you did end up giving me the answer I needed.  I was mistakenly using the "for each" when it was not needed at all.  I had tried the code you put in, but with the for each and it would fail, it was adding my user folder prior to the path in the .csv (c:\user\username\@\\folderpath.....).  I tried your code above removing the for-each and it works beautifully.  For that, Thanks! 

    I am not a PowerShell guru and most likely never will, I am a jack of all where I am, and PowerShell falls much lower on the list of demands on my time.  With that said, I am not ignorant to PowerShell either, I do have a grasp of the fundamentals and a few more advanced functions.
    Tuesday, October 23, 2018 7:58 PM
  • @Jiten SH

    so what you put there is definitely above my level.  You are correct that I know more than the basics in PowerShell, just not what you put there.  Your original suggestion wasn't working because the -Path is a requirement, so by omitting it, it simply asked me for the parameter once I ran the script.  I needed a way for -Path to be filled by the lines of information in my .CSV.  JRV did end up helping me and gave me what works for what I need.  I was almost there, but I was still using foreach which was messing the whole thing up.  Once I removed foreach, it worked perfectly.  1 single script to move everything into an archive folder.
    Tuesday, October 23, 2018 8:08 PM
  • Hmm, It was working for me cos I used <g class="gr_ gr_64 gr-alert gr_tiny gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="64" id="64">$</g>_ parameter as a source.

    @JRV Nobody gives a shit, you keep saying, people, Learn Powershell basic. I believe OP knows more than basic.

    Fix your post it is unreadable.

    It does not work as required because the variable is never used for the path.

    No profanity please.


    \_(ツ)_/


    • Edited by jrv Tuesday, October 23, 2018 8:26 PM
    Tuesday, October 23, 2018 8:25 PM
  • I post to learn PowerShell because your posts show you do not understand the most basic things.  If you want to just keep guessing then that us your business.  I can give you answers and pointers but I cannot make you understand what you are not prepared to understand.

    Knowing something about PowerShell from copying and pasting is not "knowing".   Knowing the basics includes knowing how to read the help and how to use the syntax elements.  If you do not understand the syntax then you do not know the basics.

    Knowing PowerShell at an advanced level requires using the syntax correctly long enough to fully understand how to design and write programs in PowerShell.  Without the basics you will never move to an advanced level and you will have issues with understanding answers to questions.  When you finally learn the basics you will understand why I am pushing this.

    Don't waste time guessing and do the tutorial until you understand what it is trying to teach you.  You can ask questions here about things that you have difficulty with in the tutorial.


    \_(ツ)_/

    Tuesday, October 23, 2018 8:34 PM
  • This is exactly why I do not post on these forums for help, because of jrv's very bad attitude.

    Other forums, such as Experts Exchange, have taught me a lot with scripting. Great guys over there. You may want to re-post on there.

    All the best.

    Wednesday, October 24, 2018 10:08 AM
  • I post to learn PowerShell because your posts show you do not understand the most basic things.  If you want to just keep guessing then that us your business.  I can give you answers and pointers but I cannot make you understand what you are not prepared to understand.

    Knowing something about PowerShell from copying and pasting is not "knowing".   Knowing the basics includes knowing how to read the help and how to use the syntax elements.  If you do not understand the syntax then you do not know the basics.

    Knowing PowerShell at an advanced level requires using the syntax correctly long enough to fully understand how to design and write programs in PowerShell.  Without the basics you will never move to an advanced level and you will have issues with understanding answers to questions.  When you finally learn the basics you will understand why I am pushing this.

    Don't waste time guessing and do the tutorial until you understand what it is trying to teach you.  You can ask questions here about things that you have difficulty with in the tutorial.


    \_(ツ)_/

    @JRV

    You know nothing of my skill, ability, or knowledge.  I do not "know" things simply by copy\pasting from other sources.  I do not simply come here and ask others to write a script for me.  I have put together plenty of scripts without having to ask anyone.  I research and learn the pieces I need to do what I need to do.  As I said, I am more like a Jack of all trades where I work.  I work on networks that are not connected to the internet, therefore online help files are not always an option while I am building a script.  You come here with some sort of preconceived notion of people because of the questions they are asking and instead of actually trying to help them, you just insult their intelligence and tell them to go read help files or take tutorials.  If that is the case, why even have this forum?  Lets just delete all the forums, all the questions and answers here and simply put a few links here to some help files and tutorials.  Let everyone figure it out all for themselves.  Let's have everyone reinvent the wheel by themselves.

    Please stop responding to my posts\questions unless you learn how to be constructive and helpful.  You say learn basic and I will have less headaches, yet you and your rude condescending attitude has caused me way more irritation, aggravation and stress than any script ever has.  I know what I need to know to perform my job.  I rarely come up against something that I cannot fairly easily write a script to do.  I am constantly trying to learn new things, however advancing what I know about PS is not at the top of the list of things I research and learn, or responsibilities I must keep up with.  I do not need your condescending attitude on the rare occasions that I reach out here for help.  I do not need you to tell me to go learn this or that.  I have never came here and asked for help without first searching\researching ways to do it and trying to understand the code or cmdlets needed to accomplish the tasks I need to complete.


    Edit:  For those that would question that I work on networks that do not connect to the internet, so how do I post here, I have a network that I use for internet access, or I post from my home computer after hours.
    Wednesday, October 24, 2018 3:01 PM
  • I don't understand why you are so upset.  I am trying to point you in a direction that will help you gain a correct understanding of the technology.

    I know what your skill, knowledge and experience are because I can see that code you have posted and I have read your constant complaints in defense of what that code tells us.

    Stop complaining about me and use the time to really learn this technology correctly.  Until then you will not understand what it is you are doing wrong.

    Yes you have accumulated a lot of ideas from copying what others have done but you have also missed most of the critical basic concepts and methods because you choose to learn by accident rather than by intent. This leads to a lot of incorrect ideas but by relearning formally what you have already experienced will be clarified and you will learn in depth.

    Ask yourself this: How can anyone know any programming language and not know how to use a simple loop syntax? Think about that then ask your self how many more basic ideas are you missing or understanding incorrectly.

    Fix this and the rest will begin to become clear.  If you think you already know all of the answers then ignore this and stumble on in your own way.

    Anyway - have fun with whatever you do.  My intent is not criticism but is to coax you to try to learn this technology the way any professional engineer would - by studying.


    \_(ツ)_/

    Wednesday, October 24, 2018 7:58 PM