Add Date to multiple files in same folder

Welcome Forums General PowerShell Q&A Add Date to multiple files in same folder

Viewing 20 reply threads
  • Author
    Posts
    • #207879
      Participant
      Topics: 1
      Replies: 9
      Points: 39
      Rank: Member

      Hi all,

      I am trying to add the Date field to multiple files in one specific folder.

      Alle file names are in the same folder and are set up like: Name.docx

      I now want to add the Date to all of them:

      01022017_Name.docx

      I do not want the Creation date, nor Last modified, also not the date of today, I need the Date field of Windows. See the link below for a print screen of my File Explorer folder. As you can see, only the Date column has relevant information.

      https://imgur.com/tMsgLxJ

      Which code should I use?

      Thanks in advance!

       

       

      • This topic was modified 2 months, 4 weeks ago by zzzipke69.
      • This topic was modified 2 months, 4 weeks ago by zzzipke69.
    • #207987
      Participant
      Topics: 5
      Replies: 322
      Points: 445
      Helping Hand
      Rank: Contributor
      $file = Get-ChildItem -Path \\path\to\files -Filter *.docx
      foreach ($f in $file){
          $new = "{0}_{1}" -f (Get-date -Date $f.CreationTime -Format ddmmyyyy),$f.Name
          Rename-Item -Path $f.FullName -NewName $new -WhatIf
      }
      
    • #208362
      Participant
      Topics: 1
      Replies: 9
      Points: 39
      Rank: Member

      Hi,

      Thanks for your response. It is the first time ever I am using PowerShell so I am not sure how to copy your code.

      I just opened PowerShell and I pasted your code with my folder structure. Now I get the error: Get-ChildItem : A positional parameter cannot be found that accepts argument ‘batch\Documents’.

      This is the code I am using. What am I doing wrong?

      PS C:\Users\myname> $file = Get-ChildItem -Path C:\Users\myname\Documents\Clients\Client\project name\Documents received\foldername -Filter *.docx
      foreach ($f in $file){
      $new = “{0}_{1}” -f (Get-date -Date $f.CreationTime -Format ddmmyyyy),$f.Name
      Rename-Item -Path $f.FullName -NewName $new -WhatIf
      }
      • This reply was modified 2 months, 4 weeks ago by zzzipke69.
    • #208479
      Moderator
      Topics: 3
      Replies: 218
      Points: 1,188
      Helping Hand
      Rank: Community Hero

      Since there are spaces in between the path, you need to wrap them with single or double quotes…

      $file = Get-ChildItem -Path 'C:\Users\myname\Documents\Clients\Client\project name\Documents received\foldername' -Filter *.docx
      foreach ($f in $file)
      {
          $new = “{0}_{1}” -f (Get-date -Date $f.CreationTime -Format ddmmyyyy), $f.Name
          Rename-Item -Path $f.FullName -NewName $new -WhatIf
      }
      Since you are a beginner, please start with some basic PowerShell courses.
      Thank you.
    • #208779
      Participant
      Topics: 1
      Replies: 9
      Points: 39
      Rank: Member

      Hi,

      Thanks for your response. I am a beginner indeed. I only need to solve this hurdle for work, real quick. Unfortunately, I do not have the time to take multiple courses on how to code in PowerShell. I was hoping this forum was the location to ask for help in writing the correct code.

      When I run your code, it runs, but nothing happens. I get a list of What ifs:

      
      What if: Performing the operation “Rename File” on target “Item: C:\Users\myname\Documents\Clients\Client\project name\Documents received\foldername\(PKE7CG ajoarvot 2160) .docx Destination: C:\Users\myname\Documents\Clients\Client\project name\Documents received\foldername\04472020_(PKE7CG ajoarvot 2160) .docx”.
      
      

      It does this for every line, but it does not change anything to the actual document name in my folder.

      Also: it added 04472020, which is not a date and not the date I need.

      Thanks!

    • #208785
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      You will have to learn at least a little bit about the cmdlets you’re about to use. You should never ever use code you don’t fully understand in a productive environment.

      Please read at least the help for the cmdlet Rename-Item … especially the parameter -WhatIf.

    • #208794
      Participant
      Topics: 1
      Replies: 9
      Points: 39
      Rank: Member

      Thanks for the link to the Whatif section. Now I see that the code would give me a wrong output.

      How can I get the date of Windows instead of the 04472020?

       

      I already read a lot on the NewName code before posting my question here. Unfortunately everything I could find online was linked to replacements and creation dates, never the “Date” field in Windows.

    • #208806
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      How can I get the date of Windows instead of the 04472020?

      … never the “Date” field in Windows.

      What do you mean with “date of Windows” and “Date field in Windows”?

      Please keep in mind that we cannot see your screen and we cannot read your mind. You should be as explicit as possible when you ask questions in a technical forum. Try do provide every single piece of information for the helper to understand what you’re talking about. We’re all on different systems and in different environments. So you should not think of anything to be logic or given or set by default. 😉

    • #208860
      Participant
      Topics: 1
      Replies: 9
      Points: 39
      Rank: Member

      I tried to do just that… I posted an image in my opening post to make it as clear as possible.

      In windows you have different columns, creation date of the file, last modified date and apparently also a column named “Date” (see my print screen on the link in the opening post).

      This is the date I need. It is the creation date of the file by the content creator. I just copied all the documents in my own folder, so now creation date and last modified changed to the 4th of March when I did this action. Apparently the Date field still holds the information I need. I do not know what the technical name of this field is, to request it via PowerShell.

      • This reply was modified 2 months, 3 weeks ago by zzzipke69.
    • #208914
      Participant
      Topics: 1
      Replies: 64
      Points: 363
      Helping Hand
      Rank: Contributor

      Easiest way to see what you have to choose from

      Get-ChildItem .\LAW.txt |get-member

      That will return a list of properties, so for example

      Get-ChildItem .\LAW.txt |select-object -expand LastAccessTime
      Monday, March 2, 2020 10:11:51 AM

      You’ll see various properties that respond to an event on the file, you’ll need to go through them and see what matches the time you’re looking for.

    • #208926
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      The easiest way to get all possible times Powershell returns for a file would be something like this:

      Get-ChildItem | 
          Select-Object -Property Name,*time* | 
              Format-Table -AutoSize

      Now you can compare that to the time in your Windows (I’d recommend to name it “Windows Explorer” next time 😉 ) and choose what you need.

    • #209250
      Participant
      Topics: 1
      Replies: 9
      Points: 39
      Rank: Member

      Mmmhh damn.. I ran your code and turns out PowerShell does not find the “Date” field in Windows Explorer. For every possible date I get the 4th of March. You can find a print screen here: https://imgur.com/oVR0DQ1

      The 4th of March was my creation date in Windows Explorer (see my opening post).

      Any other suggestions?

    • #209265
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      Hmmm … if I run this command I get actually 6 different time stamps …
      CreationTime
      CreationTimeUtc
      LastAccessTime
      LastAccessTimeUtc
      LastWriteTime
      LastWriteTimeUtc

      The “Date” column in my Windows Explorer is equal to the “LastWriteTime”.

      Try this:

      Get-ChildItem | 
          Select-Object -Property Name,*time* | 
              Format-List
    • #209271
      Participant
      Topics: 1
      Replies: 9
      Points: 39
      Rank: Member

      https://imgur.com/w9R8D8h

      This is depressing 🙁

      • This reply was modified 2 months, 3 weeks ago by zzzipke69.
    • #209373
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      Despite the fact that you’re not showing the same files you might have an issue with your Windows system. I don’t think that’s a Powershell problem. Can you reproduce the effect with another computer and maybe with other file types?

    • #209859
      Participant
      Topics: 1
      Replies: 9
      Points: 39
      Rank: Member

      Ok, I just ran it again.

      Windows: https://imgur.com/MLcX6Vr

      PowerShell: https://imgur.com/gu5lVsF

      How can it be an issue with Windows? My Windows Explorer shows the exact Date, right? But it looks like the “Date”-field in Windows Explorer is not one of your date fields below? – I don’t know how I can figure out what the field name is of the Date-field.
      CreationTime
      CreationTimeUtc
      LastAccessTime
      LastAccessTimeUtc
      LastWriteTime
      LastWriteTimeUtc

      • This reply was modified 2 months, 3 weeks ago by zzzipke69.
    • #209886
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      How can it be an issue with Windows?

      I don’t know.

      My Windows Explorer shows the exact Date, right?

      I don’t know.

      But it looks like the “Date”-field in Windows Explorer is not one of your date fields below? – I don’t know how I can figure out what the field name is of the Date-field.

      1. Are you sure these files are 2 or 3 years old?
      2. Can you reproduce this behaviour on another computer? (I can’t)
      3. What date would you like to have in your file names?

    • #209889
      Participant
      Topics: 1
      Replies: 9
      Points: 39
      Rank: Member

      Yes, the creation date of these files is 2-3 years ago. I will prove it to you. You saw the Date-field in Windows explorer, it says 8/2/2017 for the first file. When I open this first file, you see the date: https://imgur.com/mcJ5Ik7 (this is a print screen of the info section of the Word-document)

      The date I want in my file name is THIS date, 8/2/2017.
      So I want a file name something like: 20170208_(PKE7CG ajoarvot 2160).docx

      Somehow this date is still stored in Windows Explorer in the Date-field. Unfortunately I do not know the technical name of this field to retrieve it in PowerShell. When I run PowerShell with your code, the only dates I get are the dates of when I downloaded the documents to my computer (3/4/2020 – as you see in every print screen I posted in older posts).

      • This reply was modified 2 months, 3 weeks ago by zzzipke69.
    • #209898
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      Somehow this date is still stored in Windows Explorer in the Date-field.

      Could you try to copy this file to a computer where no Microsoft Word is installed and try it there again. I could imagine that the explorer reads the metadata from inside the word file. That shouldn’t be possible where word isn’t installed. The bad news would be that you’d need to read the Word internatl metadata to use this date.

    • #209904
      Participant
      Topics: 1
      Replies: 9
      Points: 39
      Rank: Member

      Finding a computer with no Word installed? Wut? 😀
      Both my work computer and my private laptop have Word installed. If I ask a friend who has a Mac computer, I don’t know if they have access to PowerShell…

      I think it’s going to be easier and faster to just adjust all of the files manually. I don’t see the ending of this story anymore … 🙁

      Thanks for your time though Olaf .. Appreciate the effort!

      • This reply was modified 2 months, 3 weeks ago by zzzipke69.
    • #209925
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP
Viewing 20 reply threads
  • You must be logged in to reply to this topic.