HI
I need to copy .dll files in C:\Windows\system32\ which is found or match in my my_file.txt file to C:\tools folder.

my code not work and need also to found words that can match my_file.txt.
so for example I have RICHED20.dll in my my_file.txt if the same file name"RICHED20.dll" exist in C:\Windows\system32\ the RICHED20.dll copy to my C:\tools

import os
import shutil
source = os.listdir "C:\Windows\system32\"
destination = "C:\tools"
for files in source:
    if files.endswith(".dll"):
        shutil.move(files,destination)

my_file.txt

dbghelp.dll
RICHED20.dll
Riched32.dll
napinsp.dll
wshbth.dll
cscapi.dll
pnrpnsp.dll
winrnr.dll
rasadhlp.dll
Attachments
dbghelp.dll
RICHED20.dll
Riched32.dll
napinsp.dll
wshbth.dll
cscapi.dll
pnrpnsp.dll
winrnr.dll
rasadhlp.dll
wbemsvc.dll
NTDSAPI.dll
fastprox.dll
wbemprox.dll
wbemcomn.dll
dhcpcsvc.DLL
dhcpcsvc6.DLL
IPHLPAPI.DLL
wkscli.dll
netutils.dll
NETAPI32.dll
WINNSI.DLL
NLAapi.dll
dwmapi.dll
uxtheme.dll
COMCTL32.dll
VERSION.dll
wshtcpip.dll
rsaenh.dll
DNSAPI.dll
wship6.dll
mswsock.dll
CRYPTSP.dll
srvcli.dll
CRYPTBASE.dll
RpcRtRemote.dll
MSASN1.dll
CRYPT32.dll
KERNELBASE.dll
WINTRUST.dll
USER32.dll
msvcrt.dll
SHLWAPI.dll
ole32.dll
LPK.dll
NSI.dll
GDI32.dll
WS2_32.dll
IMM32.DLL
OLEAUT32.dll
RPCRT4.dll
USP10.dll
CLBCatQ.DLL
SHELL32.dll
sechost.dll
MSCTF.dll
kernel32.dll
COMDLG32.dll
FLTLIB.DLL
ntdll.dll
Explorer.EXE
imageres.dll
wmploc.DLL
werconcpl.dll
wscui.cpl
WMVCORE.DLL
wpdshext.dll
wpdshext.dll
PhotoViewer.dll
mf.dll
framedynos.dll
UIAnimation.dll
ieframe.DLL
d3d9.dll
NaturalLanguage6.dll
RICHED20.dll
ACLUI.dll
OobeFldr.dll
provsvc.dll
hgcpl.dll
SyncCenter.dll
imapi2.dll
OobeFldr.dll
NLSData0009.dll
OLEACC.dll
QAgent.dll
wwanapi.dll
Wlanapi.dll
FXSRESM.DLL
FXSAPI.dll
fxsst.dll
DeviceCenter.dll
wwapi.dll
Actioncenter.dll
pnidui.dll
netshell.dll
wercplsupport.dll
RichEd32.dll
ksuser.dll
wdmaud.drv
wlanutil.dll
hcproviders.dll
NetworkExplorer.dll
sud.dll
tiptsf.dll
msls31.dll
gameux.dll
PhotoMetadataHandler.dll
wscinterop.dll
zipfldr.dll
avgse.dll
ieproxy.dll
timedate.cpl
IconCodecService.dll
acppage.dll
srchadmin.dll
cscobj.dll
QUtil.dll
ntshrui.dll
sfc_os.DLL
sfc.dll
msiltcfg.dll
shdocvw.dll
syncui.dll
CSCAPI.dll
SYNCENG.dll
actxprxy.dll
CSCDLL.dll
cscui.dll
msxml6.dll
EhStorShell.dll
EXPLORERFRAME.dll
dxgi.dll
WINMM.dll
msutb.dll
LINKINFO.dll
rarext.dll
WINSPOOL.DRV
MPR.dll
NTDSAPI.dll
SqmApi.dll
MSVCR110.dll
MSVCP110.dll
dhcpcsvc.DLL
dhcpcsvc6.DLL
PortableDeviceTypes.dll
wpdshserviceobj.dll
ehSSO.dll
dxp.dll
prnfldr.dll
WSCAPI.dll
BatMeter.dll
stobject.dll
EhStorAPI.dll
mlang.dll
WMASF.DLL
SearchFolder.dll
PortableDeviceApi.dll
AltTab.dll
comsvcs.dll
audiodev.dll
MsftEdit.dll
audiodev.dll
mfplat.dll
thumbcache.dll
gz.fmt
IPHLPAPI.DLL
npmproxy.dll
midimap.dll
MSACM32.dll
msacm32.drv
mssprxy.dll
samcli.dll
wkscli.dll
netutils.dll
AUDIOSES.DLL
twext.dll
WindowsCodecs.dll
WINNSI.DLL
WTSAPI32.dll
es.dll
slc.dll
dsrole.dll
ATL.DLL
nlaapi.dll
ntmarta.dll
sbdrop.dll
AVRT.dll
POWRPROF.dll
xmllite.dll
dwmapi.dll
MMDevApi.dll
HID.DLL
SndVolSSO.DLL
DUser.dll
DUI70.dll
gdiplus.dll
PROPSYS.dll
SAMLIB.dll
comctl32.dll
CRYPTUI.dll
authui.dll
VERSION.dll
USERENV.dll
SPINF.dll
credssp.dll
bcryptprimitives.dll
DEVRTL.dll
rsaenh.dll
dnsapi.DLL
CRYPTSP.dll
bcrypt.dll
ncrypt.dll
wevtapi.dll
srvcli.dll
Secur32.dll
SSPICLI.DLL
apphelp.dll
CRYPTBASE.dll
SXS.DLL
WINSTA.dll
RpcRtRemote.dll
profapi.dll
MSASN1.dll
CRYPT32.dll
KERNELBASE.dll
CFGMGR32.dll
WINTRUST.dll
DEVOBJ.dll
USER32.dll
msvcrt.dll
SHLWAPI.dll
ole32.dll
LPK.dll
SETUPAPI.dll
WININET.dll
WLDAP32.dll
NSI.dll
GDI32.dll
WS2_32.dll
IMM32.dll
OLEAUT32.dll
urlmon.dll
RPCRT4.dll
imagehlp.dll
USP10.dll
iertutil.dll
CLBCatQ.DLL
SHELL32.dll
sechost.dll
MSCTF.dll
kernel32.dll
avgcommx.dll
Normaliz.dll
arj.fmt
StructuredQuery.dll
shacct.dll
mmcshext.dll
WinSATAPI.dll
NLSLexicons0009.dll
SPPC.DLL
EhStorAPI.dll
sendmail.dll
PhotoBase.dll
wshext.dll
tar.fmt
WMPNSSUI.dll
d3d8thk.dll
hhsetup.dll
avgntopensslx.dll
avgsysx.dll
ntdll.dll
PSAPI.DLL
avgfilevaultx.dll
avglogx.dll
ntdll.dll
apisetschema.dll
kdcom.dll
msrpc.sys
mcupdate_GenuineIntel.dll
BOOTVID.dll
CI.dll
lsi_sas.sys
WDFLDR.SYS
WMILIB.SYS
pci.sys
partmgr.sys
BATTC.SYS
volmgrx.sys
PCIIDEX.SYS
mountmgr.sys
atapi.sys
ksecdd.sys
msahci.sys
fltmgr.sys
Ntfs.sys
fvevol.sys
cng.sys
Fs_Rec.sys
NETIO.SYS
rdyboost.sys
disk.sys
tcpip.sys
vmstorfl.sys
spldr.sys
hwpolicy.sys
CLASSPNP.SYS
avglogx.sys
avgidshx.sys
mrxsmb10.sys
cdrom.sys
Beep.SYS
vga.sys
watchdog.sys
rdpencdd.sys
Msfs.SYS
tdx.sys
rassstp.sys
csc.sys
blbdrive.sys
avgidsshimx.sys
avgdiskx.sys
i8042prt.sys
vmmouse.sys
parport.sys
fdc.sys
ndiswan.sys
netbt.sys
ws2ifsl.sys
pacer.sys
vmhgfs.sys
wanarp.sys
rdbss.sys
mssmbios.sys
raspppoe.sys
rdpbus.sys
mrxsmb20.sys
ks.sys
flpydisk.sys
NDProxy.SYS
portcls.sys
usbccgp.sys
hidusb.sys
HIDPARSE.SYS
vmusbmouse.sys
mrxsmb.sys
rasl2tp.sys
dxgkrnl.sys
usbuhci.sys
E1G60I32.sys
usbehci.sys
intelppm.sys
CompositeBus.sys
swenum.sys
parvdm.sys
PROCMON23.SYS
fastfat.SYS
rfcomm.sys
bthpan.sys
dump_diskdump.sys
dump_dumpfve.sys
monitor.sys
lltdio.sys
HTTP.sys
win32k.sys
TSDDD.dll
cdd.dll
peauth.sys
srvnet.sys
srv2.sys
bthport.sys
BTHUSB.sys
dbghelp.dll
RICHED20.dll
Riched32.dll
napinsp.dll
wshbth.dll
cscapi.dll
pnrpnsp.dll
winrnr.dll
rasadhlp.dll
wbemsvc.dll
NTDSAPI.dll
fastprox.dll
wbemprox.dll
wbemcomn.dll
dhcpcsvc.DLL
dhcpcsvc6.DLL
IPHLPAPI.DLL
wkscli.dll
netutils.dll
NETAPI32.dll
WINNSI.DLL
NLAapi.dll
dwmapi.dll
uxtheme.dll
COMCTL32.dll
VERSION.dll
wshtcpip.dll
rsaenh.dll
DNSAPI.dll
wship6.dll
mswsock.dll
CRYPTSP.dll
srvcli.dll
CRYPTBASE.dll
RpcRtRemote.dll
MSASN1.dll
CRYPT32.dll
KERNELBASE.dll
WINTRUST.dll
USER32.dll
msvcrt.dll
SHLWAPI.dll
ole32.dll
LPK.dll
NSI.dll
GDI32.dll
WS2_32.dll
IMM32.DLL
OLEAUT32.dll
RPCRT4.dll
USP10.dll
CLBCatQ.DLL
SHELL32.dll
sechost.dll
MSCTF.dll
kernel32.dll
COMDLG32.dll
FLTLIB.DLL
ntdll.dll
ntdll.dll
CRYPTBASE.dll
sxs.dll
sxssrv.DLL
winsrv.DLL
basesrv.DLL
CSRSRV.dll
KERNELBASE.dll
USER32.dll
msvcrt.dll
LPK.dll
GDI32.dll
RPCRT4.dll
USP10.dll
kernel32.dll
ntdll.dll
wshtcpip.dll
credssp.dll
wship6.dll
mswsock.dll
secur32.dll
SSPICLI.DLL
apphelp.dll
CRYPTBASE.dll
RpcRtRemote.dll
profapi.dll
KERNELBASE.dll
USER32.dll
msvcrt.dll
LPK.dll
NSI.dll
GDI32.dll
WS2_32.dll
IMM32.DLL
RPCRT4.dll
USP10.dll
ADVAPI32.dll
sechost.dll
MSCTF.dll
kernel32.dll
ntdll.dll
CRYPTBASE.dll
sxs.dll
sxssrv.DLL
winsrv.DLL
basesrv.DLL
CSRSRV.dll
KERNELBASE.dll
USER32.dll
msvcrt.dll
LPK.dll
GDI32.dll
RPCRT4.dll
USP10.dll
kernel32.dll
ntdll.dll
MPR.dll
UXINIT.dll
wkscli.dll
netutils.dll
WindowsCodecs.dll
slc.dll
UxTheme.dll
rsaenh.dll
CRYPTSP.dll
netjoin.dll
SspiCli.dll
apphelp.dll
CRYPTBASE.dll
WINSTA.dll
RpcRtRemote.dll
profapi.dll
KERNELBASE.dll
USER32.dll
msvcrt.dll
ole32.dll
LPK.dll
GDI32.dll
IMM32.DLL
RPCRT4.dll
USP10.dll
ADVAPI32.dll
sechost.dll
MSCTF.dll
kernel32.dll
ntdll.dll
WTSAPI32.dll
wshtcpip.dll
UBPM.dll
credssp.dll
wship6.dll
mswsock.dll
AUTHZ.dll
srvcli.dll
SCESRV.dll
Secur32.dll
scext.dll
SspiCli.dll
apphelp.dll
CRYPTBASE.dll
WINSTA.dll
RpcRtRemote.dll
profapi.dll
KERNELBASE.dll
USER32.dll
msvcrt.dll
LPK.dll
NSI.dll
GDI32.dll
WS2_32.dll
IMM32.DLL
RPCRT4.dll
USP10.dll
ADVAPI32.dll
sechost.dll
MSCTF.dll
kernel32.dll
ntdll.dll
dssenh.dll
IPHLPAPI.DLL
samcli.dll
netutils.dll
WINNSI.DLL
SAMLIB.dll
wshtcpip.dll
GPAPI.dll
USERENV.dll
scecli.DLL
credssp.dll
bcryptprimitives.dll
pku2u.DLL
tspkg.DLL
efslsaext.dll
rsaenh.dll
wdigest.DLL
schannel.DLL
logoncli.dll
DNSAPI.dll
netlogon.DLL
msv1_0.DLL
wship6.dll
mswsock.dll
CRYPTSP.dll
kerberos.DLL
negoexts.DLL
netjoin.dll
msprivs.DLL
bcrypt.dll
ncrypt.dll
AUTHZ.dll
cngaudit.dll
wevtapi.dll
cryptdll.dll
SAMSRV.dll
lsasrv.dll
Secur32.dll
SspiSrv.dll
SspiCli.dll
cryptbase.dll
WINSTA.dll
RpcRtRemote.dll
profapi.dll
MSASN1.dll
CRYPT32.dll
KERNELBASE.dll
USER32.dll
msvcrt.dll
LPK.dll
WLDAP32.dll
NSI.dll
GDI32.dll
WS2_32.dll
IMM32.DLL
RPCRT4.dll
USP10.dll
ADVAPI32.dll
sechost.dll
MSCTF.dll
kernel32.dll
ntdll.dll
pcwum.dll
credssp.dll
WMsgAPI.dll
SYSNTFY.dll
secur32.dll
SSPICLI.DLL
CRYPTBASE.dll
RpcRtRemote.dll
KERNELBASE.dll
msvcrt.dll
RPCRT4.dll
ADVAPI32.dll
sechost.dll
kernel32.dll
ntdll.dll
wmiutils.dll
wbemsvc.dll
NTDSAPI.dll
FastProx.dll
wmidcprv.dll
wbemprox.dll
wbemcomn.dll
WTSAPI32.dll
ntmarta.dll
rpcss.dll
pcwum.DLL
GPAPI.dll
USERENV.dll
SPINF.dll
umpnpmgr.dll
credssp.dll
DEVRTL.dll
rsaenh.dll
CRYPTSP.dll
SspiCli.dll
apphelp.dll
CRYPTBASE.dll
WINSTA.dll
RpcRtRemote.dll
profapi.dll
MSASN1.dll
CRYPT32.dll
KERNELBASE.dll
CFGMGR32.dll
WINTRUST.dll
DEVOBJ.dll
USER32.dll
msvcrt.dll
ole32.dll
LPK.dll
SETUPAPI.dll
WLDAP32.dll
NSI.dll
GDI32.dll
WS2_32.dll
IMM32.DLL
OLEAUT32.dll
RPCRT4.dll
USP10.dll
CLBCatQ.DLL
ADVAPI32.dll
sechost.dll
MSCTF.dll
kernel32.dll
ntdll.dll
fwpuclnt.dll
WTSAPI32.dll
ntmarta.dll
VERSION.dll
FirewallAPI.dll
wshtcpip.dll
rpcepmap.dll
rpcss.dll
credssp.dll
rsaenh.dll
wship6.dll
mswsock.dll
CRYPTSP.dll
secur32.dll
SSPICLI.DLL
CRYPTBASE.dll
WINSTA.dll
RpcRtRemote.dll
KERNELBASE.dll
user32.dll
msvcrt.dll
ole32.dll
LPK.dll
WLDAP32.dll
NSI.dll
GDI32.dll
WS2_32.dll
IMM32.DLL
OLEAUT32.dll
RPCRT4.dll
USP10.dll
CLBCatQ.DLL
ADVAPI32.dll
sechost.dll
MSCTF.dll
kernel32.dll
ntdll.dll
dbghelp.dll
wuapi.dll
WMALFXGFXDSP.dll
adtschema.dll
Cabinet.dll
qmgr.dll
napinsp.dll
wshbth.dll
pnrpnsp.dll
winrnr.dll
rasadhlp.dll
wbemsvc.dll
NTDSAPI.dll
fastprox.dll
wbemprox.dll
wbemcomn.dll
dhcpcore6.dll
dhcpcsvc.DLL
dhcpcsvc6.DLL
nrpsrv.DLL
adtschema.dll
wscsvc.dll
mfplat.DLL
RstrtMgr.dll
IPHLPAPI.DLL
wkscli.dll
netutils.dll
audioses.dll
fwpuclnt.dll
WINNSI.DLL
lmhsvc.dll
NLAapi.dll
gpsvc.dll
AVRT.dll
POWRPROF.dll
audiosrv.dll
MMDevAPI.DLL
wevtsvc.dll
PROPSYS.dll
VERSION.dll
firewallapi.dll
wshtcpip.dll
GPAPI.dll
USERENV.dll
credssp.dll
bcryptprimitives.dll
rsaenh.dll
DNSAPI.dll
wship6.dll
mswsock.dll
CRYPTSP.dll
bcrypt.dll
ncrypt.dll
secur32.dll
SSPICLI.DLL
CRYPTBASE.dll
WINSTA.dll
RpcRtRemote.dll
profapi.dll
MSASN1.dll
CRYPT32.dll
KERNELBASE.dll
CFGMGR32.dll
WINTRUST.dll
DEVOBJ.dll
USER32.dll
msvcrt.dll
SHLWAPI.dll
ole32.dll
LPK.dll
SETUPAPI.dll
NSI.dll
GDI32.dll
WS2_32.dll
IMM32.DLL
OLEAUT32.dll
RPCRT4.dll
imagehlp.dll
USP10.dll
CLBCatQ.DLL
ADVAPI32.dll
sechost.dll
MSCTF.dll
#the following will copy any dll files in my_file.txt from
#C:\Windows\Sytem32 to C:\Tools
import os
import shutil

sourcedir = "C:\\Windows\\System32"

source = os.listdir(sourcedir)

destination = "C:\\Tools"  

dllmatch = open("my_file.txt", "r")    # may need full path here

matchf = dllmatch.readlines()

for i in range(0,len(matchf)):
    if matchf[i][-1] == "\n":
        matchf[i] = matchf[i][:-1]    # :-1 to strip off \n

for files in source:
    if files in matchf:
        if os.path.splitext(files)[1] == ".dll":
            shutil.copy(os.path.join(sourcedir,files),destination)

Good effort Peter 18,the code dos what it shall.
I would suggest some improvements.
To strip off \n is better to use strip()

>>> s = 'hello\n'
>>> s.strip()
'hello'

So code under can be one line matchf = [i.strip() for i in dllmatch]

matchf = dllmatch.readlines()

for i in range(0,len(matchf)):
    if matchf[i][-1] == "\n":
        matchf[i] = matchf[i][:-1]    # :-1 to strip off \

Complete code with a couple more changes.

import os
import shutil    

source = os.listdir("C:\\Windows\\System32")
destination = "C:\\Tools"  

with open('my_file.txt') as f_obj:
    matchf = [i.strip() for i in f_obj]

for files in source:
    if files.endswith('.dll') and files in matchf:
        shutil.copy(os.path.join(sourcedir, files), destination)

Edited 2 Years Ago by snippsat

This article has been dead for over six months. Start a new discussion instead.