Video Walkthrough:
Try using the code in order to make sure you understand.
So this tutorial is about copying, moving, and renaming files and folders using VbScript.
Below is the format of the script to copy and to move the files and folders. Note that the root command is CreateObject(“Scripting.FileSystemObject”):
Option Explicit Dim fso Set fso = CreateObject("Scripting.FileSystemObject") 'To Copy a file fso.CopyFile "LOCATION", "NEW LOCATION" 'To Copy a folder fso.CopyFolder "LOCATION", "NEW LOCATION" 'To Move a file fso.MoveFile "LOCATION", "NEW LOCATION" 'To Move a folder fso.MoveFolder "LOCATION", "NEW LOCATION"
You put the current address of the file/folder where it says “LOCATION”, and put the new address where you want to move or copy the file/folder where it says “NEW LOCATION”.
Table of Contents
Copy File into Folder
For example, let’s say I want to copy my picture on the desktop to a new folder on the desktop
The address of either folder or pic can be found by right clicking either folder or picture and selecting properties. Find the location descriptor.
Insert the locations into the code:
Option Explicit Dim fso Set fso = CreateObject("Scripting.FileSystemObject") fso.CopyFile "C:\Users\rock\Desktop\BigEyeCat.jpg", "C:\Users\rock\Desktop\TestFolder\"
Note that the picture ends with .jpg, and the ending location must end with a backslash \
Move Folder into Folder
Let’s say I want to move a folder into another folder.
Both are on the desktop “C:\Users\rock\Desktop”. So all you have to do is add the folder names.
Option Explicit Dim fso Set fso = CreateObject("Scripting.FileSystemObject") fso.MoveFolder "C:\Users\rock\Desktop\TestFolder", "C:\Users\rock\Desktop\TestFolder2\"
Again, the ending location must end with a backslash \
Output:
Rename File or Folder
Video Walkthrough:
Renaming a file or folder would use the move command, since the copy command causes there to be duplicates.
To rename a file, let the NEW LOCATION be a file with a different name.
fso.MoveFolder "C:\Users\rock\Desktop\TestFolder\BigEyeCat.jpg", "C:\Users\rock\Desktop\TestFolder2\FunnyCat.jpg"
To rename a folder, don’t add the backslash \ at the end of the NEW LOCATION and have a different name for the folder
fso.MoveFolder "C:\Users\rock\Desktop\TestFolder", "C:\Users\rock\Desktop\TestFolder3"
Move or Copy Multiple Files into Folder
If you only want to move the all the files, but not the folder use *.* or *
fso.MoveFile "C:\Users\rock\Desktop\TestFolder\*.*", "C:\Users\rock\Desktop\TestFolder2\" fso.CopyFile "C:\Users\rock\Desktop\TestFolder\*.*", "C:\Users\rock\Desktop\TestFolder2\"
If you only want to move all the files of a specific type use * plus the extension (i.e. *.txt or *.jpg)
fso.MoveFile "C:\Users\rock\Desktop\TestFolder\*.jpg", "C:\Users\rock\Desktop\TestFolder2\" fso.CopyFile "C:\Users\rock\Desktop\TestFolder\*.jpg", "C:\Users\rock\Desktop\TestFolder2\"
If you only want to move all the files of a specific name, regardless of type, use * after the filename and period (ie. catpic.* or file.*)
fso.MoveFile "C:\Users\rock\Desktop\TestFolder\BigEyeCat.*", "C:\Users\rock\Desktop\TestFolder2\" fso.CopyFile "C:\Users\rock\Desktop\TestFolder\BigEyeCat.*", "C:\Users\rock\Desktop\TestFolder2\"
Example: Check Before Copy/Move
To check if a folder or file exists before moving or copying a file/folder:
Option Explicit dim fso Set fso = CreateObject("Scripting.FileSystemObject") If fso.FolderExists("C:\Users\rock\Desktop\TestFolder2") then fso.CopyFolder "C:\Users\rock\Desktop\TestFolder2", "C:\Users\rock\Desktop\TestFolder3\" Else wscript.echo "doesn't exist" End If
Remember that the placement of the backslash \ affects how the VbScript reads & runs the code.
Is it possible to do multiple commands?
fso.MoveFile “C:\Users\rock\Desktop\TestFolder\BigEyeCat.*”, “C:\Users\rock\Desktop\TestFolder1\”
fso.MoveFile “C:\Users\rock\Desktop\TestFolder2\BigEyeCat2.*”, “C:\Users\rock\Desktop\TestFolder2\”
fso.MoveFile “C:\Users\rock\Desktop\TestFolder\BigEyeCat3.*”, “C:\Users\rock\Desktop\TestFolder3\”
I believe so.
I am using the method above mentioned to copy files to a USB card.
After issuing this command I then issue a command to eject the USB card.
The eject fails because the USB card is in use.
Is there a way to detect when the copy is completed?
Regards,
André
Hello, i need to move specified file Statistic-C1.txt but only when the file is older then 14 days. now i have this but when i wanna put file path into the code it shows me error.
Dim fso, f, f1, fc, strComments, strScanDir
strDir = “d:\test_bat\”
strDays = 14
Arc_Folder = “d:\presun\”
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set f = fso.GetFolder(strDir)
Set fc = f.Files
For Each f1 in fc
If DateDiff(“d”, f1.DateCreated,Now) > strDays Then
fso.movefile F1, arc_folder
End If
Next
Done = msgbox(“Záloha dokončena”,vbokonly)
WScript.Quit
Don’t know if this is still live but is there a way to do this on a folder that needs admin rights to rename? I keep getting a permission denied error. Some way to pass credentials through a VBscript.