Move all files to a different direcotry except one file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

Move all files to a different direcotry except one file

#1 Post by loc3loan » 07 Aug 2014 07:59

Hello all,

I need some help to write an bat file that will move all the file to a different directory except for one file with the lastest date stamp? Thanks for your help.

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Move all files to a different direcotry except one file

#2 Post by ShadowThief » 07 Aug 2014 08:30

Try this, changing the source and target directory names for the ones you need. Note that this will also move any folders in the source directory.

Code: Select all

@echo off
cls

set "source_dir=C:\where\the\files\are"
set "target_dir=C:\where\the\files\will\go"

for /f "skip=1 delims=" %%A in ('dir %source_dir% /b /o-d') do (
   move "%source_dir%\%%A" %target_dir%
)

loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

Re: Move all files to a different direcotry except one file

#3 Post by loc3loan » 07 Aug 2014 08:34

It works very well. Thank you so much for your help.

loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

Re: Move all files to a different direcotry except one file

#4 Post by loc3loan » 03 Oct 2014 07:37

Sorry. It is only working for the folder that has no space in it. What do I need to do with a space in the source and target directory name? Thanks.

C:\Program Files (x86)\Microsoft SQL Server Compact Edition

Thanks.

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Move all files to a different direcotry except one file

#5 Post by Squashman » 03 Oct 2014 08:42

Any folders or file names that have spaces needs to have quotes around the entire path.
As you can see in the MOVE command there is quotes around the Source but not the target.

loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

Re: Move all files to a different direcotry except one file

#6 Post by loc3loan » 03 Oct 2014 10:22

Thanks for the reply. Here is the modified code, but it is still not working.

@echo off
cls

set "source_dir=F:\log 1"
set "target_dir=F:\log 2"

for /f "skip=1 delims=" %%A in ('dir %source_dir% /b /o-d') do (
move "%source_dir%\%%A" "%target_dir%"
)


Thanks.

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Move all files to a different direcotry except one file

#7 Post by ShadowThief » 03 Oct 2014 10:33

Not enough double quotes.

Code: Select all

@echo off
cls

set "source_dir=F:\log 1"
set "target_dir=F:\log 2"

for /f "skip=1 delims=" %%A in ('dir "%source_dir%" /b /o-d') do (
move "%source_dir%\%%A" "%target_dir%"
)

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Move all files to a different direcotry except one file

#8 Post by Squashman » 03 Oct 2014 10:43

loc3loan,
It is just good coding practice to always put quotes around all folder and file paths regardless of them having spaces or not. It does not hurt to have them. So when you see your program failing, you should turn ECHO ON and look at the output to see what the error is and what the batch file is executing. Regardless of that the number one rule is to just use "QUOTES" all the time.

loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

Re: Move all files to a different direcotry except one file

#9 Post by loc3loan » 03 Oct 2014 11:13

Thank you so much for your help. It is working now. Have a good weekend.

Post Reply