Link to home
Start Free TrialLog in
Avatar of AdrianWhitehurst
AdrianWhitehurstFlag for Afghanistan

asked on

Need script help

Hello,
Can someone point me in the right direction or provide a script that moves files from a local folder to a server folder? Once the script is written I will be using Task Scheduler to run every minute between 7am and 4pm.
 The local folder is on a Windows XP PC and needs to be moved to a folder on a 2008 server. The account I am using is a network admin account on both the PC and server. Both the PC and server are on the domain, and I can write to each location using my admin account. I can also map to each location.

I have tried writing a batch file;
move c:\mytest\*.* \\servername\folder1\folder2\folder3
pause

But it returns the following error:
The filename, directory name, or volume label syntax is incorrect

I have tried powershell, but found out that "move-item" will only move files on the same volume, not across multiple volumes.

I have tried robocopy:
robocopy c:\mytest U:\ /mov /R:1 /W:1 /LOG:log1.log
pause

I have also tried with /move
In both instances it will copy the files successfully, but it will not delete the source files when done.

Any help someone could provide would be helpful.  

By the way, I have searched for VB Scripts but am unable to understand the scripting language., so I have not tested much with the ones I have found out of fear that it might be doing more in the background that I need it to.

PLEASE HELP!

Thank you
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

It sounds like maybe you just have spaces in the folder names?  if so add " " around them, i.e.

move "c:\my test\*.*" "\\servername\share\folder 1\folder 2"

If you are trying to copy to a mapped drive and then schedule the task the mapped drive won't be there for that scheduled job, unless you specifically map it with "net use x: \\server\share" for instance first.

Please let us see the actual paths and bacth lines used if this isn't it.

Steve
VBScript won't do anything in the background btw, just exactly what you tell it to do in the order you write it.  Having said that for something like this it should be one MOVE command or half a dozen lines in VBScript.
You can try this script, if help you:
Dim fso, sfolder, dfolder, folder, files

Set fso = CreateObject("Scripting.FileSystemObject")
sfolder = "C:\Temp\"
dfolder = "U:\"
Set folder = fso.GetFolder(sfolder)
Set files = folder.Files
  
For each folderIdx In files
	fso.MoveFile sfolder & folderIdx.Name, dfolder & folderIdx.Name
Next

Open in new window

Avatar of AdrianWhitehurst

ASKER

@Dragon-It:
I accidentally put a space between the My and Test when submitting the question. Sorry about tht

The actual path is:
move c:\mytest\*.* \\server1\folder1\folder2\MyTest
pause
@ X_Layer:

I copied/pasted your script into a notepad and saved it as a .vbs file.
I ran it, I get no errors, but nothing happens.

I have even tried changing "U:\" to the full server/folder path with quotes

Both times, nothing happened =(

Thanks for your script though.
SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@ dragon-it:
Thank you. I ran the script and it created the txt file then moved it successfully to the destination server

This lets me know that the move command will work for what I need.  Now I just have to figure out how to get it to move the folder/files under c:\mytest to the destination folder

Below is the output after running the batch file:

Volume in drive C has no label.
 Volume Serial Number is 6424-B11C

 Directory of c:\mytest

10/10/2011  02:06 PM    <DIR>          .
10/10/2011  02:06 PM    <DIR>          ..
10/10/2011  02:06 PM    <DIR>          folder1
10/10/2011  02:06 PM    <DIR>          folder2
10/10/2011  02:06 PM    <DIR>          folder3
10/10/2011  02:06 PM    <DIR>          folder4
10/10/2011  02:06 PM    <DIR>          folder5
               0 File(s)              0 bytes
               7 Dir(s)  27,579,375,616 bytes free
 Volume in drive \\server1\folder1 is FileData
 Volume Serial Number is 3498-7A22

 Directory of \\server1\folder1\folder2\MyTest

10/10/2011  04:22 PM    <DIR>          .
10/10/2011  04:22 PM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)   9,885,388,800 bytes free

Thanks for your help on this
@dragon-it:
I just reread what you wrote, "unless there are no files in that path"

There is indeed no files in that directory. It contains folders with files in each folder.
I am needing to move the subfolders of c:\mytest to the destination folder.

To test the batch file I orginally ran:
move c:\mytest\*.* \\fnic-rtl-jax\CMMain\CMData17\MyTest
pause

I put 10 copies of the txt log file in under c:\mytest and ran the batch file.  It worked!

Is there an additional command or switch I can add to the script to have it move the subfolders too?
Hmm, my script was for moving all files in source folder to destiantion folder. Look like that you have subfolders which are needed to be moved. Here is modified script:
Dim fso, sfolder, dfolder, objfolder, subfolders

Set fso = CreateObject("Scripting.FileSystemObject")
sfolder = "C:\Temp\"
dfolder = "U:\"
Set objfolder = fso.GetFolder(sfolder)
Set subfolders = objfolder.SubFolders
  
For each folder In subfolders
	fso.MoveFile folder, dfolder
Next

Open in new window

Try this and report back.
You could use move with the directory name... but that loses the top level dir then.

You could also try and move all subdirs with something like this to get all the subdirs under c:\mytest.

@echo off
cd /d C:\mytest
for /d %%D in (*) do move "%%D" "\\fnic-rtl-jax\CMMain\CMData17\MyTest"

hth.. bed calls now!

Steve
@ X_layer:

I created a vbs file using your script.
I changed "c:\temp" to "c:\mytest"
I ran the script and got the following error:

Line: 10
Char: 9
Error: File not found
Code: 800A0035
Source: Microsoft VBScript runtime error

I tried replacing "u:\" with "\\server1\folder1\folder2\MyTest\" but got the same result
Avatar of Bill Prew
Bill Prew

Are you trying to move just the files immediately under the c:\mytest folder, or also the files in the subfolders below it?

~bp
@billprew:
The subfolders and the files within those subfolders
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, my mistake:
fso.MoveFile folder, dfolder

Open in new window

should be
fso.MoveFolder folder, dfolder

Open in new window

BTW if you are hoping to move all the files under these dirs and then remove them, what is happening with the files on the destination?  i.e. are those directories then just building up.  If so and the subdirectory names aren't unique then using Move may not be right option.

Instead robocopy as suggested or a combination of xcopy & del.

e.g. if you need to ensure files are only copied and deleted as a snapshot:

We could take a dir of all the files in the subdirs, copy them and then delete them based on that dir listing.

Do empty directories need removing.

Steve
@X_layer:
I changed it to fso.MoveFolder as you instructed.  Now I am getting a permissions denied.  Below is the error I am getting and below that is the code that I have in my VBSscript.

Error:
Line: 10
Char: 9
Error: Permissions denied
Code: 800A0046
Source: Microsoft VBScript runtime error



Dim fso, sfolder, dfolder, objfolder, subfolders

Set fso = CreateObject("Scripting.FileSystemObject")
sfolder = "C:\mytest\"
dfolder = "\\server1\Folder1\folder2\MyTest\"
Set objfolder = fso.GetFolder(sfolder)
Set subfolders = objfolder.SubFolders
 
For each folder In subfolders
        fso.MoveFolder folder, dfolder
Next
@Adrian:
I'd like to clear something out, if you don't mind.

You want to move all files from "c:\mytest\*.*" to "\\servername\folder1\folder2\folder3". Correct?

Assuming that you have full access (like you say), the only reason (disclaimer here) why it may not work could be because "\folder1...\folder3" does not exist, or has some spaces in them.

Try this following command line:
ECHO test>"\\servername\folder1\folder2\folder3\Test.txt"

Now, does this create the test.txt file to "\\servername\folder1\folder2\folder3\Test.txt"?

If the root destination folders are dynamically created (like for example by the current date..., user or computer name), you must create them before moving files.

For example:
SET Dest=\\servername\folder1\folder2\folder3\Test.txt
if not exist "%Dest%" md "%Dest%"
move "c:\my test\*.*" "%Dest%"

Am I leading to some clues here?

By the way, if I'm am leading to the solution, please split points with my fellow experts.

Cheers,
Rene
@ReneGe:

I created the first batch file as you provided. I edited the batch file so that it was c:\MyTest instead of c:\My Test
When I ran the batch file, the txt file was created in the destination folder

I created a second batch file using the example you provided, editing the server path info as needed.
When I ran this batch file, I received the following error:


C:\Documents and Settings\username\Desktop>Set Dest=\\servername\Folder1\Folder2\MyTest\test.txt

C:\Documents and Settings\username\Desktop>if not exist "\\servername\Folder1\Folder2\MyTest\test.txt" md "\\servername\Folder1\Folder2\MyTest\test.tx
t"

C:\Documents and Settings\username\Desktop>Move "c:\mytest\*.*" "\\server1\Folder1\Folder2\MyTest\test.txt"
The process cannot access the file because it is being used by another process.
        0 file(s) moved.

C:\Documents and Settings\username\Desktop>pause
Press any key to continue . . .


The first time I got this error, I restarted the source computer
I got the error again after I rebooted.

I restarted the destination server, for other reasons, before running this batch file the second time.


Did you get a chance to try the option in http:#36945642 for moving just the subdirs?

Steve
Or robocopy ?

~bp
@Dragon-it:
I apologize, I thought I had responded to that message.  I get the following output when I run the script you provided:
Access is denied.
Access is denied.
Access is denied.
Access is denied.
Access is denied.
Press any key to continue . . .

I am going to revisit my permissions but I am 99% sure that I have full access.  I have went as far as adding the account I am testing with individually so that the account I use has modify access.
The other script that writes a txt file to the destination folder works.  It will create the necessary txt file. It just doesn't seem to like sub-folders.

So far with all of the scripts that everyone from this post has provided has proved that I can write to the destination folder, I can write a txt file to the designation folder, I can move any file type to the destination folder, but it will absolutely not touch sub-folders. =(
@BillPrew:
From my posting:

I have tried robocopy:
robocopy c:\mytest U:\ /mov /R:1 /W:1 /LOG:log1.log
pause

I have also tried with /move
In both instances it will copy the files successfully, but it will not delete the source files when done.
@BillPrew:
Sorry, that was meant to say, ..from my first posting
I have well rested and found that my second script should have been the following.


ECHO OFF

ECHO CREATING DESTINATION FOLDER
	REM SET Dest=\\servername\folder1\folder2\folder3
	SET Dest=%~dp0TestDest
	IF not EXIST "%Dest%" MD "%Dest%"

ECHO CREATING LOCAL TEST FILE AND FOLDER
	SET Source=%~dp0TestSource
	IF not EXIST "%Source%" MD "%Source%"
	ECHO TEST>"%Source%\test.txt"

ECHO MOVING FILE FROM SOURCE TO DEST
	MOVE "%Source%\*.*" "%Dest%"

ECHO LISTING WHAT IS IN THE DEST FOLDER
	DIR /b "%Dest%"

ECHO.
PAUSE
EXIT

Open in new window

Please alternate REM from line 4 and 5 as:
SET Dest=\\servername\folder1\folder2\folder3
REM SET Dest=%~dp0TestDest
Did you verify that you have permissions on server to create folders?
ok i think you need to do to start with is get to be able to move the folders in ecplorer gui.... Once you can do that majority of these scripts will do the stuff by the sounds of it.

Steve
@Steve:
That's what I tried to do, but in a controlled environment.

@AdrianWhitehurst:
What dragon-it suggested is indeed the most simple test you can do that will confirm if you have the required access to the remote folder.

I also am currious, if you might be running the batch file from a scheduled task with different credentials.

Cheers,
Rene
nad this is run on the xp box... Not win6 where uac could come into play?

Steve
and this is run on the xp box... Not win6 where uac could come into play?

Steve
durgh win7 of course ... I suppose win 6 was the horror that was vista...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@BillPrew
I have used your script, AND it works with one exception.
It actually moves the sub-folders and files, but unfortunately it deletes the source folder (C:\MyTest) which is needed for future batch files to be dumped into.

I have updated the script to be:

robocopy c:\mytest\ U:\ /MOVE /E /R:1 /W:1 /LOG:log1.log
MD c:\MyTest
pause

Although this works, this will mean that the user will have to run a batch, execute the batch file, and then proceed with the next batch to be dumped into the source folder.

My end goal is to have it automated using task scheduler and without the source folder being deleted. I need the Task Scheduler to run the created batch file so that the computer looks inside the source folder and move only the sub-folders and the files within the sub-folders to the destination folder.

Thank you very much for helping me to get closer to the needed script.  I at least now have something that the users can execute without having to manually copy the generated batches from the local PC to the server.

Is there another switch I can use to prevent robocopy from deleting the source folder?
@ReneGe
I will be trying your script a little later today. I will let you know once I have ran it so that you know the outcome.

@Dragon-it
I am currently testing this batch file from a Windows XP PC.  The destination folder is on a Windows 2008R2 Server SP1
I am executing the batch file from my desktop at the moment while logged in with an account that has modify rights on the destination folder.
Once I have the batch file successfully running, I will be using Task Scheduler to run every two minutes between 7am-4pm M-F using a service account that has access to the source and destination folders.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@X_Layer
THANK YOU!  That did it! By telling robocopy to put the log file in the C:\MyTest folder, it prevented the source folder(C:\MyTest) from being deleted.

I apologize if I did not divide the points up correctly(If I didn't, please let me know so that I do it correctly next time), but here is how I did it. I gave the largest amount to the Best Solution, the 2nd largest to BillPrew for getting extremely close to the solution and divided the rest of the points up for the comments that led me closer to the solution.

Thanks to all of you who helped and thank you for your patience with me.
BTW - Below is the final code that I used:

robocopy C:\MyTest U: /MOVE /E /R:2 /XF "placeholder.txt" /LOG+:C:\MyTest\movelog.txt
pause
Glad you got to something that works for you, thanks.

~bp