Move files and add datetime stamp to filename

  • Trying to move files and add the datetime stamp before the extension. I only want to copy files with a .csv extension, no other directories

    or sub folders. This runs but doesn't move anything. I would like to also move files where files have data, no zero byte files get moved.

    Thanks.

    Get-ChildItem -Path 'C:\Line_Dumps\' -Include *.csv | ForEach-Object {
    move-Item -Path $_.FullName -Destination " c:\sqlbackups\$($_.BaseName,(Get-Date).ToString("MMddyyyyHH"),$_.Extension)"
    }
  • This forum, being called "SQL Server Central", is mostly focused around SQL Server.  The Powershell component of the forum is, for the most part, how to do stuff with SQL Server inside Powershell or Powershell inside SQL Server.

    Now, that being said, I see that the folder you are trying to have as a destination is called "c:\sqlbackups", so it does fall on a SQL Server related question, but not sure why .csv files would be moved to sqlbackup folder.

    But, since your "get-childitem" call doesn't include recurisve, I think you could probably simplify the Move-Item call to be something like (note this is partially untested... I tested it on my system and it moved CSV files as you had wanted):

    get-childitem -path 'C:\Line_Dumps\ -Filter *.csv | ForEach-Object { 
    Move-Item -path $_.FullName -Destination ("C:\sqlbackups\"+$_.BaseName+((Get-Date).ToString("MMddyyyyHH"))+$_.Extension)
    }

    That should cover your first requirement - moves any file with a .CSV extension to the destination with the file name of <filename><current date>.<extension>.

    Now for the second part, not moving empty files, that is simply adding an IF statement in to see if the file size is greater than 0 (zero).  So you end up with something like this:

    get-childitem -path 'C:\Line_Dumps\' -Filter *.csv | ForEach-Object { 
    if ($_.Length -gt 0) {
    Move-Item -path $_.FullName -Destination ("C:\sqlbackups\"+$_.BaseName+((Get-Date).ToString("MMddyyyyHH"))+$_.Extension)
    }
    }

     

    Do those scripts make sense to you?  If not, feel free to ask any questions and I'll try to help!

     

    EDIT - the reason that I prefer "Filter" over "Include" is just how the two commands work.  Include needs the file list to be in the path (ie it generally needs to have a * on the end of the path to get results).  To quote the documentation on it "When using the -Include parameter, if you don't include an asterisk in the path the command returns no output".   INCLUDE is useful if you need to have multiple filters on it.  Like if you wanted to include all of the csv files that start with test.  You could do

    get-childitem -path ".\*.csv" -Include "test*"

    Not the best example as you could also do that as:

    Get-ChildItem -path ".\test*.csv"

    but you get the idea.  The FILTER argument works with or without a file list, so to me is more versatile and easier to work with.

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • Adding the filter for file size should be straight forward, just pipe it through a Where-Object before the ForEach

     

    Get-ChildItem "C:\Temp\*.csv" | Where-Object {$_.Length -gt 0}
  • Thanks for replies solution worked great to move files.

     

    Thanks again.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply