How to move file while renaming it with incrementation ?

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 9 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
hackoo
Posts: 103
Last visit: Tue Apr 26, 2016 9:02 am

How to move file while renaming it with incrementation ?

Post by hackoo »

Hi ;)
In order to improve my script Hackoo VIRUS Cleaner.vbs

I created this code to test it before to implement it in my main script, but, i get some issues with this at line N° 46 with Error Saying "File dosen't exists"

Thank you for your help !
VBScript Code
Double-click the code block to select all.
Option Explicit
Dim SFile,SFile1,SFile2,SFile3,SFile4,Folder,Title,Rename
Title = "Moving File to the Quarantaine Folder ..."
SFile = "E:\HackooTest\Nouveau dossier\VIRUS.vbs"
Folder = "E:\HackooTest\Quarantaine"
Rename = GetNameFile(sFile)
Call MoveFile2Quarantaine(sFile,Folder,Rename)
MsgBox "The File " & DblQuote(Rename) & " is moved to the Quarantaine Folder",VbInformation,Title
'*************************************************************************************
SFile1 = "E:\HackooTest\Nouveau dossier(2)\VIRUS.vbs"
Rename = GetNameFile(sFile1)
Call MoveFile2Quarantaine(sFile1,Folder,Rename)
MsgBox "The File " & DblQuote(Rename) & " is moved to the Quarantaine Folder",VbInformation,Title
'*************************************************************************************
SFile2 = "E:\HackooTest\Nouveau dossier(3)\VIRUS.vbs"
Rename = GetNameFile(sFile2)
Call MoveFile2Quarantaine(sFile2,Folder,Rename)
MsgBox "The File " & DblQuote(Rename) & " is moved to the Quarantaine Folder",VbInformation,Title
'*************************************************************************************
SFile3 = "E:\HackooTest\Nouveau dossier(4)\VIRUS.vbs"
Rename = GetNameFile(sFile3)
Call MoveFile2Quarantaine(sFile3,Folder,Rename)
MsgBox "The File " & DblQuote(Rename) & " is moved to the Quarantaine Folder",VbInformation,Title
'*************************************************************************************
SFile4 = "E:\HackooTest\Nouveau dossier(5)\VIRUS.vbs"
Rename = GetNameFile(sFile4)
Call MoveFile2Quarantaine(sFile4,Folder,Rename)
MsgBox "The File " & DblQuote(Rename) & " is moved to the Quarantaine Folder",VbInformation,Title
'*************************************************************************************
Sub MoveFile2Quarantaine(sFile,Folder,Rename)
	'On Error Resume Next
	Dim  FSO,Ws,Tab,i,j
	Set Ws = CreateObject("Wscript.Shell")
	Set FSO = CreateObject("Scripting.FileSystemObject")
	'Tab = Split(sFile,"\")
	'Rename = Tab(UBound(Tab))
	i = 0
	j = i + 1
	Do
		i = i + 1
		'If FSO.FolderExists(Folder) Then
			If Not FSO.FileExists(Folder & "\" & Rename & "_Infected.txt") Then
				FSO.GetFile(sFile).Move Folder & "\" & Rename & "_Infected.txt"
				Exit Sub
			ElseIf Not FSO.FileExists(Folder & "\" & Rename & "("& i &")" & "_Infected.txt") Then 'And FSO.FileExists(Folder & "\" & Rename & "_Infected.txt") Then
				FSO.GetFile(sFile).Move Folder & "\" & Rename & "("& j &")" & "_Infected.txt"
				Exit Sub
			ElseIf j = i Then
				j = j + 1
				If Not FSO.FileExists(Folder & "\" & Rename & "("& j &")" & "_Infected.txt") Then
					FSO.GetFile(sFile).Move Folder & "\" & Rename & "("& j &")" & "_Infected.txt"
					Exit Sub
				End if
			End If
		'End If
	Loop Until j = i
End Sub
'**********************************************************************************************
'Fonction pour ajouter les doubles quotes dans une variable
Function DblQuote(Str)
	DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
Function GetNameFile(sFile)
	Dim  Tab
	Tab = Split(sFile,"\")
	GetNameFile = Tab(UBound(Tab))
End Function
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to move file while renaming it with incrementation ?

Post by jvierra »

What is the question? If a file does not exist it does not exist. Have you checked to see if it is where you think it is?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to move file while renaming it with incrementation ?

Post by jvierra »

Is this what you are trying to do?
VBScript Code
Double-click the code block to select all.
Set fso = CreateObject("Scripting.FileSystemObject")

sFiles = Array( _
    "e:\HackooTest\Nouveau dossier\VIRUS.vbs", _
    "e:\HackooTest\Nouveau dossier(2)\VIRUS.vbs", _
    "e:\HackooTest\Nouveau dossier(3)\VIRUS.vbs", _
    "e:\HackooTest\Nouveau dossier(4)\VIRUS.vbs", _
    "e:\HackooTest\Nouveau dossier(5)\VIRUS.vbs" _
)

For Each sFile In sFiles

    If fso.FileExists(sFile) Then
        sFileName = GetNewName(sFile)
        fso.MoveFile sFile, sFileName 
        WScript.Echo "The file """ & sfile & """ is moved to the Quarantaine Folder as " & sFileName
    Else
        MsgBox sFile & vbCrLf &  " DOES NOT EXIST!"
    End If

Next


Function GetNewName(sFile)
    snamebase = Split(Right(sFile, Len(sFile) - InStrRev(sFile,"\")),".")(0)
    sname = snamebase
    i = 0
    While i < 100
        sTarget = "e:\HackooTest\Quarantaine\" & sname & "_Infected.txt"
        If fso.FileExists(sTarget) Then
            i = i + 1
            sName = snamebase & "(" & i & ")"
        Else
            GetNewName = sTarget
            Exit Function
        End If
    Wend
    
End Function
Try not to get so complicated. After you learn how to write a scipt and have some experience then the design enhancements will make more sense and will be more natural. You are trying for dells and whistles before you have mastered the basics.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to move file while renaming it with incrementation ?

Post by jvierra »

Notice how much easier mine is to read. By simplifying and using a simple approach and no odd comments the code is very easy to follow. It is also much easier to change if needed.
User avatar
hackoo
Posts: 103
Last visit: Tue Apr 26, 2016 9:02 am

Re: How to move file while renaming it with incrementation ?

Post by hackoo »

Thank you very much for this solution that worked for me like i expected ;)
And this is my adaptation from your clever code :)
VBScript Code
Double-click the code block to select all.
Option Explicit
Dim fso,Ws,sFiles,sFile,sFileName,Quarantaine,Title
Title = "Déplacement des fichiers suspects dans la quarantaine" 
Set fso = CreateObject("Scripting.FileSystemObject")
Set Ws = CreateObject("wscript.Shell")
Quarantaine = "e:\HackooTest\Quarantaine"
sFiles = Array( _
"e:\HackooTest\Folder1\VIRUS.vbs", _
"e:\HackooTest\Folder2\VIRUS.vbs", _
"e:\HackooTest\Folder3\VIRUS.vbs", _
"e:\HackooTest\Folder4\VIRUS.vbs", _
"E:\HackooTest\Folder5\VIRUS.vbs", _
"E:\HackooTest\Folder6\VIRUS.vbs" _
)

For Each sFile In sFiles
	If fso.FileExists(sFile) Then
		sFileName = GetNewName(sFile)
		fso.MoveFile sFile,sFileName 
		Ws.Popup "Le fichier " & DblQuote(sfile) & " est déplacé vers le dossier de quarantaine comme " & DblQuote(sFileName),"4",Title,VbInformation
	Else
		Ws.Popup DblQuote(sfile) & " n'existe pas !","2",Title,VbExclamation
	End If
Next
'**********************************************************************************************
Function GetNewName(sFile)
	Dim snamebase,sname,i,sTarget
	snamebase = Split(Right(sFile, Len(sFile) - InStrRev(sFile,"\")),".")(0)
	sname = snamebase
	i = 0
	While i < 100
		sTarget = Quarantaine & "\" & sname & "_Infected.txt"
		If fso.FileExists(sTarget) Then
			i = i + 1
			sName = snamebase & "(" & i & ")"
		Else
			GetNewName = sTarget
			Exit Function
		End If
	Wend
End Function
'**********************************************************************************************
Function DblQuote(Str)
	DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to move file while renaming it with incrementation ?

Post by jvierra »

You should break yourself of the habit of creating nonsense utility funciotns. THey lead to errors andmake the code harder to understand:
VBScript Code
Double-click the code block to select all.
Ws.Popup """" & sfile & """ n'existe pas !","2",Title,VbExclamation
This eliminates many lines on unnecessary code and creates a very easy to understand line.
Why use Popup when MsgBox is all you need. It does the same thing. Popup is used for other things which you are not using.

Try to avoid adding "cute" code.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to move file while renaming it with incrementation ?

Post by jvierra »

As an example I have reformatted your version. This shows how white space is better at making code readable. Use of long lines of asterisk is considered very bad form as it adds what we call "visual noise". No one outside of the creator can see the code for the noise.

Use Const for constant strings. The convention for "Const" is to use all caps in the name.
VBScript Code
Double-click the code block to select all.
Option Explicit
Dim fso, sFiles, sFile, sFileName
Const TITLE = "Déplacement des fichiers suspects dans la quarantaine"
Const QUARANTAINE = "e:\HackooTest\Quarantaine"

Set fso = CreateObject("Scripting.FileSystemObject")
sFiles = Array( _
    "e:\HackooTest\Folder1\VIRUS.vbs", _
    "e:\HackooTest\Folder2\VIRUS.vbs", _
    "e:\HackooTest\Folder3\VIRUS.vbs", _
    "e:\HackooTest\Folder4\VIRUS.vbs", _
    "E:\HackooTest\Folder5\VIRUS.vbs", _
    "E:\HackooTest\Folder6\VIRUS.vbs" _
)
 
For Each sFile In sFiles
    If fso.FileExists(sFile) Then
        sFileName = GetNewName(sFile)
        fso.MoveFile sFile,sFileName 
        MsgBox( "Le fichier """ & sfile) & """ est déplacé vers le dossier de quarantaine comme """ & sFileName & """",vbOKOnly + vbInformation ,TITLE
    Else
        MsgBox """" & sFile & """ n'existe pas !", vbOKOnly + vbExclamation,TITLE
    End If
Next



Function GetNewName(sFile)
    Dim snamebase,sname,i,sTarget

    snamebase = Split(Right(sFile, Len(sFile) - InStrRev(sFile,"\")),".")(0)
    sname = snamebase

    i = 0
    While i < 100
        sTarget = Quarantaine & "\" & sname & "_Infected.txt"
        If fso.FileExists(sTarget) Then
            i = i + 1
            sName = snamebase & "(" & i & ")"
        Else
            GetNewName = sTarget
            Exit Function
        End If
    Wend

End Function
This topic is 9 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked