0

I would like my script to fetch certain files from within multiple directories (location).

For Example:

Set 1:

Following files from C:\test\ext

20160830.ext  -- (CurrentDate - 1).ext
20160823.ext  -- (CurrentDate - 7).ext
20160802.ext  -- (CurrentDate - 28).ext

Following file from C:\test\nxt

20160830.nxt  -- (CurrentDate - 1).nxt

Set 2:

Following files from C:\test2\ext (These files have a prefix before the name)

IND20160830.ext  -- (INDCurrentDate - 1).ext
IND20160823.ext  -- (INDCurrentDate - 7).ext
IND20160802.ext  -- (INDCurrentDate - 28).ext

Following files from C:\test2\nxt

IND20160830.nxt  -- (INDCurrentDate - 1).nxt

I have got the current date converted into yyyymmdd format using the following command

$Curr_date = (get-date).ToString('yyyyMMdd')

Output: 20160831

But I am unsure how to fetch the files as per conditions listed above from those directory as its got to be dynamic as the script will be schedule to run weekly and should fetch the files as per the above condition.

9
  • By fetch do you mean copy the files to another location? Commented Aug 31, 2016 at 6:15
  • i have got the date format for Current Date -1, -7 and -28 days using (get-date).AddDays(-1).ToString('yyyyMMdd'). So thats done. I need to know how can i search & fetch the files with these dates.extensions. Commented Aug 31, 2016 at 6:15
  • @Jonas yes that's right Jonas so for example if (file is currentdate - 1.ext) then copy to destination Commented Aug 31, 2016 at 6:16
  • 2
    You will need to set conditions and parse through your test folders and copy the output to your destination. Commented Aug 31, 2016 at 6:22
  • 1
    Browse to meta.stackexchange.com/questions/74784/… Commented Aug 31, 2016 at 6:50

2 Answers 2

1

This is a small example of copying one of your files from C:\test\ext to a file called Output on the desktop. Note the "" around your file name. This means that the spaces in your file name have been excluded.

@ECHO OFF
xcopy "C:\test\ext\20160830.ext  -- (CurrentDate - 1).ext" "%userprofile%\Desktop\Output\"
pause

You need to find a way to do this to all of your files. Don't use dates if you already know the file name.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but the suggestions are very theoretical and something which i already know, i need some syntax related advice. However i have figured it out and its working. Thanks again for your support though.
0

So this is what i did:

  1. I fetch the dates in the right format first '$Date = (Get-date.AddDays(-1).TopString('yyyyMMdd')'

  2. Then create another variable '$V1 = "$Date.ext"'

  3. 'If($file.Name -eq $V1) {

    Copy-Item -Path $file.FullName -Destination $Path

    }'

This works fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.