简体   繁体   中英

Batch script issues when copying files

I'm having an issue and I have been working on it for hours. I have no idea why it wont work, but I have a feeling it has to do with doing a for loop in a for loop. Its going to be a little difficult to explain. So here goes:

I have this code that works. What it does is copy 3 files from programdata to appdata. We need the "for loop" because the profile %var% is always different in that folder. It will find the name and input it to "cd" into.

Echo Restore Firefox Files

set copycmd=/y
timeout 7
c:
cd "%userprofile%\AppData\Roaming\Mozilla\Firefox\"
for /f "tokens=1,2 delims=/" %%i in ('findstr /l ".default" profiles.ini') do call set var1=%%j
cd profiles\%var1%
if exist "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\places.sqlite" copy "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\places.sqlite"
if exist "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\key3.db" copy "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\key3.db"
if exist "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\signons.sqlite" copy "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\signons.sqlite"

The point is, that code works. It copys from where i want it to. Here's my issue. I have this code. It will not work. Note the main loop starts where it says "Echo Loop through each user and copy files".

Echo Set Usernames into Variables (User1, User2, etc)
for /F "tokens=*" %%A in (C:\ProgramData\TLCloud\WindowsUsers.txt) do (
    SET /A vidx=!vidx! + 1
    set user!vidx!=%%A
    set /A i = !i! + 1
)

Echo Put Usernames into an Array
set i=0
for /f "delims=" %%a in (C:\ProgramData\TLCloud\WindowsUsers.txt) do (
    set /A i+=1
    set list[!i!]=%%a
)

Echo Loop Through Each User and Copy Files
for /F "tokens=2 delims==" %%s in ('set list[') do (
    If NOT "%%s" == "Guest" (
        If NOT "%%s" == "Administrator" (

Echo Restore Firefox Files

set copycmd=/y
::start "browser" /d "C:\Program Files (x86)\Mozilla Firefox" firefox.exe
timeout 7
c:
cd "%userprofile%\AppData\Roaming\Mozilla\Firefox\"
for /f "tokens=1,2 delims=/" %%i in ('findstr /l ".default" profiles.ini') do call set var1=%%j
cd profiles\%var1%
if exist "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\places.sqlite" copy "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\places.sqlite"
if exist "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\key3.db" copy "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\key3.db"
if exist "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\signons.sqlite" copy "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\signons.sqlite"

)
)
)

Why will this code not work? Ignore the variables and array code, that is there so instead of "Username" I can eventually use "%%s" as the variable for changing usernames. But there is no reason this should not work, or am I not allowed to have a for loop in another for loop?

Any help is greatly appreciated.

%var1% is replaced by an empty string already on starting processing the most outer loop with set list[

One solution would be using !var1! instead of %var1% as it looks like delayed environment variable expansion is already enabled. In this case call set var1=%%j should be also corrected to just set var1=%%j .

Another solution is using following code:

Echo Set Usernames into Variables (User1, User2, etc)
for /F "tokens=*" %%A in (C:\ProgramData\TLCloud\WindowsUsers.txt) do (
   SET /A vidx=!vidx! + 1
   set user!vidx!=%%A
   set /A i = !i! + 1
)

Echo Put Usernames into an Array
set i=0
for /f "delims=" %%a in (C:\ProgramData\TLCloud\WindowsUsers.txt) do (
   set /A i+=1
   set list[!i!]=%%a
)

Echo Loop Through Each User and Copy Files
for /F "tokens=2 delims==" %%s in ('set list[') do (
   If NOT "%%s" == "Guest" (
      If NOT "%%s" == "Administrator" (

         Echo Restore Firefox Files

         set copycmd=/y
         rem start "browser" /d "C:\Program Files (x86)\Mozilla Firefox" firefox.exe
         rem timeout 7
         cd /D "%userprofile%\AppData\Roaming\Mozilla\Firefox\"
         for /f "tokens=1,2 delims=/" %%i in ('findstr /l ".default" profiles.ini') do (
            cd "profiles\%%j"
            if exist "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%%j\places.sqlite" copy "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%%j\places.sqlite"
            if exist "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%%j\key3.db" copy "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%%j\key3.db"
            if exist "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%%j\signons.sqlite" copy "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%%j\signons.sqlite"
         )
      )
   )
)

Two problems.

The :: style commenting is a broken label. It causes FOR to get confused and terminate. Replace it with REM within a block statement (a parenthesised series of statements).

Second is more major, but easily cured.

You haven't shown us your entire batch. It would follow from such statement as set list[!i!]=%%a that you have delayed expansion turned ON . This is excruciatingly important.

Within a block statement, any %var% is evaluated and replaced at parse time, so %var1% will be replaced by var1 's value when the entire block (ie the outermost FOR is parsed. Since you are attempting to change var1 within your innermost loop, to access the changed value, you need to use !var1! which means the current , not the parse-time value of the variable.

Now, since delayed expansion appears to be enabled, simply replacing %var1% with !var1! would appear to be a solution. It's not hard to notice however that var1 is being asigned the value %%j so there appears to be no reason to use var1 at all - simply replace it with %%j !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM