basic script help

Soldato
Joined
22 Mar 2009
Posts
7,754
Location
Cornwall
ok, not sure if this section is exclusive for web stuff or not. so if it is, I apologise.

basically, I want some way to automatically cut files/folders from 1 folder and paste it to another (renaming if duplicate).

is this possible?
 
so far I have
Robocopy c:\test 1\ c:\test 2\ *.* /move /e

this one errors on me, due to there being a space in the folder location. if I rename the folders without spaces and change the command to match it works fine.

ok, so here is my new bat file
Robocopy f:\dad\pictures f:\dad\picutres *.* /move /e
Robocopy f:\dad\music f:\dad\music *.* /move /e
Robocopy c:\dad\ f:\dad\ *.* /move /e

now this is what I envisage happening.

in the 'temp' sync folders I will have a 'pictures' and a 'music' folder. so this command will mean everything dad puts in 'pictures' on f (temp) will move to 'pictures' on backup (c) and the same with 'music', but still leaving the 'pictures' and 'music' folders in f (temp) folder so he can easily upload to those again. it will also then catch anything he does not stick in these folders and move them completely from f (temp) and only be visable on c (backup)

does this sound right?

so 2 things.
1. how do I get it to see spaces
2. if there is a file with the same name, is there a command to automatically name the file/folder to 'test(1).txt' 'test(2).txt' etc
 
Last edited:
To get around the space issue just "quote" your paths. As for the other you'd need to wrap some code around it. Not got time at the moment but if no one else comes along I'll see if I can knock something up.
 
To get around the space issue just "quote" your paths. As for the other you'd need to wrap some code around it. Not got time at the moment but if no one else comes along I'll see if I can knock something up.

thanks for the offer, I have got the batch file to do the copying without any problems (although I ditched spaces as I couldn't get it work even with ""). but im still unsure about how to get around the duplicate filename issue.
 
you're backing up files with the same names, are they not the same files? if so why would you want to duplicate them. If you did that each time it ran you would wind up with another file with a different name.

Am I misunderstanding why you would want to do this?
 
you're backing up files with the same names, are they not the same files? if so why would you want to duplicate them. If you did that each time it ran you would wind up with another file with a different name.

Am I misunderstanding why you would want to do this?

lol no I don't want to keep backing up the same file, but say someone saved a picture called background.jpg. and then saved another pictured called background.jpg (a different pic but same name) then they would lose the original background.jpg. same with txt/doc...files
 
No it should back up the directory structure etc. As you can't have duplicate file names in a folder you will never hit this problem surely?

no, because the script moves and not copies. therefore they have no structure on their pc other than the empty files. so when it syncs it would move it from their 'pictures' folder (which would only have the new backup.jpg) to the backup pictures folder (which would have every picture, including the other backup.jpg, they have ever put in their pcs shared folder) therefore it would see the other backup.jpg and would need to rename it, else the batch file would stop until user intervention server end.

robocopy h:\Mesh\David\ g:\David\ *.* /mov /e

robocopy h:\Mesh\Edd\ g:\Edd\ *.* /mov /e

robocopy h:\Mesh\Dad\ g:\Dad\ *.* /mov /e



mkdir h:\Mesh\David\Documents

mkdir h:\Mesh\David\Music

mkdir h:\Mesh\David\Pictures

mkdir h:\Mesh\David\Video

mkdir h:\Mesh\David\Software

mkdir h:\Mesh\David\Other


mkdir h:\Mesh\Edd\Documents

mkdir h:\Mesh\Edd\Music

mkdir h:\Mesh\Edd\Pictures

mkdir h:\Mesh\Edd\Video

mkdir h:\Mesh\Edd\Software

mkdir h:\Mesh\Edd\Other


mkdir h:\Mesh\Dad\Documents

mkdir h:\Mesh\Dad\Music

mkdir h:\Mesh\Dad\Pictures

mkdir h:\Mesh\Dad\Video

mkdir h:\Mesh\Dad\Software

mkdir h:\Mesh\Dad\Other
 
Last edited:
If I've got the general idea of what you want it to do, I think this does the job.

Code:
strSource = "C:\SourceFolderName\"
strDest = "C:\DestinationFolderName\"
i = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strSource) And objFSO.FolderExists(strDest) Then
	Set folder = objFSO.GetFolder(strSource)
	For Each file In folder.files
	If objFSO.FileExists(strDest & file.Name) Then
		Do While objFSO.FileExists(strDest & Left(file.Name, InStrRev(file.Name, ".") - 1) & "(" & i & ")" & Mid(file.Name, InStrRev(file.Name, ".")))
			i = i + 1
		Loop
		objFSO.MoveFile file.Path, strDest & Left(file.Name, InStrRev(file.Name, ".") - 1) & "(" & i & ")" & Mid(file.Name, InStrRev(file.Name, "."))
	Else
		objFSO.MoveFile file.Path, strDest & file.Name
	End If
	Next
	WScript.Echo "Done"
Else
	WScript.Echo "Folders Missing"
End If
WScript.Quit

Just save with extension .vbs and double click to run
 
Last edited:
Back
Top Bottom