Dette script, der kopierer filer, sletter dem også ved en fejl.
Dette script har jeg snuppet fra et andet spørgsmål og rettet til. det kopierer alle filer i en mappe til en anden mappe, inkl. alle undermapper, og filføjer "thumb_" til alle kopierede filnavne.Spiller perfekt.
Men - hvis jeg kører scriptet 2 gange i træk, sletter den alle filerne i anden omgang. Hvorfor - og hvordan undgås det - den skulle gerne springe over, hvis targetFolder ikke er tom...
dim targetFolder,sourceFolder, prefix
sub log(msg)
'msgBox msg
end sub
function Chk(sourceFolder,targetFolder)
chk=true
if not objFSO.FolderExists(sourceFolder) then
MsgBox("Kan ikke åbne kilde : "&sourceFolder)
chk=false
end if
if not objFSO.FolderExists(targetFolder) then
msgBox("Kan ikke åbne destination : "&targetFolder)
chk=false
end if
end function
sub Quit()
MsgBox "Forbindelsen til netværket afbrudt"
WScript.Quit
end sub
sub doCopy(sourceFolder,targetFolder,prefix)
dim folder,fileCol,folderCol,fil,tFolder,tFileCol,tFolderCol,FileStr,FolderStr
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
set folder = objFSO.getFolder(sourceFolder)
set tFolder = objFSO.getFolder(targetFolder)
set tFolderCol=tFolder.SubFolders
set tFileCol=tFolder.Files
set fileCol = folder.Files
set folderCol=folder.SubFolders
FileStr=""
For Each fil in tFileCol
FileStr=FileStr&";"&fil.name&";,"
next
FolderStr=""
For Each folder in tFolderCol
FolderStr=FolderStr&";"&folder.name&";,"
next
For Each fil in filecol
if not objFSO.FileExists(targetFolder&"\"&fil.name) then
objFSO.CopyFile SourceFolder&"\"&fil.name, targetFolder&"\"&prefix&fil.name
If Err.Number = 0 Then
log "Copying file: "&sourceFolder&"\"&fil.name
else
Quit()
end if
end if
FileStr=Replace(FileStr,";"&fil.name&";,","")
next
For Each folder in folderCOl
if not objFSO.FolderExists(targetFolder&"\"&folder.name) then
objFSO.CreateFolder(targetFolder&"\"&folder.name)
If Err.Number = 0 Then
log "Creating folder: "&sourceFolder&"\"&folder.name
else
Quit()
end if
end if
FolderStr=Replace(FolderStr,";"&folder.name&";,","")
doCopy sourceFolder&"\"&folder.name,targetFolder&"\"&folder.name, prefix
next
set fileCol = nothing
set folderCol = nothing
set folder = nothing
dim delArrFile,delArrFolder,a
if len(FileStr)>0 then
delArrFile=split(FileStr,",")
For a = LBound(delArrFile) to UBound(delArrFile)-1
objFSO.DeleteFile(targetFolder&"\"&replace(delArrFile(a),";",""))
next
end if
if len(FolderStr)>0 then
delArrFolder=split(FolderStr,",")
For a = LBound(delArrFolder) to UBound(delArrFolder)-1
objFSO.DeleteFolder(targetFolder&"\"&replace(delArrFolder(a),";",""))
next
end if
set tFileCol = nothing
set tFolderCol = nothing
set tFolder = nothing
end sub
'-----HOVEDPROGRAMMET STARTER HER------
Dim objFSO,wshShell
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
sourceFolder="C:\Temp\Source" '< UDFYLD SELV
targetFolder="C:\Temp\destination" '< UDFYLD SELV
prefix = "thumb_" '< UDFYLD SELV
Set WshShell = CreateObject("WScript.Shell")
WshShell.Popup "Hej Smukke. Nu kopieres alle filer fra " & sourceFolder & " til " & targetFolder & ", og får et prefix: " & prefix & " - hurra for det"
WshShell.Popup "Begynder kopiering - alle filer og undermapper!!!", 2, , 0
if (chk (sourceFolder,targetFolder)) then
doCopy sourceFolder,targetFolder, prefix
WshShell.Popup "Kopiering afsluttet", 2, , 0
end if
Set WshShell = nothing
Set objFSO = nothing