Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Roy
- Posts: 10
- Joined: 31 Aug 2018 14:45
#1
Post
by Roy » 31 Aug 2018 14:55
Hello all,
I am trying to build a batch that looks for .pdf files in a folder, checks for duplicates in a second folder and
all of its directories and finally if they exist in that (2nd) folder it moves the files from 1st folder to a 3rd one.
for better understanding
look for .pdf files in F1
if they exists in F2 or its subdirs
move files from F1 to F3
My script looks like below but it doesn't work due to /s parameter in if exist command.
Code: Select all
@echo off
for /f "tokens=* delims=" %%v in ('dir /b %userprofile%\Desktop\F1\*.pdf') do (
if exist "%userprofile%\Desktop\F2" /s move /y "%userprofile%\Desktop\F1\%%v" "%userprofile%\Desktop\F3"
if not exist "%userprofile%\Desktop\F2" /s echo File %%v is not a duplicate
)
pause
Any ideas please?
Last edited by
Roy on 31 Aug 2018 15:37, edited 1 time in total.
-
Roy
- Posts: 10
- Joined: 31 Aug 2018 14:45
#2
Post
by Roy » 31 Aug 2018 15:36
for /f "tokens=* delims=" %%v in ...
corrected
-
ShadowThief
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
#3
Post
by ShadowThief » 31 Aug 2018 21:30
What are you expecting the /s flag to do? The only flag that if has is /i for case-insensitive comparisons.
-
Roy
- Posts: 10
- Joined: 31 Aug 2018 14:45
#4
Post
by Roy » 31 Aug 2018 23:58
ShadowThief wrote: ↑31 Aug 2018 21:30
What are you expecting the /s flag to do? The only flag that if has is /i for case-insensitive comparisons.
I am expecting to check if exist in subdirectories also but it doesn't. Is there any other way to do it ?
-
pieh-ejdsch
- Posts: 240
- Joined: 04 Mar 2014 11:14
- Location: germany
#5
Post
by pieh-ejdsch » 01 Sep 2018 00:02
Hello
A small modification
Code: Select all
@echo off
pushD %userprofile%\desktop\F2
for /r %%i in ( *.pdf ) do (
if exist "..\F1\%%~nxi" ( move /y "..\F1\%%~nxi" ..\F3
) else echo File %%~nxi is not a duplicate
)
popD
Phil
-
Roy
- Posts: 10
- Joined: 31 Aug 2018 14:45
#6
Post
by Roy » 01 Sep 2018 00:32
pieh-ejdsch wrote: ↑01 Sep 2018 00:02
Hello
A small modification
Code: Select all
@echo off
pushD %userprofile%\desktop\F2
for /r %%i in ( *.pdf ) do (
if exist "..\F1\%%~nxi" ( move /y "..\F1\%%~nxi" ..\F3
) else echo File %%~nxi is not a duplicate
)
popD
Phil
It works. Thanks for it.
Is there any way to limit the search in %userprofile%\desktop\F2 only to files (.pdf) that have been modified the current date? something like forfiles /d +0
-
Roy
- Posts: 10
- Joined: 31 Aug 2018 14:45
#7
Post
by Roy » 01 Sep 2018 05:20
I only want to search today's files so i must put a date parameter in below command
for /r %%i in ( *.pdf )
something like ( 'date /t' ) or something ,
any ideas?
-
Ed Dyreen
- Expert
- Posts: 1569
- Joined: 16 May 2011 08:21
- Location: Flanders(Belgium)
-
Contact:
#8
Post
by Ed Dyreen » 01 Sep 2018 17:39
Roy wrote: ↑01 Sep 2018 00:32
pieh-ejdsch wrote: ↑01 Sep 2018 00:02
Hello
A small modification
Code: Select all
@echo off
pushD %userprofile%\desktop\F2
for /r %%i in ( *.pdf ) do (
if exist "..\F1\%%~nxi" ( move /y "..\F1\%%~nxi" ..\F3
) else echo File %%~nxi is not a duplicate
)
popD
assuming DATE format: dayName day/month/year
assuming TIMESTAMP format:
Phil
It works. Thanks for it.
Is there any way to limit the search in %userprofile%\desktop\F2 only to files (.pdf) that have been modified the current date? something like forfiles /d +0
Code: Select all
@echo off
pushD "%userprofile%\desktop\F2"
for /r %%i in ( *.pdf ) do if exist "..\F1\%%~nxi" (
for /F "useback tokens=1 delims= " %%? in (
'%%~ti'
) do for /F "useback tokens=2 delims= " %%D in (
'%DATE%'
) do if /I "%%~?" EQU "%%~D" (
move /y "..\F1\%%~nxi" "..\F3"
) else echo. File %%~nxi last modified %%~? today %%~D ignoring
) else echo. File %%~nxi is not a duplicate
popD
assuming DATE format : dayName
day/month/year
assuming TIMESTAMP format :
day/month/year hour:sec
-
Roy
- Posts: 10
- Joined: 31 Aug 2018 14:45
#9
Post
by Roy » 02 Sep 2018 02:32
Ed Dyreen wrote: ↑01 Sep 2018 17:39
Code: Select all
@echo off
pushD "%userprofile%\desktop\F2"
for /r %%i in ( *.pdf ) do if exist "..\F1\%%~nxi" (
for /F "useback tokens=1 delims= " %%? in (
'%%~ti'
) do for /F "useback tokens=2 delims= " %%D in (
'%DATE%'
) do if /I "%%~?" EQU "%%~D" (
move /y "..\F1\%%~nxi" "..\F3"
) else echo. File %%~nxi last modified %%~? today %%~D ignoring
) else echo. File %%~nxi is not a duplicate
popD
assuming DATE format : dayName
day/month/year
assuming TIMESTAMP format :
day/month/year hour:sec
Thank you for your answer and solution. The problem with time it takes to finish, still persists though.
Folder F2 has almost 300k pdf files in subdirs (~80GB). We should first filter the files with modified date of today and then start checking if they exist.
-
Ed Dyreen
- Expert
- Posts: 1569
- Joined: 16 May 2011 08:21
- Location: Flanders(Belgium)
-
Contact:
#10
Post
by Ed Dyreen » 02 Sep 2018 04:42
Roy wrote: ↑02 Sep 2018 02:32
Thank you for your answer and solution. The problem with time it takes to finish, still persists though.
Folder F2 has almost 300k pdf files in subdirs (~80GB). We should first filter the files with modified date of today and then start checking if they exist.
If performance is important you made an ill choice using batch. Better use vbscript, jscript or if you have it powershell. You could first filter the files with modified date of today and then start checking if they exist but it will make little difference, batch is slow by nature.
-
Roy
- Posts: 10
- Joined: 31 Aug 2018 14:45
#11
Post
by Roy » 02 Sep 2018 10:52
Ed Dyreen wrote: ↑02 Sep 2018 04:42
Roy wrote: ↑02 Sep 2018 02:32
Thank you for your answer and solution. The problem with time it takes to finish, still persists though.
Folder F2 has almost 300k pdf files in subdirs (~80GB). We should first filter the files with modified date of today and then start checking if they exist.
If performance is important you made an ill choice using batch. Better use vbscript, jscript or if you have it powershell. You could first filter the files with modified date of today and then start checking if they exist but it will make little difference, batch is slow by nature.
I created a script in Powershell, it was easier to build and runs well!
Code is as follows for anyone else looking for it.
Code: Select all
$src = Get-ChildItem -path C:\Users\Roy\Desktop\F1
$pdf = Get-ChildItem -Recurse -Filter *.pdf -path C:\Users\Roy\Desktop\F2 | ? { $_.LastWriteTime -gt (Get-Date).Date}
$matches = (Compare-Object -ReferenceObject $src -DifferenceObject $pdf -ExcludeDifferent -IncludeEqual -PassThru -Property Name)
foreach ($file in $matches)
{
Move-Item -path C:\Users\Roy\Desktop\F1\$($file.Name) -destination C:\Users\Roy\Desktop\F3
}
Last edited by
Roy on 02 Sep 2018 10:55, edited 1 time in total.
-
pieh-ejdsch
- Posts: 240
- Joined: 04 Mar 2014 11:14
- Location: germany
#12
Post
by pieh-ejdsch » 02 Sep 2018 10:53
than use xcopy
Note: Xcopy /L /U /Y /D:m-t-y
... does not check whether the target file is more up-to-date,
but outputs the source file name if the specified date is the same as or older than the source file and the target file is also present.
Only: Xcopy /L /U /Y /D
... this will Output all NOT up-to-date target files.
Code: Select all
@echo off
setlocal
set prompt=$g$s
:getCurrent.timestamp
rem robocopy /L "\.. Timestamp ..\\" .
for /f "eol=D tokens=1-6 delims=/: " %%T in (' robocopy /L /njh "\|" .^|find "123" ') do (
rem XcopyToday=MM-DD-YYYY OR M-D-Y
set "XcopyToday=%%U-%%V-%%T"
)
rem END get.currentTimestamp
> "%temp%\todayList" (
for /d /r "%userprofile%\desktop\F2" %%i in (.) do xcopy /L /U /Y /D:%XcopyToday% "%userprofile%\desktop\F1\*.pdf" "%%~i"
)
if not exist "%userprofile%\desktop\F3" md "%userprofile%\desktop\F3"
pushD "%userprofile%\desktop\F3"
for /f "usebackQ delims=" %%i in ( "%temp%\todayList" ) do if exist "%%~i" move /y "%%~i"
popD
del "%temp%\todayList"
Phil
-
Roy
- Posts: 10
- Joined: 31 Aug 2018 14:45
#13
Post
by Roy » 04 Sep 2018 01:49
Thank you very much Phil, works great and it seems it is faster than the one i created in PowerShell.