Page 1 of 1

Move all files to a different direcotry except one file

Posted: 07 Aug 2014 07:59
by loc3loan
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.

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

Posted: 07 Aug 2014 08:30
by ShadowThief
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%
)

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

Posted: 07 Aug 2014 08:34
by loc3loan
It works very well. Thank you so much for your help.

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

Posted: 03 Oct 2014 07:37
by loc3loan
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.

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

Posted: 03 Oct 2014 08:42
by Squashman
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.

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

Posted: 03 Oct 2014 10:22
by loc3loan
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.

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

Posted: 03 Oct 2014 10:33
by ShadowThief
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%"
)

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

Posted: 03 Oct 2014 10:43
by Squashman
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.

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

Posted: 03 Oct 2014 11:13
by loc3loan
Thank you so much for your help. It is working now. Have a good weekend.