Batch file to move specific files and folders

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
krushant
Posts: 4
Joined: 19 Jul 2011 08:17

Batch file to move specific files and folders

#1 Post by krushant » 19 Jul 2011 08:36

Hi All,

I'm trying to create a batch file to move all the contents of a particular directory to a backup directory

Source directory: F:\Krushant\ABC
Destination backup directory: F:\Krushant\backup

ABC folder contents:

00112CC6
00112E5E
00112EA7
00112CC6.Job
00112E5E.Job
00112EA7.Job
Config.dat
ConSts01.bin
ConSts01.txt
Convert.dbg
FIleCopy.dbg

The one coloured in blue are junk folders

I want to move all the contents of the folder ABC to backup folder, except the 5 files marked in RED

Please suggest a batch file that can acchieve the above task

Its urgent... Please help!!!

Thanks,
Krushant

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Batch file to move specific files and folders

#2 Post by dbenham » 19 Jul 2011 09:51

The ROBOCOPY command has many options, including those you need to meet your requirements.

It is standard with Vista and later. If you have an earlier version of Windows it is available in a Microsoft resource kit.

Dave Benham

krushant
Posts: 4
Joined: 19 Jul 2011 08:17

Re: Batch file to move specific files and folders

#3 Post by krushant » 19 Jul 2011 09:55

Hi Dave,

This is a client side windows server 2003 environment.

Unfortunately, I won't able to download any resource kit in this server :(

Please advise if there's any other way we can do this???

Thanks in advance!!!

krushant
Posts: 4
Joined: 19 Jul 2011 08:17

Re: Batch file to move specific files and folders

#4 Post by krushant » 19 Jul 2011 10:10

Hi All,


Just to add:

I have devised a workaround for moving these files and folders.

I'm using the copy/paste->delete->copy back specific files technique


step 1: Copying all the contents of folder ABC to backup

XCOPY "F:\Krushant\ABC" "F:\Krushant\backup" /E /Y

Step 2: Deleting the contents of folder ABC, but not the folder ABC(The below code will take care of that)

cd "F:\Krushant\ABC"
rmdir "F:\Krushant\ABC\" /S /Q

Step 3:

This step will have the code to copy the 5 files above from backup folder back to source ABC folder

This will basically be similar to moving all folder/files


This workaround will definitely work, but I see a potential risk in copying back the 5 files

The 5 files seen above are business critical files.

Is there a risk of these files getting corrupt, while copying back to source folder from backup folder?

Please let me know your thoughts on this!!!


Thanks,
Krushant

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Batch file to move specific files and folders

#5 Post by dbenham » 19 Jul 2011 10:21

Do you want to move the directories as well :?:
Or do you want to replicate the directory structure and just move the files :?:

Never mind - your above post answers my questions

Dave Benham

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Batch file to move specific files and folders

#6 Post by dbenham » 19 Jul 2011 11:05

krushant wrote:Is there a risk of these files getting corrupt, while copying back to source folder from backup folder?
It seems like a slim chance, but I understand your concern. You also have to worry that there is a finite amount of time when the critical files are not where they are supposed to be.

I like your idea of starting with XCOPY, but why delete the entire source tree and then copy back? Instead you can selectively delete from the source in an efficient manner.

You could base the process on a file containing filenames that should not be moved. You can then easily update the list without having to touch your code.

keep.dat

Code: Select all

Config.dat
ConSts01.bin
ConSts01.txt
Convert.dbg
FIleCopy.dbg
keep.dat

This code is UNTESTED:

Code: Select all

@echo off
XCOPY "F:\Krushant\ABC" "F:\Krushant\backup" /E /Y
F:
cd "F:\Krushant\ABC"
for /f %%d in ('dir /b /ad') do rmdir /s /q %%d
for /f %%f in ('dir /b ^| findstr /vxi /g:keep.dat') do del %%f
::skip the next step if you want to keep the backup of your critical files
for /f %%f in (keep.dat) do del "F:\Krushant\backup\%%f"

My code assumes the KEEP.DAT is in your source tree. It would probably be more secure to put it in some other directory with your batch file and modify the FINDSTR command to reference the absolute path.

Dave Benham

krushant
Posts: 4
Joined: 19 Jul 2011 08:17

Re: Batch file to move specific files and folders

#7 Post by krushant » 20 Jul 2011 02:15

Thanks Dave... Let me try that... I'll get back to you soon. Thanks for your help !!!

Post Reply