0

I need to copy lots of specific pdf files with specific names. All the files are insame folder, I have all the file names in csv. I made a lazy "script" which is like this (obv. not all the files in this post):

Copy-Item -Path 391248* -Destination <destination>
Copy-Item -Path 241044* -Destination <destination>
Copy-Item -Path 532350* -Destination <destination>

Does anyone have more clean solution for this?

2

3 Answers 3

0
$fileList  = 'C:\filelist.csv'

Get-Content -Path $fileList | ForEach-Object {

    Copy-Item -Path $_ -Destination 'destination'
}
Sign up to request clarification or add additional context in comments.

2 Comments

Please dont post code only answer, try to add some context of how this will solve the problem in question.
This is a simple one liner only
0

Another one liner solution is:

cat "The path of csv file.csv" | % {copy -path $_ -destination "The destination"}

Comments

0

My CSV looks like this:

Filename
1
2
3
4
5
6
7
8
9

This csv does not contain the file extension, the below code adds the file extension '.pdf'. You can modify it to contain anything you require.

$csv_location = 'path to your csv'

# 'FileName' is the name of the column inside the csv
$file_names   = ( Import-Csv $csv_location ).FileName
$destination  = 'path of files to be copied into'
$origin       = 'path of files to be copied from'

$file_names | ForEach-Object {

#   This line adds the extension '.pdf' to the filename and file path obtained from $file_names and $origin, respectively.
    $full_path = $origin + $_ + '.pdf'

#   Copy from origin to ( destination + file name + file extension )
    Copy-Item -Path ( $full_path ) -Destination $destination -Force
}

If your csv contains the file extension,

$full_path = $origin + $_

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.