Reputation: 23
This code works well for me to move files with the same filename but different extension into a subfolder.
So the current logic is: If an NC1 file has the same filename as a PDF file then move that NC1 to its respective subfolder.
But my files don't have the same filename.
The following are 2 example files:
How do I change this code to follow this logic: If a PDF filename CONTAINS the filename of an NC1 file then move that NC1 to its respective subfolder.
for %%j in ("..\2PDF_Outsourced\1PDF_Heavy\*.pdf") do (
if exist "%%~nj.nc1" (
move /-y "%%~nj.nc1" "\2NC1_Outsourced\1NC1_Heavy"
)
)
for %%j in ("..\2PDF_Outsourced\1PDF_Light\*.pdf") do (
if exist "%%~nj.nc1" (
move /-y "%%~nj.nc1" "\2NC1_Outsourced\1NC1_Light"
)
)
Thank you in advance for any help. I have been stuck at this stage for a while and am struggling to understand delimiters, strings, and wildcards.
Upvotes: 2
Views: 1381
Reputation: 130829
The trick is to reverse your logic. Iterate the .nc1 files and then look to see if matching .pdf exists (with wildcards). Adding a 2nd loop for Heavy and Light avoids code replication.
for %%F in ("*.nc1") do for %%P in (Heavy Light) do (
if exist "..\2PDF_Outsourced\1PDF_%%P\*%%~nF*.pdf" (
if exist "%%F" move /-y "%%F" "\2NC1_Outsourced\1NC1_%%P"
)
)
EDIT
I added a 2nd IF EXIST to the code above just in case the name matches both Heavy and Light pdf files.
If Endoro's concern about ignoring names that match a substring of a larger word is valid, then the above can be extended:
for %%F in ("*.nc1") do (
set "name=%%~nF"
setlocal enableDelayedExpansion
for %%C in (. [ ^^) do set "name=!name:%%C=\%%C!"
for %%N in (!name!) do (
endlocal
for %%P in (Heavy Light) do for /f "eol=: delims=" %%A in (
'dir /b /a-d "..\2PDF_Outsourced\1PDF_%%P\*%%~nF*.pdf"^|findsdr /i "\<%%N\>"'
) do if exist "%%F" move /-y "%%F" "\2NC1_Outsourced\1NC1_%%P"
)
)
Upvotes: 1
Reputation: 37569
you can use findstr
:
REM this is true
echo(999-P-f100 - PLATE - Rev 0 - 287x200|findstr "\<f100\>"
REM this is false
echo(999-P-f1000 - PLATE - Rev 0 - 287x200|findstr "\<f100\>"
REM also false
echo(999-P-f10 - PLATE - Rev 0 - 287x200|findstr "\<f100\>"
you can put this in a for
loop:
for %%i in (*.nc1) do (
for /f "delims=" %%j in ('dir /a-d /b "..\2PDF_Outsourced\1PDF_Heavy\*.pdf"^|findstr /i "%%~ni"') do (
Upvotes: 1
Reputation: 29339
this is a brute force approach that can help you get started
for %%a in ("*.nc1") do (
for %%b in ("*.pdf") do (
echo.%%~nb | findstr /i /c:"%%~na" 1>nul
if not errorlevel 1 (
echo %%~na found in %%~nb
)
)
)
if you have many NC1 and PDF files then you may need to optimize this method (for example by storing the list of PDF files)
Upvotes: 0
Reputation: 57252
not tested :
@echo off
call :copy_pdf "\2NC1_Outsourced\1NC1_Light"
call :copy_pdf "\2NC1_Outsourced\1NC1_Heavy"
goto :eof
:copy_pdf [%1 - directory to search]
if not exist "%~1\" echo dir not exist >2 & exit /b 1
pushd "..\%~1"
for %%N in (*.nc1) {
for /f "delims=" %%P in ('dir /b /s /a:-d *.pdf^| find /i "%%~nN"') do (
move /-y "%%~nP.nc1" "%~1"
)
)
popd
Upvotes: 0