locked
Rename files in batch RRS feed

  • Question

  • Good afternoon

    I currently have batches of files which have been scanned into our systems and are held in a central location, they already have the filename with the current date prefixed, however i was wondering if possible i could add the creationtime (just the time bit) added to the filename at this point before it gets processed 

    Currently this is the scenario

    Item scanned on system as PDF document titled     scanner1-19-07-2013(1).pdf / scanner1-19-07-2013(2).pdf, etc

    (The titles are auto generated by our scanning system but unfortunately the time is not something which can be prefixed)

    These are saved to a temporary location, i.e 'c:\temp'

    At this point i wish powershell to add the creation 'time' to the filenames as after this the files are put through our processing system

    The aim is for me to be able to report on the 'Time' the files were scanned / reached the c:\temp folder for reporting purposes

    i.e how long has the document taken from being scanned to reaching the final destination?

    I hope this makes sense

    any Help / advice much appreciated

    Regards

    Barrie

    Friday, July 19, 2013 2:41 PM

Answers

  • If I understand you correctly, you can try something like this:

    Get-Item 'C:\Temp\*.pdf' | ForEach-Object {
    	$creationTime = '{0:M-dd-yyyy_hh.mm.ss}' -f $_.CreationTime
    	Rename-Item -Path $_.FullName "$($_.Name)$creationTime.$($_.Extension)"
    }

    That should loop through the C:\temp for and grab all pdf files only, then renames the file to be scanner7-19-2013_11.10.22.pdf. YOu will need to play around with the date format to be what you want it to be:

    $creationTime = '{0:M-dd-yyyy_hh.mm.ss}'

    Changing M-dd-yyyy_hh.mm.ss

    You can view this page for vaild date formatters Date Formatters

    Also remember when choosing a seperator between month, day, year, hour, minutes, and seconds, you can NOT use the following characters \ / : * ? " < > | as those are invalid characters for a file name.


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Don't Retire Technet



    • Proposed as answer by Mike Laughlin Friday, July 19, 2013 3:42 PM
    • Edited by clayman2 Monday, July 22, 2013 12:37 PM edit script
    • Marked as answer by ManchesterBazza Monday, July 22, 2013 1:52 PM
    Friday, July 19, 2013 3:16 PM

All replies

  • If I understand you correctly, you can try something like this:

    Get-Item 'C:\Temp\*.pdf' | ForEach-Object {
    	$creationTime = '{0:M-dd-yyyy_hh.mm.ss}' -f $_.CreationTime
    	Rename-Item -Path $_.FullName "$($_.Name)$creationTime.$($_.Extension)"
    }

    That should loop through the C:\temp for and grab all pdf files only, then renames the file to be scanner7-19-2013_11.10.22.pdf. YOu will need to play around with the date format to be what you want it to be:

    $creationTime = '{0:M-dd-yyyy_hh.mm.ss}'

    Changing M-dd-yyyy_hh.mm.ss

    You can view this page for vaild date formatters Date Formatters

    Also remember when choosing a seperator between month, day, year, hour, minutes, and seconds, you can NOT use the following characters \ / : * ? " < > | as those are invalid characters for a file name.


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Don't Retire Technet



    • Proposed as answer by Mike Laughlin Friday, July 19, 2013 3:42 PM
    • Edited by clayman2 Monday, July 22, 2013 12:37 PM edit script
    • Marked as answer by ManchesterBazza Monday, July 22, 2013 1:52 PM
    Friday, July 19, 2013 3:16 PM
  • Hi

    Thanks for this, will definitely give this a go, will update on Monday as i am away from

    have a good weekend

    Regards

    Barrie

    Friday, July 19, 2013 3:38 PM
  • You can simplify that to just:

    Get-Item 'C:\Temp\*.pdf' |
    Rename-Item -NewName {"scanner$('{0:M-dd-yyyy_hh.mm.ss}' -f $_.CreationTime).$($_.Extension)"}
    The -NewName parameter is pipeline bound and since its type isn't scriptblock, you can use a scriptblock to compute its value.  Inside that scriptblock you have access to the pipeline object $_.

    Friday, July 19, 2013 3:51 PM
  • You might also want to make sure you're only renaming files that match the file name format you've specified (in case there are other PDFs in the folder that you didn't intend to modify), and make sure no file with the target name already exists (in which case Rename-Item would fail):

    $directory = "C:\Temp"
    $pattern = '^.*-\d{2}-\d{2}-\d{4}(?:\(.*\))?\.pdf'
    
    Get-Item -Path (Join-Path -Path $directory -ChildPath "*.pdf") |
    ForEach-Object {
        if ($_.Name -match $pattern) {
    	$creationTime = '{0:M-dd-yyyy_hh.mm.ss}' -f $_.CreationTime
            
            $i = 1
            $fileName = "scanner$creationTime($i)$($_.Extension)"
    
            while ((Test-Path -Path (Join-Path -Path $directory -ChildPath $fileName)) -eq $true) {
                $i++
                $fileName = "scanner$creationTime($i)$($_.Extension)"
            }
    
            Rename-Item -Path $_.FullName -NewName $fileName
        }
    }

    Friday, July 19, 2013 5:36 PM
  • Good Morning

    I have had a go at this and noticed as you mentioned earlier that i would need to check the format of the names, unfortunately although your script works (the first one) i need to keep the current name exact as it is but just add the creationdate time stamp to it

    I tried to manipulate the script to something like this which obviously doesnt work

    Get-Item 'C:\Scripts\OCR\Batchscanning\testers\*.pdf' | ft name ForEach-Object{$newname = ($_.name + "$('{0:hh.mm.ss}' -f $_.CreationTime).$($_.Extension))}    

    Any help would be greatly appreciated

    regards

    Barrie

    Monday, July 22, 2013 10:44 AM
  • I modified my script above (the one marked as potential answer), give it a try and let me know

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Don't Retire Technet

    Monday, July 22, 2013 12:38 PM
  • Potential Answer = Was the answer

    Many thanks for this and for your help

    Regards

    Barrie

    Monday, July 22, 2013 1:53 PM