Batch Scripting Moving files with timestamp

被刻印的时光 ゝ 提交于 2019-12-01 14:15:17

You cannot do it with xcopy but as you've outlined in the comments, this renames the files before copying.

The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

SET "DATE_FOLDER=%YYYY%%MM%%DD%"

cd /d "C:\Test\BaseLine\"
SET "ACHIEVE_DIR=C:\Test\Master Achieve" 
md "%ACHIEVE_DIR%" 2>nul

for /d %%a in (*) do (
for /r %%b in ("%%a\*.jpg") do ren "%%~b" "%%~nb - %DATE_FOLDER%%%~xb"
xcopy "%%a\*.jpg" "%ACHIEVE_DIR%\" /s/h/e/k/f/c/y
)
pause

Edit code to move all *-tasty.jpg files to the %ACHIEVE_DIR% and datestamp them, then remove the original containing folders under C:\Test\BaseLine\ with all remaining files but leaving the files inside C:\Test\BaseLine\ intact.

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

SET "DATE_FOLDER=%YYYY%%MM%%DD%"

cd /d "C:\Test\BaseLine\"
SET "ACHIEVE_DIR=C:\Test\Master Achieve" 
md "%ACHIEVE_DIR%" 2>nul

for /d %%a in (*) do (
for /r %%b in ("%%a\*-tasty.jpg") do move "%%~b" "%ACHIEVE_DIR%\%%~nb - %DATE_FOLDER%%%~xb"
rd /s /q "%%a"
)
pause

This works -

@echo off
for /f "skip=1 delims=." %%i in ('wmic OS Get localdatetime') do set ts=%%i
set dt=%ts:~6,2%-%ts:~4,2%-%ts:~0,4%

set workdir=D:\test\baseline\
set newdir=D:\test\Achieve\

cd %workdir%

for /f "delims=" %%i in ('dir /b /s *.jpg') do call :search "%%i"
goto :eof

:search
set filepath=%~f1
set dirpath=%~dp1
set filename=%~n1
set fileextn=%~x1
if "%dirpath%" EQU "%workdir%" goto :eof
copy /y "%filepath%" %newdir%\%filename%_%dt%%fileextn% >nul 2>&1

:eof

Test output -

D:\>dir "d:\test\baseline", "d:\test\baseline\Folder 123", "d:\test\baseline\Folder 321"
 Volume in drive D is New Volume
 Volume Serial Number is B04C-AB59

 Directory of d:\test\baseline

23/07/2014  21:24    <DIR>          .
23/07/2014  21:24    <DIR>          ..
23/07/2014  20:35                 2 1.jpg
23/07/2014  20:35                 2 2.jpg
23/07/2014  20:36    <DIR>          Folder 123
23/07/2014  20:37    <DIR>          Folder 321
               2 File(s)              4 bytes

 Directory of d:\test\baseline\Folder 123

23/07/2014  20:36    <DIR>          .
23/07/2014  20:36    <DIR>          ..
23/07/2014  20:36                 2 3.jpg
23/07/2014  20:36                 2 4.jpg
               2 File(s)              4 bytes

 Directory of d:\test\baseline\Folder 321

23/07/2014  20:37    <DIR>          .
23/07/2014  20:37    <DIR>          ..
23/07/2014  20:37                 2 5.jpg
23/07/2014  20:37                 2 6.jpg
               2 File(s)              4 bytes
               2 Dir(s)   7,037,329,408 bytes free

D:\>draft.bat

D:\test\baseline>cd\

D:\>dir d:\test\Achieve
 Volume in drive D is New Volume
 Volume Serial Number is B04C-AB59

 Directory of d:\test\Achieve

23/07/2014  21:24    <DIR>          .
23/07/2014  21:24    <DIR>          ..
23/07/2014  20:36                 2 3_23-07-2014.jpg
23/07/2014  20:36                 2 4_23-07-2014.jpg
23/07/2014  20:37                 2 5_23-07-2014.jpg
23/07/2014  20:37                 2 6_23-07-2014.jpg
               4 File(s)              8 bytes
               2 Dir(s)   7,037,329,408 bytes free

Cheers, G

EDIT - This works based on your original question. I haven't seen the comments on the other answer yet. Let me know if you need changes.

BEFORE running draft.bat

D:\>dir "d:\test\baseline", "d:\test\baseline\Folder 123", "d:\test\baseline\Folder 321"
Volume in drive D is New Volume
Volume Serial Number is B04C-AB59
 Directory of d:\test\baseline

23/07/2014  21:24    <DIR>          .
23/07/2014  21:24    <DIR>          ..
23/07/2014  20:35                 2 1.jpg
23/07/2014  20:35                 2 2.jpg
23/07/2014  20:36    <DIR>          Folder 123
23/07/2014  20:37    <DIR>          Folder 321
               2 File(s)              4 bytes

 Directory of d:\test\baseline\Folder 123

23/07/2014  20:36    <DIR>          .
23/07/2014  20:36    <DIR>          ..
23/07/2014  20:36                 2 3-tasty.jpg
23/07/2014  20:36                 2 3-not tasty.jpg
23/07/2014  20:36                 2 4-tasty.jpg
23/07/2014  20:36                 2 4-not tasty.jpg
               2 File(s)              4 bytes

 Directory of d:\test\baseline\Folder 321

23/07/2014  20:37    <DIR>          .
23/07/2014  20:37    <DIR>          ..
23/07/2014  20:37                 2 5-tasty.jpg
23/07/2014  20:37                 2 5-not tasty.jpg
23/07/2014  20:37                 2 6-tasty.jpg
23/07/2014  20:37                 2 6-not tasty.jpg
               2 File(s)              4 bytes
               2 Dir(s)   7,037,329,408 bytes free

D:\>draft.bat

AFTER

D:\test\baseline>cd\

D:\>dir d:\test\Achieve
 Volume in drive D is New Volume
 Volume Serial Number is B04C-AB59

 Directory of d:\test\Achieve

23/07/2014  21:24    <DIR>          .
23/07/2014  21:24    <DIR>          ..
23/07/2014  20:36                 2 3_23-07-2014.jpg
23/07/2014  20:36                 2 4_23-07-2014.jpg
23/07/2014  20:37                 2 5_23-07-2014.jpg
23/07/2014  20:37                 2 6_23-07-2014.jpg
               4 File(s)              8 bytes
               2 Dir(s)   7,037,329,408 bytes free


 Directory of d:\test\baseline

23/07/2014  21:24    <DIR>          .
23/07/2014  21:24    <DIR>          ..
23/07/2014  20:35                 2 1.jpg
23/07/2014  20:35                 2 2.jpg
23/07/2014  20:35                 2 3.jpg
23/07/2014  20:35                 2 4.jpg
23/07/2014  20:35                 2 5.jpg
23/07/2014  20:35                 2 6.jpg
               2 File(s)              4 bytes

Notice how baseline does not have any more folders because they all got deleted after moving all of files
non-tasty -> baseline without non-tasty extension
tasty -> achieve without tasty but with date stamp

I hope you can understand now. Thanks a lot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!