0

Summary

I need to copy zip files from one folder to another, the names of the files to copy are in another file, but without the extension

Files in source

a.zip
b.zip
c.zip
no-copy.zip

Text File Contents

a
b
c
2
  • What would you do if the extension were in the text file?
    – AFH
    Oct 10, 2017 at 21:21
  • 2
    This sounds like an excellent use for Find-Replace. In Word, Find each Carriage-return, Replace with '.zip' Carriage-return. There are other programs and regular expressions that let you do the same thing. Oct 10, 2017 at 21:57

2 Answers 2

1

Here is in a simple command prompt:

If you have the list file in your source folder and stand in it, you can run this in the command prompt to achieve what you need:

for /F "tokens=1" %%g in (list.txt) do (copy %%g.zip c:\folder\dest)

Explanation:

for /F "tokens=1" %%g stablish the loop saving each line of the file in variable %%g

in (list.txt) the file which have the names (without extension as you said)

do (copy %%g.zip c:\folder\dest) copy the content of the variable (which is the name of the file) plus '.zip' to c:\folder\dest.

3
  • Hello @LogoS, thank you for your reply. I'm getting an error message: "%%g was unexpected at this time", Please advise, Thanks yarone
    – yarone
    Oct 11, 2017 at 8:48
  • Sorry, I think you said you are making an script and I tested in a .bat file, that's why the double %. If you run it from the command prompt directly you need to use a single %. for /F "tokens=1" %g in (list.txt) do (copy %g.zip c:\copia\dest) (superuser.com/a/894479/748243)
    – LogoS
    Oct 11, 2017 at 11:47
  • Hello @LogoS, My bad, i thought i could also run it in CMD, i used only single % and its working great, thank you very much for hour help.
    – yarone
    Oct 11, 2017 at 22:14
0

Here's a one-liner that you can use in Powershell:

Get-Content \\path\to\textfile.txt | ForEach { Copy-Item SourceFolder\$_ DestinationFolder\$_.zip }

This is assuming you're running Windows 7 or above.

It reads your text file, and for each line, copies the file from the source folder to the destination folder while simultaneously appending a ".zip" to the filename.

4
  • Hello @Charles Burge, thank you for your reply. I'm getting an error message: "Copy-Item : Cannot find path '\\path\to\textfile.txt' because it does not exist." should i need to put $_.zip in both source and destination? Thanks yarone
    – yarone
    Oct 11, 2017 at 8:44
  • I thought it would be clear - you're supposed to replace \\path\to\textfile.txt with the actual path to the text file referenced in your question. And no, don't put the $_.zip in the source. The point is that you're *renaming" $_ to $_.zip. Oct 11, 2017 at 17:17
  • Hello @Charles Burge, it was very clear, i did rename it to the exact path and only change it back again here. When i tried the command above i received the error :"Copy-Item : Cannot find path '\\server\folder\file' because it does not exist." and i noticed that it was searching for the file without the extension and when i added "$_.zip" also to the source it did find the file and started copying it. Again the text file only included the file name without the extension, thank you very much for your help.
    – yarone
    Oct 11, 2017 at 22:07
  • OK that makes sense... I had misunderstood the original question. Glad you got it working. Oct 12, 2017 at 1:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .