locked
Newbie - Simple Delete and Replace Script RRS feed

  • Question

  • This should be so simple and yet - somehow - sigh...I am trying to write a very simple script to delete the contents of one folder (including sub-directories) and copy the contents of another folder (with sub-directories) into the original folder. The contents delete fine but then I get a "Copy-Item" 0 KB file in the target directory. Where am I going wrong? Below is the script:

    set folder="C:\WindowsSigPad\WebContent"
    cd /d %folder%
    for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
    PS C:\> Copy-Item "D:\WebContent95" -Destination "C:\WindowsSigPad\WebContent" -Recurse

    I used to enjoy writing batch files. This is not That! Thank You for your Grace toward a newbie!

    Monday, May 14, 2018 5:03 PM

All replies

  • This is the Powershell forum. Your code is only partly Powershell. I recommend to use pure Powershell syntax and not to use aliasses. On top of that you should format your code as code here in the forum.

    To delete the files and subdirectories of a folder you can use

    Remove-Item -Path <Path to your directory> -Filter * -Recurse -Force

    As you can see, if you use pure Powershell syntax it's actually self explaining and self documenting.

    The desitnation folder you want to copy your files or directories to should already exist.


    Best regards,

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Monday, May 14, 2018 5:20 PM
    Monday, May 14, 2018 5:20 PM
  • This should be so simple and yet - somehow - sigh...I am trying to write a very simple script to delete the contents of one folder (including sub-directories) and copy the contents of another folder (with sub-directories) into the original folder. The contents delete fine but then I get a "Copy-Item" 0 KB file in the target directory. Where am I going wrong? Below is the script:

    set folder="C:\WindowsSigPad\WebContent"
    cd /d %folder%
    for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
    PS C:\> Copy-Item "D:\WebContent95" -Destination "C:\WindowsSigPad\WebContent" -Recurse

    I used to enjoy writing batch files. This is not That! Thank You for your Grace toward a newbie!

          Hello RMWilkinson,

          Yo can do it in many ways.

          I show you one simple script that you can store as a .ps1 (the powershell script extension), but you can   do it with windows commands because it is powershell compatible.

    $directoryToDeleteContent="C:\WindowsSigPad\WebContent"
    $directoryFrom="D:\WebContent95"
    $directoryTo=$directoryToDeleteContent
    
    get-childItem -Path $directoryToDeleteContent | Remove-Item -Recurse
    get-childitem -path $directoryFrom | copy-item -Recurse -Destination $directoryTo

    If you have any question, do not hesitate to ask me.

    Regards.

    Octavio.


         


    Monday, May 14, 2018 8:06 PM