PowerShell script to move files with date as name in it to a folder



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I did some basic scripts in the past with powershell but now I'm stuck.



We have users that place PDF files with specific date in the filename:




TSE_M001340_04-Nov-2015_bis_03-Nov-2018.pdf


What I try to accomplish:
look for the date after "bis_" (in this example "03-Nov-2018") and if it is before (so younger) compared to the actual date move it to another folder.



What I tried so far:



$a = dir c:temptest
$a | foreach
$file = $_.Name;
$full = $_.FullName

switch -wildcard ($file)
"*bis_*"
$file
[string]$date = ($file.Split("_"))[2]
$date
$datetime = [DateTime]::ParseExact($date, "yyyyMMdd", $null)
$datetime
if ($datetime -lt (Get-Date))
Move-Item $full "C:temptestcopy"


#end switch
#end foreach


Error:




String was not recognized as a valid DateTime.




Any thoughts?



@Florian:
still getting the error and all files are copied.
this is the output of the variables:



PS C:Temptest> $split
bis_

PS C:Temptest> $File
TSE_M001340_Dolder AG_04-Nov-2015_bis_03-Nov-2018

PS C:Temptest> $date
M001340

PS C:Temptest>
PS C:Temptest>
PS C:Temptest> $datetime

PS C:Temptest>
PS C:Temptest> $full
C:temptestTSE_M001340_AG_04-Nov-2015_bis_03-Nov-2018.pdf

PS C:Temptest>









share|improve this question



















  • 5





    Umm... does 03-Nov-2018 look like it'd be matching the format yyyyMMdd to you?

    – Ansgar Wiechers
    Nov 16 '18 at 11:27


















0















I did some basic scripts in the past with powershell but now I'm stuck.



We have users that place PDF files with specific date in the filename:




TSE_M001340_04-Nov-2015_bis_03-Nov-2018.pdf


What I try to accomplish:
look for the date after "bis_" (in this example "03-Nov-2018") and if it is before (so younger) compared to the actual date move it to another folder.



What I tried so far:



$a = dir c:temptest
$a | foreach
$file = $_.Name;
$full = $_.FullName

switch -wildcard ($file)
"*bis_*"
$file
[string]$date = ($file.Split("_"))[2]
$date
$datetime = [DateTime]::ParseExact($date, "yyyyMMdd", $null)
$datetime
if ($datetime -lt (Get-Date))
Move-Item $full "C:temptestcopy"


#end switch
#end foreach


Error:




String was not recognized as a valid DateTime.




Any thoughts?



@Florian:
still getting the error and all files are copied.
this is the output of the variables:



PS C:Temptest> $split
bis_

PS C:Temptest> $File
TSE_M001340_Dolder AG_04-Nov-2015_bis_03-Nov-2018

PS C:Temptest> $date
M001340

PS C:Temptest>
PS C:Temptest>
PS C:Temptest> $datetime

PS C:Temptest>
PS C:Temptest> $full
C:temptestTSE_M001340_AG_04-Nov-2015_bis_03-Nov-2018.pdf

PS C:Temptest>









share|improve this question



















  • 5





    Umm... does 03-Nov-2018 look like it'd be matching the format yyyyMMdd to you?

    – Ansgar Wiechers
    Nov 16 '18 at 11:27














0












0








0








I did some basic scripts in the past with powershell but now I'm stuck.



We have users that place PDF files with specific date in the filename:




TSE_M001340_04-Nov-2015_bis_03-Nov-2018.pdf


What I try to accomplish:
look for the date after "bis_" (in this example "03-Nov-2018") and if it is before (so younger) compared to the actual date move it to another folder.



What I tried so far:



$a = dir c:temptest
$a | foreach
$file = $_.Name;
$full = $_.FullName

switch -wildcard ($file)
"*bis_*"
$file
[string]$date = ($file.Split("_"))[2]
$date
$datetime = [DateTime]::ParseExact($date, "yyyyMMdd", $null)
$datetime
if ($datetime -lt (Get-Date))
Move-Item $full "C:temptestcopy"


#end switch
#end foreach


Error:




String was not recognized as a valid DateTime.




Any thoughts?



@Florian:
still getting the error and all files are copied.
this is the output of the variables:



PS C:Temptest> $split
bis_

PS C:Temptest> $File
TSE_M001340_Dolder AG_04-Nov-2015_bis_03-Nov-2018

PS C:Temptest> $date
M001340

PS C:Temptest>
PS C:Temptest>
PS C:Temptest> $datetime

PS C:Temptest>
PS C:Temptest> $full
C:temptestTSE_M001340_AG_04-Nov-2015_bis_03-Nov-2018.pdf

PS C:Temptest>









share|improve this question
















I did some basic scripts in the past with powershell but now I'm stuck.



We have users that place PDF files with specific date in the filename:




TSE_M001340_04-Nov-2015_bis_03-Nov-2018.pdf


What I try to accomplish:
look for the date after "bis_" (in this example "03-Nov-2018") and if it is before (so younger) compared to the actual date move it to another folder.



What I tried so far:



$a = dir c:temptest
$a | foreach
$file = $_.Name;
$full = $_.FullName

switch -wildcard ($file)
"*bis_*"
$file
[string]$date = ($file.Split("_"))[2]
$date
$datetime = [DateTime]::ParseExact($date, "yyyyMMdd", $null)
$datetime
if ($datetime -lt (Get-Date))
Move-Item $full "C:temptestcopy"


#end switch
#end foreach


Error:




String was not recognized as a valid DateTime.




Any thoughts?



@Florian:
still getting the error and all files are copied.
this is the output of the variables:



PS C:Temptest> $split
bis_

PS C:Temptest> $File
TSE_M001340_Dolder AG_04-Nov-2015_bis_03-Nov-2018

PS C:Temptest> $date
M001340

PS C:Temptest>
PS C:Temptest>
PS C:Temptest> $datetime

PS C:Temptest>
PS C:Temptest> $full
C:temptestTSE_M001340_AG_04-Nov-2015_bis_03-Nov-2018.pdf

PS C:Temptest>






powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 13:28







Patrick

















asked Nov 16 '18 at 11:25









PatrickPatrick

33




33







  • 5





    Umm... does 03-Nov-2018 look like it'd be matching the format yyyyMMdd to you?

    – Ansgar Wiechers
    Nov 16 '18 at 11:27













  • 5





    Umm... does 03-Nov-2018 look like it'd be matching the format yyyyMMdd to you?

    – Ansgar Wiechers
    Nov 16 '18 at 11:27








5




5





Umm... does 03-Nov-2018 look like it'd be matching the format yyyyMMdd to you?

– Ansgar Wiechers
Nov 16 '18 at 11:27






Umm... does 03-Nov-2018 look like it'd be matching the format yyyyMMdd to you?

– Ansgar Wiechers
Nov 16 '18 at 11:27













2 Answers
2






active

oldest

votes


















0














An approach with a regular expression to grep the date in a where-Object and

a single pipe should be more efficient:



# if the abbreviated month names compply with the current culture
$CI = Get-Culture

# or a selected deviating culture
#$CI = New-Object system.globalization.cultureinfo("de-DE")

$RE = "_bis_(d2-("+($CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|')+")-20d2)"

Get-ChildItem | Where-Object $_.Name -match $RE -and
([datetime]::ParseExact($Matches[1],"dd-MMM-yyyy",$CI) -lt [datetime]::now)|
Move-Item -Destination "C:temptestcopy" -WhatIf


If the output looks OK remove the -WhatIf



To better explain what $CI for culture information returns,



On my German locale



> $CI = Get-Culture
> $CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|'
Jan|Feb|Mrz|Apr|Mai|Jun|Jul|Aug|Sep|Okt|Nov|Dez



> $CI = New-Object system.globalization.cultureinfo("en-US")
> $CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|'
Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec
> $RE = "_bis_(d2-("+($CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|')+")-20d2)"
> $RE
_bis_(d2-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-20d2)


See the RegEx live on regex101.com






share|improve this answer

























  • thanks!! Looks good so far but need to do further testing

    – Patrick
    Nov 16 '18 at 14:33






  • 1





    works! Thanks a lot for your help.

    – Patrick
    Nov 16 '18 at 16:16


















0














As @AnsgarWiechers pointed out, you are not using the correct date format.



MWE



$a = dir c:temptest
$a | foreach
$file = $_.Name;
$full = $_.FullName

switch -wildcard ($file)
"*bis_*"
$file
[string]$date = ($file.Split("_"))[2]
$date
$datetime = [DateTime]::ParseExact($date, "dd-MMM-yyyy", $null)
$datetime
if ($datetime -lt (Get-Date))
Move-Item $full "C:temptestcopy"


#end switch
#end foreach


Edit



It appears that all of your files do not follow the same structure. I take it that you should use the dat after bis_ based on the two provided examples:




  • TSE_M001340_04-Nov-2015_bis_03-Nov-2018.pdf

  • file_test_bis_23-APR-2021.pdf



Here is the updated code:



$a = dir c:temptest
$a | foreach
$file = $_.BaseName;
$full = $_.FullName
$split = "bis_"

switch -wildcard ($file)
"*$split*"
$file
[string]$date = ($file.Split($split))[1]
$date
$datetime = [DateTime]::ParseExact($date, "dd-MMM-yyyy", $null)
$datetime
if ($datetime -lt (Get-Date))
Move-Item $full "C:temptestcopy"


#end switch
#end foreach





share|improve this answer

























  • ok didn't see that...

    – Patrick
    Nov 16 '18 at 12:30











  • I will try that, thanks

    – Patrick
    Nov 16 '18 at 12:30











  • i only need to look for the name after "bis_XXX" so that's why my test file looks different the a original one.

    – Patrick
    Nov 16 '18 at 13:19











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336923%2fpowershell-script-to-move-files-with-date-as-name-in-it-to-a-folder%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














An approach with a regular expression to grep the date in a where-Object and

a single pipe should be more efficient:



# if the abbreviated month names compply with the current culture
$CI = Get-Culture

# or a selected deviating culture
#$CI = New-Object system.globalization.cultureinfo("de-DE")

$RE = "_bis_(d2-("+($CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|')+")-20d2)"

Get-ChildItem | Where-Object $_.Name -match $RE -and
([datetime]::ParseExact($Matches[1],"dd-MMM-yyyy",$CI) -lt [datetime]::now)|
Move-Item -Destination "C:temptestcopy" -WhatIf


If the output looks OK remove the -WhatIf



To better explain what $CI for culture information returns,



On my German locale



> $CI = Get-Culture
> $CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|'
Jan|Feb|Mrz|Apr|Mai|Jun|Jul|Aug|Sep|Okt|Nov|Dez



> $CI = New-Object system.globalization.cultureinfo("en-US")
> $CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|'
Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec
> $RE = "_bis_(d2-("+($CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|')+")-20d2)"
> $RE
_bis_(d2-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-20d2)


See the RegEx live on regex101.com






share|improve this answer

























  • thanks!! Looks good so far but need to do further testing

    – Patrick
    Nov 16 '18 at 14:33






  • 1





    works! Thanks a lot for your help.

    – Patrick
    Nov 16 '18 at 16:16















0














An approach with a regular expression to grep the date in a where-Object and

a single pipe should be more efficient:



# if the abbreviated month names compply with the current culture
$CI = Get-Culture

# or a selected deviating culture
#$CI = New-Object system.globalization.cultureinfo("de-DE")

$RE = "_bis_(d2-("+($CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|')+")-20d2)"

Get-ChildItem | Where-Object $_.Name -match $RE -and
([datetime]::ParseExact($Matches[1],"dd-MMM-yyyy",$CI) -lt [datetime]::now)|
Move-Item -Destination "C:temptestcopy" -WhatIf


If the output looks OK remove the -WhatIf



To better explain what $CI for culture information returns,



On my German locale



> $CI = Get-Culture
> $CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|'
Jan|Feb|Mrz|Apr|Mai|Jun|Jul|Aug|Sep|Okt|Nov|Dez



> $CI = New-Object system.globalization.cultureinfo("en-US")
> $CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|'
Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec
> $RE = "_bis_(d2-("+($CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|')+")-20d2)"
> $RE
_bis_(d2-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-20d2)


See the RegEx live on regex101.com






share|improve this answer

























  • thanks!! Looks good so far but need to do further testing

    – Patrick
    Nov 16 '18 at 14:33






  • 1





    works! Thanks a lot for your help.

    – Patrick
    Nov 16 '18 at 16:16













0












0








0







An approach with a regular expression to grep the date in a where-Object and

a single pipe should be more efficient:



# if the abbreviated month names compply with the current culture
$CI = Get-Culture

# or a selected deviating culture
#$CI = New-Object system.globalization.cultureinfo("de-DE")

$RE = "_bis_(d2-("+($CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|')+")-20d2)"

Get-ChildItem | Where-Object $_.Name -match $RE -and
([datetime]::ParseExact($Matches[1],"dd-MMM-yyyy",$CI) -lt [datetime]::now)|
Move-Item -Destination "C:temptestcopy" -WhatIf


If the output looks OK remove the -WhatIf



To better explain what $CI for culture information returns,



On my German locale



> $CI = Get-Culture
> $CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|'
Jan|Feb|Mrz|Apr|Mai|Jun|Jul|Aug|Sep|Okt|Nov|Dez



> $CI = New-Object system.globalization.cultureinfo("en-US")
> $CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|'
Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec
> $RE = "_bis_(d2-("+($CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|')+")-20d2)"
> $RE
_bis_(d2-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-20d2)


See the RegEx live on regex101.com






share|improve this answer















An approach with a regular expression to grep the date in a where-Object and

a single pipe should be more efficient:



# if the abbreviated month names compply with the current culture
$CI = Get-Culture

# or a selected deviating culture
#$CI = New-Object system.globalization.cultureinfo("de-DE")

$RE = "_bis_(d2-("+($CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|')+")-20d2)"

Get-ChildItem | Where-Object $_.Name -match $RE -and
([datetime]::ParseExact($Matches[1],"dd-MMM-yyyy",$CI) -lt [datetime]::now)|
Move-Item -Destination "C:temptestcopy" -WhatIf


If the output looks OK remove the -WhatIf



To better explain what $CI for culture information returns,



On my German locale



> $CI = Get-Culture
> $CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|'
Jan|Feb|Mrz|Apr|Mai|Jun|Jul|Aug|Sep|Okt|Nov|Dez



> $CI = New-Object system.globalization.cultureinfo("en-US")
> $CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|'
Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec
> $RE = "_bis_(d2-("+($CI.DateTimeFormat.AbbreviatedMonthNames -ne '' -join '|')+")-20d2)"
> $RE
_bis_(d2-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-20d2)


See the RegEx live on regex101.com







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 17:16

























answered Nov 16 '18 at 13:54









LotPingsLotPings

20.3k61633




20.3k61633












  • thanks!! Looks good so far but need to do further testing

    – Patrick
    Nov 16 '18 at 14:33






  • 1





    works! Thanks a lot for your help.

    – Patrick
    Nov 16 '18 at 16:16

















  • thanks!! Looks good so far but need to do further testing

    – Patrick
    Nov 16 '18 at 14:33






  • 1





    works! Thanks a lot for your help.

    – Patrick
    Nov 16 '18 at 16:16
















thanks!! Looks good so far but need to do further testing

– Patrick
Nov 16 '18 at 14:33





thanks!! Looks good so far but need to do further testing

– Patrick
Nov 16 '18 at 14:33




1




1





works! Thanks a lot for your help.

– Patrick
Nov 16 '18 at 16:16





works! Thanks a lot for your help.

– Patrick
Nov 16 '18 at 16:16













0














As @AnsgarWiechers pointed out, you are not using the correct date format.



MWE



$a = dir c:temptest
$a | foreach
$file = $_.Name;
$full = $_.FullName

switch -wildcard ($file)
"*bis_*"
$file
[string]$date = ($file.Split("_"))[2]
$date
$datetime = [DateTime]::ParseExact($date, "dd-MMM-yyyy", $null)
$datetime
if ($datetime -lt (Get-Date))
Move-Item $full "C:temptestcopy"


#end switch
#end foreach


Edit



It appears that all of your files do not follow the same structure. I take it that you should use the dat after bis_ based on the two provided examples:




  • TSE_M001340_04-Nov-2015_bis_03-Nov-2018.pdf

  • file_test_bis_23-APR-2021.pdf



Here is the updated code:



$a = dir c:temptest
$a | foreach
$file = $_.BaseName;
$full = $_.FullName
$split = "bis_"

switch -wildcard ($file)
"*$split*"
$file
[string]$date = ($file.Split($split))[1]
$date
$datetime = [DateTime]::ParseExact($date, "dd-MMM-yyyy", $null)
$datetime
if ($datetime -lt (Get-Date))
Move-Item $full "C:temptestcopy"


#end switch
#end foreach





share|improve this answer

























  • ok didn't see that...

    – Patrick
    Nov 16 '18 at 12:30











  • I will try that, thanks

    – Patrick
    Nov 16 '18 at 12:30











  • i only need to look for the name after "bis_XXX" so that's why my test file looks different the a original one.

    – Patrick
    Nov 16 '18 at 13:19















0














As @AnsgarWiechers pointed out, you are not using the correct date format.



MWE



$a = dir c:temptest
$a | foreach
$file = $_.Name;
$full = $_.FullName

switch -wildcard ($file)
"*bis_*"
$file
[string]$date = ($file.Split("_"))[2]
$date
$datetime = [DateTime]::ParseExact($date, "dd-MMM-yyyy", $null)
$datetime
if ($datetime -lt (Get-Date))
Move-Item $full "C:temptestcopy"


#end switch
#end foreach


Edit



It appears that all of your files do not follow the same structure. I take it that you should use the dat after bis_ based on the two provided examples:




  • TSE_M001340_04-Nov-2015_bis_03-Nov-2018.pdf

  • file_test_bis_23-APR-2021.pdf



Here is the updated code:



$a = dir c:temptest
$a | foreach
$file = $_.BaseName;
$full = $_.FullName
$split = "bis_"

switch -wildcard ($file)
"*$split*"
$file
[string]$date = ($file.Split($split))[1]
$date
$datetime = [DateTime]::ParseExact($date, "dd-MMM-yyyy", $null)
$datetime
if ($datetime -lt (Get-Date))
Move-Item $full "C:temptestcopy"


#end switch
#end foreach





share|improve this answer

























  • ok didn't see that...

    – Patrick
    Nov 16 '18 at 12:30











  • I will try that, thanks

    – Patrick
    Nov 16 '18 at 12:30











  • i only need to look for the name after "bis_XXX" so that's why my test file looks different the a original one.

    – Patrick
    Nov 16 '18 at 13:19













0












0








0







As @AnsgarWiechers pointed out, you are not using the correct date format.



MWE



$a = dir c:temptest
$a | foreach
$file = $_.Name;
$full = $_.FullName

switch -wildcard ($file)
"*bis_*"
$file
[string]$date = ($file.Split("_"))[2]
$date
$datetime = [DateTime]::ParseExact($date, "dd-MMM-yyyy", $null)
$datetime
if ($datetime -lt (Get-Date))
Move-Item $full "C:temptestcopy"


#end switch
#end foreach


Edit



It appears that all of your files do not follow the same structure. I take it that you should use the dat after bis_ based on the two provided examples:




  • TSE_M001340_04-Nov-2015_bis_03-Nov-2018.pdf

  • file_test_bis_23-APR-2021.pdf



Here is the updated code:



$a = dir c:temptest
$a | foreach
$file = $_.BaseName;
$full = $_.FullName
$split = "bis_"

switch -wildcard ($file)
"*$split*"
$file
[string]$date = ($file.Split($split))[1]
$date
$datetime = [DateTime]::ParseExact($date, "dd-MMM-yyyy", $null)
$datetime
if ($datetime -lt (Get-Date))
Move-Item $full "C:temptestcopy"


#end switch
#end foreach





share|improve this answer















As @AnsgarWiechers pointed out, you are not using the correct date format.



MWE



$a = dir c:temptest
$a | foreach
$file = $_.Name;
$full = $_.FullName

switch -wildcard ($file)
"*bis_*"
$file
[string]$date = ($file.Split("_"))[2]
$date
$datetime = [DateTime]::ParseExact($date, "dd-MMM-yyyy", $null)
$datetime
if ($datetime -lt (Get-Date))
Move-Item $full "C:temptestcopy"


#end switch
#end foreach


Edit



It appears that all of your files do not follow the same structure. I take it that you should use the dat after bis_ based on the two provided examples:




  • TSE_M001340_04-Nov-2015_bis_03-Nov-2018.pdf

  • file_test_bis_23-APR-2021.pdf



Here is the updated code:



$a = dir c:temptest
$a | foreach
$file = $_.BaseName;
$full = $_.FullName
$split = "bis_"

switch -wildcard ($file)
"*$split*"
$file
[string]$date = ($file.Split($split))[1]
$date
$datetime = [DateTime]::ParseExact($date, "dd-MMM-yyyy", $null)
$datetime
if ($datetime -lt (Get-Date))
Move-Item $full "C:temptestcopy"


#end switch
#end foreach






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 13:04

























answered Nov 16 '18 at 11:51









FlorianFlorian

234112




234112












  • ok didn't see that...

    – Patrick
    Nov 16 '18 at 12:30











  • I will try that, thanks

    – Patrick
    Nov 16 '18 at 12:30











  • i only need to look for the name after "bis_XXX" so that's why my test file looks different the a original one.

    – Patrick
    Nov 16 '18 at 13:19

















  • ok didn't see that...

    – Patrick
    Nov 16 '18 at 12:30











  • I will try that, thanks

    – Patrick
    Nov 16 '18 at 12:30











  • i only need to look for the name after "bis_XXX" so that's why my test file looks different the a original one.

    – Patrick
    Nov 16 '18 at 13:19
















ok didn't see that...

– Patrick
Nov 16 '18 at 12:30





ok didn't see that...

– Patrick
Nov 16 '18 at 12:30













I will try that, thanks

– Patrick
Nov 16 '18 at 12:30





I will try that, thanks

– Patrick
Nov 16 '18 at 12:30













i only need to look for the name after "bis_XXX" so that's why my test file looks different the a original one.

– Patrick
Nov 16 '18 at 13:19





i only need to look for the name after "bis_XXX" so that's why my test file looks different the a original one.

– Patrick
Nov 16 '18 at 13:19

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336923%2fpowershell-script-to-move-files-with-date-as-name-in-it-to-a-folder%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線