Move files from one folder to another

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.
Locked
User avatar
operez
Posts: 14
Joined: Tue Aug 09, 2011 12:04 am

Move files from one folder to another

Post by operez » Wed Jan 25, 2012 8:01 am


I need to move files from one folder to another, but I want to check to see if the file already exist in the new folder and if it does, I want to rename the file before I move it, so that I have all copies of that file.
I plan on moving each file to a folder name based on the file's extension.

All PDFs into a folder named PDF, all DOCs into a folder named DOC, etc.

I already have a vbscript to list all of the files in a folder and all its subfolders, just need to create a function to move the files and create the subfolder if it doesn't already exist.
Can someone please help me or direct me to an example?

User avatar
operez
Posts: 14
Joined: Tue Aug 09, 2011 12:04 am

Move files from one folder to another

Post by operez » Wed Jan 25, 2012 8:01 am


I need to move files from one folder to another, but I want to check to see if the file already exist in the new folder and if it does, I want to rename the file before I move it, so that I have all copies of that file.
I plan on moving each file to a folder name based on the file's extension.

All PDFs into a folder named PDF, all DOCs into a folder named DOC, etc.

I already have a vbscript to list all of the files in a folder and all its subfolders, just need to create a function to move the files and create the subfolder if it doesn't already exist.
Can someone please help me or direct me to an example?

User avatar
jvierra
Posts: 13275
Joined: Tue May 22, 2007 9:57 am
Contact:

Move files from one folder to another

Post by jvierra » Wed Jan 25, 2012 9:31 am

Look into the Scripting.FileSystemObject. It can get you all of the pieces you need to determine the extension of teh file name and the exitence of files and folders.
http://msdn.microsoft.com/en-us/library ... s.71).aspx

Consider using PowerShell as it is better at doing this.




User avatar
jvierra
Posts: 13275
Joined: Tue May 22, 2007 9:57 am
Contact:

Move files from one folder to another

Post by jvierra » Thu Jan 26, 2012 4:40 am

To rename a file withthe FSO use the MoveFIle method or the Move method of the file object.


User avatar
operez
Posts: 14
Joined: Tue Aug 09, 2011 12:04 am

Move files from one folder to another

Post by operez » Thu Jan 26, 2012 9:42 am

Thanks jvierra, I have the FileExist and Move down, it's the logic to check if I already have a renamed file in the Target folder and figuring out the version number so that I can increment the file number.

Example, if FileName(4).ext already exit, rename the next one FileName(5).ext

Hope someone can help me out.

Locked