move files excluding *.log using a batch file
I want to move files from one directory to anoth开发者_运维问答er excluding *.log file. I have tried XCOPY as it has a EXCLUDE switch but it does COPY not MOVE, files still remain as source folder.
If you can use ROBOCOPY, try this:
ROBOCOPY C:\Source C:\Dest * /MOV /XF *.LOG
To get you started, try this bat
for %%a in (%1\*) do (
if /I not "%%~xa"==".log" (
ECHO move /Y %%~fa %2\%%~nxa
)
)
After careful testing, remove the ECHO
command.
For more information, read HELP FOR
, HELP IF
and for the syntax used to extract the extension from the filename, read HELP CALL
.
Without RoboCopy, you can get close in batch. This will take a passed wildcard, run dir on it, then remove entries in the directory list ending in .log. It then calls move on each one. Since we're moving each file individually, we need to make sure the destination exists and is a folder - otherwise move might interpret it as a move and rename, instead.
Note that what dir returns may not be what you expect if you pass it a single folder name specifically. All wildcards should work fine, though.
@if ("%~1") == ("") goto usage
@if ("%~2") == ("") goto usage
@if not ("%~3") == ("") goto usage
@if not exist "%~2" mkdir "%~2"
@dir /a:d "%~2" >nul 2>nul
@if %errorlevel% neq 0 echo Directory %2 cannot be created or a file with that name already exists in that location. && exit /b 1
@for /f "delims=" %%a in ('dir /b "%~1" 2^>nul ^| findstr /e /i /v ".log"') do @move /y "%%a" "%~2" >nul && echo %%a
@goto :eof
:usage
@echo movewithoutlog.bat
@echo Moves a set of files, omitting any with .log extension.
@echo Usage: movewithoutlog.bat files destination
@echo e.g. movewithoutlog.bat *.* ..\newdestination
@exit /b 1
You could probably do something like:
mv | grep -v *.log | ./* /path
To test if you're going to be moving the right files you can just do an "ls" and pipe that to the "grep" missing out the "mv"
http://www.pcreview.co.uk/forums/use-exclude-option-xcopy-t1469487.html
At the previous hyperlink, I can read the following QUESTION with the related ANSWER:
QUESTION:
Hi,
I use an xcopy command below but it doesn't do what I want. Instead, it complains "cannot read file: .obj" and then stops. xcopy c:\t1 c:\t2 /EXCLUDE:.obj
I want all .obj files are skipped during copying. Can someone help me? Thanks. Tony
ANSWER:
The /EXCLUDE directive specifies a list of files which contain strings of file names to exclude. For example, to do what you're trying to do, you'll need to create a file, suppose we call it MyExcludes.txt. In this file, there is just one filespec - ".obj" (w/o the quotes).
Then change the command to this:
xcopy c:\t1 c:\t2 /EXCLUDE:MyExcludes.txt
===> THIS IS A PARTIAL SOLUTION, because you copy all but .obj files, but you don't delete them from the SOURCE. You can try to redirect the output of XCOPY to file, and then to manually add DELETE to every row of the file written by the XCOPY command. At last, you execute the file in order to delete the source files.
精彩评论