#1 18 Apr 2020 00:02

paul0041
Member
Registered: 19 Mar 2015
Posts: 21

For /R with variable

Hi All, I've got a puzzler:

for /R D:\Users\UserX\Contacts\ %%A in (*.*) do if not exist !BackupLocation!\!UserID!\!MACx!\Contacts\%%~nxA echo %%~A >> !WorkingDirectory!\missingFiles.txt

The above works fine and creates a text file that lists files in the source directory that are not in the backup directory.

However this does not work:
for /R !UserFolder!\!UserID!\Contacts\ %%A in (*.*) do if not exist !BackupLocation!\!UserID!\!MACx!\Contacts\%%~nxA echo %%~A >> !WorkingDirectory!\missingFiles.txt

in this case !UserFolder!=D:\Users and !UserID!=UserX so I figured it would work... but apparently you can't use a variable before the parameter in a for /R loop?

How can I do this?

Offline

#2 18 Apr 2020 10:29

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 180

Re: For /R with variable

You can use dir to walk the directory instead and parse that output.

for /f "delims=" %%A in ('dir /S /b !UserFolder!\!UserID!\Contacts\') do if not exist !BackupLocation!\!UserID!\!MACx!\Contacts\%%~nxA echo %%~A >> !WorkingDirectory!\missingFiles.txt

Offline

#3 20 Apr 2020 16:22

paul0041
Member
Registered: 19 Mar 2015
Posts: 21

Re: For /R with variable

Very cool...  Thank you Shadow Thief!

Offline

#4 20 Apr 2020 22:50

paul0041
Member
Registered: 19 Mar 2015
Posts: 21

Re: For /R with variable

Well... almost.  Fails when it gets to subdirectories because %%~nxA doesn't contain the path below the root folder... not sure what to do.

Offline

#5 30 Apr 2020 00:20

paul0041
Member
Registered: 19 Mar 2015
Posts: 21

Re: For /R with variable

The answer was a little substitution to remove the root portion of the source path to create a relative path that could be appended to the backup location and searched, but this led to another problem... see post "Parser Puzzler"

for /f "delims=" %%A in ('If Exist "!RootPath!\!folder!" dir /S /b "!RootPath!\!folder!"') do (
Set Pathx=%%A
call set Pathx=%%Pathx:!RootPath!=%%
if not exist "%BackupLocation%\!UserID!\!MACx!!Pathx!" echo %%~A >> %WorkingDirectory%\missingFiles.txt
)

Offline

Board footer

Powered by FluxBB