Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Batch file or Script for moving specific files and folders to another location

Posted on 2010-08-27
33
Medium Priority
?
566 Views
Last Modified: 2012-05-10
Hello,
I looking to write a batch or script to run at system system startup, which will go into the directory %userprofile%desktop\ and move all the files and folders and subfolders contained in there to \\servername\sharename\orphans\ and if it already contains a file or folder of the same name, renames the file or folder so it can moved. But I would like to exclude files from being moved with the *.lnk extension.

Your help would be mostly appreciated. Many Thanks in advance
0
Comment
Question by:bilbazafa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 18
  • 7
  • 5
  • +1
33 Comments
 
LVL 63

Expert Comment

by:SysExpert
ID: 33543703
0
 
LVL 63

Expert Comment

by:SysExpert
ID: 33543805
also xcopy can be used to copy all but the excluded files ( has exclude option ), and then do a delete of all

I would suggest something like moving the lnk files to a temp location, move all the rest, and then move the lnk files back


I hope this helps !
0
 

Author Comment

by:bilbazafa
ID: 33544917
So far from the info you have sent me, I have been testing this:-

@echo off
cd\ %userprofile%desktop
attrib +r +s *.lnk
move /Y *.* "c:\temp"
attrib -r -s *.lnk

It works great but how do I move Folders and not just Files also?
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 63

Expert Comment

by:SysExpert
ID: 33545413
That's why I suggested a copy ( xcopy ) , and then a delete since xcopy and robocopy will both handle files and directories, while Move will not.

Other option is to use a For loop and parse through  each subdirectory



0
 

Author Comment

by:bilbazafa
ID: 33545915
My appologise, or course yes I've been reading about this command. I'll give it a go thank you
0
 

Author Comment

by:bilbazafa
ID: 33586346
OK,

So far I have this test batch file and it works great:-

@echo off
:: variables
set drive=\\management\UserDocs\JUNK
set backupcmd=xcopy /s /c /e /i /r /y
/EXCLUDE:\\servername\SYSVOL\company.co.uk\scripts\Exclude.txt

echo ### Copying Desktop Content...
%backupcmd% "%USERPROFILE%\Desktop" "%drive%\Desktop Content"

echo ### Hiding Desktop Shortcut files...
attrib +r +s %userprofile%\Desktop\*.lnk

echo ### Deleting Desktop Content...
del %userprofile%\Desktop\*.* /S /Q

echo ### Unhiding Desktop Shortcut files...
attrib -r -s %userprofile%\Desktop\*.lnk

The only problem I have is that it leaves empty folders on the users Desktop. If I use the RD command it tries and deletes the actual Desktop folder itself and not to mention it deletes all the shortcut .lnk files!!!

Any sugestions?

Many Thanks
0
 

Author Comment

by:bilbazafa
ID: 33587541
Tried using Robocopy with the /MOVE command but the folders still stay on the Desktop??

Sigh.....
0
 
LVL 63

Expert Comment

by:SysExpert
ID: 33589431
You may need to add a  separate cleanup command that deletes the folders on the desktop
0
 

Author Comment

by:bilbazafa
ID: 34075704
Would you be able to give me an example please?

Thanks
0
 
LVL 63

Expert Comment

by:SysExpert
ID: 34089793
as an example, desktop is actually located somewhere like this

C:\Documents and Settings\admin\Desktop  - if admin is the user.

So using a batch file you could easily delete folders o the desktop

RD /s /q C:\Documents and Settings\admin\Desktop\foldername

could be added to the batch file or similar to delete folders

 rd /?  for options
0
 

Author Comment

by:bilbazafa
ID: 34091407
Would I be able to change it to lets say:-

RD /s /q C:\Documents and Settings\admin\Desktop\*.*

So it will delete any folder on the Desktop?
0
 

Author Comment

by:bilbazafa
ID: 34091648
Apparently the RD command removes the Root folder also, so I cannot see this command working.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 34225513
There are two ways to prevent RD from removing the root folder:
change into that folder first, and do a   rd . /s/q 2>nul   . Since the folder is in use, it cannot be removed, resulting in an error, but all subfolders are removed at that time.
remove each folder by a FOR loop
for /D %%D in ("%UserProfile%\..\admin\Desktop") do rd "%%~D" /s/q

Open in new window

0
 

Author Comment

by:bilbazafa
ID: 34354312
I'll give it a go,

Thanks
0
 
LVL 8

Expert Comment

by:Here2Help
ID: 34362409
Hey.

Just stumbled across this app 'Sync' looks promising.
http://syncdir.sourceforge.net/

Copy sync.bat and sync.jar to an appropriate location.  Edit Sync.bat as needed.
0
 

Author Comment

by:bilbazafa
ID: 34471512
Thanks Here2Help
0
 

Author Comment

by:bilbazafa
ID: 34473707
Hi Qlemo,

You state that there are 2 ways of achieving what I am after. The example above, which one of the 2 is it?

Thanks
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 34473812
Do you mean the code snippet I provided? That one belongs to the 2nd option, "remove each folder by a FOR loop".
0
 

Author Comment

by:bilbazafa
ID: 34481418
Qlemo so far I have this code below. I know how to move the files as you know it's how I delete them all afterwards including the folder on the users Desktop. So how can I adapt your coding the delete all the files and folders on the users Desktop?

echo ### Hiding Desktop Shortcut files...
attrib +r +s %userprofile%\Desktop\*.lnk

REM echo ### Deleting Desktop Content...
REM del %userprofile%\Desktop\*.* /S /Q
rd %userprofile%\Desktop\*.* /q

echo ### Unhiding Desktop Shortcut files...
attrib -r -s %userprofile%\Desktop\*.lnk
0
 

Author Comment

by:bilbazafa
ID: 34481466
Sorry I have forgotten to remove the remark off the fifth line.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 34482325
That line is not necessary. RD removes the files in all folders when called with /s. So the first option (changing into the root folder) would look like this:
pushd %userprofile%\Desktop
echo ### Hiding Desktop Shortcut files...
attrib +r +s *.lnk

REM echo ### Deleting Desktop Content...
REM del %userprofile%\Desktop\*.* /S /Q
rd . /s /q

echo ### Unhiding Desktop Shortcut files...
attrib -r -s *.lnk
popd

Open in new window

0
 

Author Comment

by:bilbazafa
ID: 34482727
Brill thanks it's working!!

One thing when it runs the commands:-

echo ### Hiding Desktop Shortcut files...
attrib +r +s *.lnk

echo ### Unhiding Desktop Shortcut files...
attrib -r -s *.lnk

It says " File not found - *.lnk " But it works and does not delete the users shortcuts. Why is this?

Thanks
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 34483032
I had to reread the thread to get what you really are looking for ;-).
Those attrib commands are useless anyway now. RD will ignore the attributes, and still delete the links. Your links are not deleted only because they are not in the user's profile, but probably in the "all users" one.
If you really need to maintain the .lnk files (and I think you should) then you have to use the second method; it replaces all the stuff related to deleting:
for /D %%D in ("%UserProfile%\Desktop") do rd "%%~D" /s/q

Open in new window

0
 

Author Comment

by:bilbazafa
ID: 34483432
Yes Qlemo your right, silly me I should of thought of that, it's been a long day!

Ok I've re-done the code with what you have said, but it still deletes the shortcuts I've now placed on the test Desktop and also displays the " File not found - *.lnk "

@echo off
pushd %userprofile%\Desktop
echo ### Hiding Desktop Shortcut files...
attrib +r +s *.lnk
echo ### Deleting Desktop Content...
for /D %%D in ("%UserProfile%\Desktop") do rd "%%~D" /s/q
echo ### Unhiding Desktop Shortcut files...
attrib -r -s *.lnk
popd
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 34483817
Sorry, I should have added a wildcard to the for. And, as said, you need to replace ALL "deleting" parts, and we do not need the pushd/popd anymore:
@echo off
echo ### Deleting Desktop Content...
for /D %%D in ("%UserProfile%\Desktop\*") do rd "%%~D" /s/q

Open in new window

0
 

Author Comment

by:bilbazafa
ID: 34484804
Thanks Qlemo,

The last code works fab, but it only deletes the folders and their contents on the user’s Desktop and not the files I put on there? Do we need to put back another bit of coding and do that separately?
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 34485827
Never thought there could be any other files as links on the desktop ... . Yes, we need to (re-)implement a separate logic then for those files. Put all together:
@echo off
pushd %UserProfile%\Desktop
echo ### Deleting Desktop Folders ...
for /D %%D in (*) do rd "%%~D" /s/q
echo ### Removing anything but links
attrib -r *
attrib +r *.lnk
del /a:-r /q *
attrib -r *.lnk
popd

Open in new window

0
 

Author Comment

by:bilbazafa
ID: 34489200
Nice one Qlemo it works perfect! If I also wanted to stop .rpd files from being deleted off the users Desktop would I do the following:-

@echo off
pushd %UserProfile%\Desktop
echo ### Deleting Desktop Folders ...
for /D %%D in (*) do rd "%%~D" /s/q
echo ### Removing anything but links
attrib -r *
attrib +r *.lnk
attrib +r *.rdp
del /a:-r /q *
attrib -r *.lnk
attrib -r *.rdp
popd
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 34489325
Exactly.
0
 

Author Closing Comment

by:bilbazafa
ID: 34489361
Awesome Thanks!!
0
 

Author Comment

by:bilbazafa
ID: 35314242
Qlemo, may I ask another question regarding the code you wrote for me? How would I incorporate the code so it would also delete files and sub folders in a directory called C:\CAD Temp

@echo off
pushd %UserProfile%\Desktop
echo ### Deleting Desktop Folders ...
for /D %%D in (*) do rd "%%~D" /s/q
echo ### Removing anything but links
attrib -r *
attrib +r *.lnk
attrib +r *.rdp
del /a:-r /q *
attrib -r *.lnk
attrib -r *.rdp
popd

Many Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question