Page 1 of 1
Exist in /s "directory"doesn't work
Posted: 31 Aug 2018 14:55
by Roy
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?
Re: Exist in /s "directory"doesn't work
Posted: 31 Aug 2018 15:36
by Roy
for /f "tokens=* delims=" %%v in ...
corrected
Re: Exist in /s "directory"doesn't work
Posted: 31 Aug 2018 21:30
by ShadowThief
What are you expecting the /s flag to do? The only flag that if has is /i for case-insensitive comparisons.
Re: Exist in /s "directory"doesn't work
Posted: 31 Aug 2018 23:58
by Roy
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 ?
Re: Exist in /s "directory"doesn't work
Posted: 01 Sep 2018 00:02
by pieh-ejdsch
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
Re: Exist in /s "directory"doesn't work
Posted: 01 Sep 2018 00:32
by Roy
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
Re: Exist in /s "directory"doesn't work
Posted: 01 Sep 2018 05:20
by Roy
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?
Re: Exist in /s "directory"doesn't work
Posted: 01 Sep 2018 17:39
by Ed Dyreen
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
Re: Exist in /s "directory"doesn't work
Posted: 02 Sep 2018 02:32
by Roy
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.
Re: Exist in /s "directory"doesn't work
Posted: 02 Sep 2018 04:42
by Ed Dyreen
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.
Re: Exist in /s "directory"doesn't work
Posted: 02 Sep 2018 10:52
by Roy
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
}
Re: Exist in /s "directory"doesn't work
Posted: 02 Sep 2018 10:53
by pieh-ejdsch
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
Re: Exist in /s "directory"doesn't work
Posted: 04 Sep 2018 01:49
by Roy
Thank you very much Phil, works great and it seems it is faster than the one i created in PowerShell.