Folks,
I'm trying to set a variable using a for loop and executing a command based on the set variable. But when I do try to do that I get odd behaviour.
The command:
FOR /D %%G in (*) DO SET matr=%%~nG&& robocopy %matr%\AppData\Local\Microsoft\Outlook "E:\Backup - %computername%\Users\%matr%\AppData\Local\Microsoft\Outlook" /s /b /log+:"E:\Backup - %computername%\Log de Backup.txt" /v /A-:SHR /r:2 /w:5 /tee
Explanation:
For every folder in a given folder (C:\Users\, stated earlier on my script), set a variable called matr with the name of the folder. The aim is to, at every loop, look for the next folder inside C:\Users and set the variable as the name of the folder. After that, I run robocopy using the variable as starting point for the source. For exemple, the first folder found on C:\Users would be administrator, and that should make my robocop use administrator\Appdata\Local\Microsoft\Outlook as source. The destination would use the variable too to target a folder in an external drive, in an specific folder in it.
This is the behaviour I get when I run the script with administrative privileges:
E:\>cd /D C:\Users\
C:\Users>FOR / %G in (*) DO SET matr=%~nG && robocopy \AppData\Local\Microsoft\Outlook "E:\Backup - P43971\Users\\AppData\Local\Microsoft\Outlook" /s /b /log+:"E:\Backup - P43971\Log de Backup.txt" /v /A-:SHR /r:2 /w:5 /tee
C:\Users>SET matr=Administrador && robocopy \AppData\Local\Microsoft\Outlook "E:\Backup - P43971\Users\\AppData\Local\Microsoft\Outlook" /s /b /log+:"E:\Backup - P43971\Log de Backup.txt" /v /A-:SHR /r:2 /w:5 /tee
I don't understand why the variable gets blanked out. I tried putting SETLOCAL ENABLEDELAYEDEXPANSION and using !matr! instead of %matr% but to no avail. When using !matr! instead of %matr% the batch recognizes it as direct text and not a variable call.
Relevant parts of the code:
SETLOCAL ENABLEDELAYEDEXPANSION
cd /D C:\Users\
FOR /D %%G in (*) DO SET matr=%%~nG&& robocopy %matr%\AppData\Local\Microsoft\Outlook "E:\Backup - %computername%\Users\%matr%\AppData\Local\Microsoft\Outlook" /s /b /log+:"E:\Backup - %computername%\Log de Backup.txt" /v /A-:SHR /r:2 /w:5 /tee
echo Outlook backup done!
echo.
pause
Behavior when NOT setting enabledelayedexpansion AND using single %matr%:
E:\>cd /D C:\Users\
C:\Users>FOR / %G in (*) DO SET matr=%~nG && robocopy \AppData\Local\Microsoft\Outlook "E:\Backup - P43971\Users\\AppData\Local\Microsoft\Outlook" /s /b /log+:"E:\Backup - P43971\Log de Backup.txt" /v /A-:SHR /r:2 /w:5 /tee
C:\Users>SET matr=Administrador && robocopy \AppData\Local\Microsoft\Outlook "E:\Backup - P43971\Users\\AppData\Local\Microsoft\Outlook" /s /b /log+:"E:\Backup - P43971\Log de Backup.txt" /v /A-:SHR /r:2 /w:5 /tee
Behaviour when setting enabledelayedexpansion and using double %%matr%%:
E:\>cd /D C:\Users\
C:\Users>FOR / %G in (*) DO SET matr=%~nG && robocopy %matr%\AppData\Local\Microsoft\Outlook "E:\Backup - P43971\Users\%matr%\AppData\Local\Microsoft\Outlook" /s /b /log+:"E:\Backup - P43971\Log de Backup.txt" /v /A-:SHR /r:2 /w:5 /tee
C:\Users>SET matr=Administrador && robocopy %matr%\AppData\Local\Microsoft\Outlook "E:\Backup - P43971\Users\%matr%\AppData\Local\Microsoft\Outlook" /s /b /log+:"E:\Backup - P43971\Log de Backup.txt" /v /A-:SHR /r:2 /w:5 /tee
Behaviour when set enabledelayedexpansion and using single !matr!:
E:\>cd /D C:\Users\
C:\Users>FOR / %G in (*) DO SET matr=%~nG && robocopy !matr!\AppData\Local\Microsoft\Outlook "E:\Backup - P43971\Users\!matr!\AppData\Local\Microsoft\Outlook" /s /b /log+:"E:\Backup - P43971\Log de Backup.txt" /v /A-:SHR /r:2 /w:5 /tee
C:\Users>SET matr=Administrador && robocopy !matr!\AppData\Local\Microsoft\Outlook "E:\Backup - P43971\Users\!matr!\AppData\Local\Microsoft\Outlook" /s /b /log+:"E:\Backup - P43971\Log de Backup.txt" /v /A-:SHR /r:2 /w:5 /tee
forline readsfor / %G- theDis missing. Are you using a proper text-editor like Editplus or Notepad++? If you are, ensure you are saving the file as ANSI, not Unicode. If you are trying to use Notepad or a word-processor, then try using a proper text-editor as these utilities may try to format the file to make "sense".