Add date to script?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Daans180
Posts: 4
Joined: 20 Nov 2014 05:54

Add date to script?

#1 Post by Daans180 » 20 Nov 2014 06:00

Hello i have a move script but i like to add a date function so i can, lets see move all files older then >.....

this is what i have allready.

who can help me.
kind regards,


Code: Select all

echo off
SETLOCAL EnableDelayedExpansion
set movedFiles=0
for /R V:\Administratie\CS\Pakbon_Summaries\ %%G in (*) do (
    echo moving... "%%G"
    move /Y "%%G" V:\Administratie\CS\Pakbon_Summaries_per_maand\
    set /a movedFiles+="1"
        if !movedFiles! EQU 1000 GOTO endOfCopy rem if you moved 500 files
  )
  :endOfCopy
  echo Done, %movedFiles% files Where copied successfully
  pause
ENDLOCAL


MOD EDIT: PLEASE USE CODE TAGS

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Add date to script?

#2 Post by foxidrive » 20 Nov 2014 09:40

This isn't fully tested: change the source and target folders to a test folder location, and test this with dummy files first.

The date to compare with is given as the first parameter to the batch file in YYYYMMDD format: such as MyBatch.bat 20130631

Code: Select all

@echo off
if "%~1"=="" (
   echo Moves filenames with modified date earlier than the date given as %%1 (in YYYYMMDD format^)
   echo/
   echo example: "%~f0" 20131229
   echo/
   pause
   goto :EOF
   )

:loop
set "file=%temp%\movefile-%random%.vbs"
if exist "%file%" goto :loop


:: change the source folder and filespec here

dir "V:\Administratie\CS\Pakbon_Summaries\*.*" /s /b /a-d >"%file%.list"

:: change the target folder here

set "target=V:\Administratie\CS\Pakbon_Summaries_per_maand\"

:: Create a list of files with modified dates in this format: YYYYMMDD "filename"

(
 echo. Const ForReading = 1, ForWriting = 2
 echo. infile = "%file%.list"
 echo. Set fso = CreateObject("Scripting.FileSystemObject"^)
 echo. Set f1 = fso.OpenTextFile(infile, ForReading^)
 echo. Do While not f1.AtEndOfStream
 echo. Set f = fso.GetFile(f1.readline^)
 echo. ' n = f.DateLastAccessed
 echo. ' nc = f.DateCreated
 echo. nm = f.DateLastModified
 echo. WScript.Echo CStr(Year(nm^)^)+Right(100+Month(nm^),2^)+Right(100+Day(nm^),2^)+" "+chr(34^)+f+chr(34^)
 echo. loop
 echo. f1.close
)>"%file%"
cscript //nologo "%file%" >"%file%.txt"

:: show the list of files in this format: modified-date "filename"
:: type "%file%.txt"&pause

set c=0
for /f "usebackq tokens=1,*" %%a in ("%file%.txt") do (
      if %%a LSS %~1 (
         echo/Creation date %%a - moving "%%~b"
         set /a c+=1
         move /Y "%%~b" "%target%" >nul
      )
   )
echo done - %c% files were moved.

del "%file%" "%file%.txt"  "%file%.list"
pause

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

Re: Add date to script?

#3 Post by Squashman » 20 Nov 2014 09:50

Hi Foxidrive,
Seen you use this code twice in the last few days. Is this more flexible then just using FORFILES?

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Add date to script?

#4 Post by Yury » 20 Nov 2014 10:00

Code: Select all

@echo off

set "dir=D:\Test"
set age=7

pushd "%dir%"
set movedFiles=0
for /f "tokens=*" %%G in ('
 robocopy . %random% /nc /ns /ndl /njh /njs /l /minage:%age%
') do (
 echo Moving:.. "%%G"
 move /y "%%G" "V:\Administratie\CS\Pakbon_Summaries_per_maand\"&& (
 set /a movedFiles+=1
 )
 for /f %%# in ('set /a movedFiles') do if %%# equ 1000 goto endOfCopy
 )
popd

:endOfCopy
echo.
echo Done: %movedFiles% files were moved successfully.
echo.
pause
exit /b 0

Daans180
Posts: 4
Joined: 20 Nov 2014 05:54

Re: Add date to script?

#5 Post by Daans180 » 25 Nov 2014 06:23

thanks all
this is working for me now
the last script worked fine

Thanks again
Daniel

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Add date to script?

#6 Post by foxidrive » 25 Nov 2014 06:43

Squashman wrote:Hi Foxidrive,
Seen you use this code twice in the last few days. Is this more flexible then just using FORFILES?


The first case compared creation + modified dates which I think could be difficult for forfiles

I haven't tested it but using the VBS script could be quite a bit faster for a lot of files.

Daans180
Posts: 4
Joined: 20 Nov 2014 05:54

Re: Add date to script?

#7 Post by Daans180 » 26 Nov 2014 02:23

And a small next question,
What shoul you adjust in the script if you like to move file between a particular date:

let say files in a particular week or day,



@echo off

set "dir=V:\Administratie\CS\Pakbon_Summaries"
set age=299

pushd "%dir%"
set movedFiles=0
for /f "tokens=*" %%G in ('
robocopy . %random% /nc /ns /ndl /njh /njs /l /minage:%age%
') do (
echo Moving:.. "%%G"
move /y "%%G" "V:\Administratie\CS\Pakbon_Summaries_per_maand_2\"&& (
set /a movedFiles+=1
)
for /f %%# in ('set /a movedFiles') do if %%# equ 1000 goto endOfCopy
)
popd

:endOfCopy
echo.
echo Done: %movedFiles% files were moved successfully.
echo.
pause
exit /b 0

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Add date to script?

#8 Post by Yury » 26 Nov 2014 06:09

Daans180 wrote:What shoul you adjust in the script if you like to move file between a particular date



Code: Select all

@echo off

set "from=D:\Test 1"
set "to=D:\Test 2"
set maxage=20141010
set minage=20141110

pushd "%from%"
set movedFiles=0
for /f "tokens=*" %%G in ('
 robocopy . %random% /nc /ns /ndl /njh /njs /l /minage:%minage% /maxage:%maxage%
') do (
 echo Moving:.. "%%G"
 move /y "%%G" "%to%\"&& (
 set /a movedFiles+=1
 )
 for /f %%# in ('set /a movedFiles') do if %%# equ 1000 goto endOfCopy
 )
popd

:endOfCopy
echo.
echo Done: %movedFiles% files were moved successfully.
echo.
pause
exit /b 0

Daans180
Posts: 4
Joined: 20 Nov 2014 05:54

Re: Add date to script?

#9 Post by Daans180 » 02 Dec 2014 02:10

Thanks for the reply this works for me,

Great forum for starters at least,

will be back.
Daniel 8)

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

Re: Add date to script?

#10 Post by Squashman » 02 Dec 2014 19:35

Daans180 wrote:Thanks for the reply this works for me,

Great forum for starters at least,

will be back.
Daniel 8)

Hopefully to learn and contribute.

Post Reply