Windows批处理 - 嵌套循环 - Firefox配置文件迁移 [英] Windows batch - Nested loop - Firefox profile migration

查看:141
本文介绍了Windows批处理 - 嵌套循环 - Firefox配置文件迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我试图将Firefox配置文件从本地机器迁移到用户的漫游配置文件目录。一些用户已经在他们的家庭驱动器中的配置文件,大多数不会。

我已经尝试使用来自Mike Kaply的CCK工具执行此操作,但不幸的是,它不会迁移Firefox配置文件目录转换为用户的漫游配置文件,而只是指向没有Firefox配置文件的漫游配置文件目录。

所以,要解决这个问题,我决定创建一个批处理,该目录在home目录下的每个用户目录中查找目录FirefoxProfile。如果这个文件夹不存在,它会创建它(隐藏文件夹)。此外,该脚本将遍历每个本地配置文件,在每个Firefox配置文件文件夹中搜索最新的places.sqlite文件(Firefox的书签文件),将其复制到家庭中的用户FirefoxProfile文件夹,最后将文件profiles.ini复制到用户的Firefox文件夹下的AppData \ Roaming \Mozilla。

如果用户的主目录下的文件夹FirefoxProfile确实存在,该脚本将执行相同的步骤,如果该文件夹不存在,但不能创建文件夹FirefoxProfile。



我写的脚本没有按预期运行。我试图重写它(重新排列引号和括号,重新格式化代码),但总是归结为以下问题:
脚本删除FOR循环中的参数/ D。另外从脚本不能正确识别%%变量,而是删除前导的%字符。



另外,FOR / F循环。 flie 3.txt的输出是:

lockquote
DIR / S / B / O:-D
testuser1.V2 \\ AppData \Roaming\Mozilla\Firefox\Profiles\places.sqlite
home\testuser1\FireFoxProfile\Default


但它应该回馈这样的内容:

lockquote
testuser1.V2\\AppData\\Roaming\\Mozilla \\Firefox\Profiles\lh15n9tq.default\places.sqlite
home\testuser1\FireFoxProfile\Default




<我非常感谢任何帮助。
提前致谢

脚本:

  setlocal enabledelayedexpansion 
SET HomeProfile =
SET WSProfile =
SET FFProfile =

pushd \\srv (*)DO(
SETHomeProfile = %% A
如果不存在!HomeProfile!\\ \\FireFoxProfile(
echo does not exist>> C:\ tmp\1.txt
echo!HomeProfile!\FireFoxProfile\Default>> C:\ tmp \ 2.txt
cd profile_WS
FOR / D %% B in(!HomeProfile!.V2)DO(
SETWSProfile = %% B
FOR / Fusebackq delims =%% C IN('DIR!WSProfile!\AppData\Roaming\Mozilla\Firefox\Profiles\places.sqlite / S / B / O:-D')DO(
SETFFProfile = %% C
echo!FFProfile!home\!HomeProfile!\FireFoxProfile\Default>> C:\ tmp\3.txt
echo C:\\ \\ tmp \profiles.ini!WSProfile!\AppData\Roaming\Mozilla\Fir efox>> C:\ tmp\4.txt
))
)ELSE(
echo!HomeProfile!\FireFoxProfile\Default>> C:\ tmp \5.txt
FOR / D %% B IN(!HomeProfile!.V2)DO(
SETWSProfile = %% B
FOR / Fusebackq delims = %% C IN('DIR!WSProfile!\AppData\Roaming\Mozilla\Firefox\Profiles\places.sqlite / S / B / O:-D')DO(
SETFFProfile = %% C
echo!FFProfile! Home \!HomeProfile!\FireFoxProfile\Default>> C:\ tmp\6.txt
echo C:\tmp\profiles.ini!WSProfile!\AppData\Roaming\Mozilla\Firefox>> C:\ tmp\7.txt)
))

popd
PAUSE

脚本状态:

为了测试脚本,我替换了实际的代码,脚本将其输出写入文本文件。



问题:

用户脚本失败,如下所示:

  V:\home>(
SETHomeProfile = testuser1
如果不存在!HomeProfile!\FireFoxProfile(
echo不存在1>> C:\ tmp\1.txt
echo!HomeProfile!\FireFoxProfile\Default 1>>> ; C:\ tmp\2.txt
cd profile_WS
FOR(!HomeProfile!.V2)中的%B%DO(
SETWSProfile =%B
FOR / Fusebackq delims%C IN('DIR / S / B / O:-D!WSProfile!\AppData\Roaming\Mozilla\Firefox\Profiles\places.sqlite')DO(
SETFFProfile =%C
echo!FFProfile!home\!HomeProfile!\FireFoxProfile\Def ault 1>> C:\ tmp\3.txt
echo C:\tmp\profiles.ini!WSProfile!\AppData\Roaming\Mozilla\Firefox 1>> C :\tmp\4.txt


)ELSE(
echo!HomeProfile!\FireFoxProfile\Default 1>> C:\ tmp \\ \\5.txt
FOR /%B IN(!HomeProfile!.V2)DO(
SETWSProfile =%B
FOR / Fusebackq delims%C IN('DIR / S / B / O:-D!WSProfile!\AppData\Roaming\Mozilla\Firefox\Profiles\places.sqlite')DO(
SETFFProfile =%C
回声!FFProfile! Home \!HomeProfile!\FireFoxProfile\Default 1>> C:\ tmp\6.txt
echo C:\tmp\profiles.ini!WSProfile!\AppData\Roaming \Mozilla\Firefox 1>> C:\ tmp\7.txt





V: (
SETWSProfile = testuser1.V2
FOR / Fusebackq delims%C IN('DIR / S / B / O:-D!WSProfile!\AppData\\ \\ Roaming \Mozilla\Firefox\Profiles\places.sqlite')DO(
SETFFProfile =%C
echo!FFProfile!home\!HomeProfile!\FireFoxProfile\默认1>> C:\ tmp\3.txt
echo C:\tmp\profiles.ini!WSProfile!\AppData\Roaming\Mozilla\Firefox 1>> C :\tmp\4.txt



V:\home>(
SETFFProfile = DIR
echo! FFProfile!home\!HomeProfile!\FireFoxProfile\Default 1>> C:\ tmp\3.txt
echo C:\ tmp\profiles.ini! WSProfile!\AppData\Roaming\Mozilla\Firefox 1>> C:\ tmp\4.txt

引起我注意的是:

  cd profile_WS 
!(!HomeProfile!.V2)中的/%B DO(
SETWSProfile =%B




 <$ c $ 

(!HomeProfile!.V2)中的FOR / D %% B(

%% A.V2 ,所以你只需将WSProfile设置为 \\srv-xyz\users\home\profile_WS\\ \\ testuser1.V2 ,可能可能,因为你当前的目录应该是 \\srv-xyz\users\home\profile_WS ,只要使用设置WSProfile =%CD%\ %% A.V2 根据exis,你所采取的步骤似乎有所不同用户主目录下的FirefoxProfile文件夹。如果它不存在,则将目录更改为 profile_WS ,并且永远不会从那里返回,这将在脚本的其余部分中产生问题。如果确实存在,您将停留在家庭



编辑



我已经决定发布这个,尽管它不是OP的问题的解决方案,尽管它可能对他们以及其他所有人都有用。



这将试图找到所有标准本地用户的所有活动的Firefox配置文件。它以格式%UsersName [i]%其中 i 可以是递增的整数找到每个用户配置文件 c> @Echo Off
For / FUseBackQ Tokens = 1 * Delims ==%% A In(
`WMIc Path Win32_UserProfile Where Special!='True'Get LocalPath / Value`
)Do For / FDelims =%% C In(%% B)Do Call:Users%% C
If Defined i If Not%i%==0 (SetLocal EnableDelayedExpansion
For / L %% A In(1,1,%i%)Do Echo(%%!UN![%% A] %% =!%UN%[%% A]!
EndLocal
Echo(按任意键退出...
超时-1 1> Nul)
退出/ B

:用户
设置PF =%〜1 \AppData\Roaming\Mozilla\Firefox\
如果不存在%PF%GoTo:EOF
设置UN =%〜nx1
设置i = 0
For / FTokens = 1 * Delims ==%% A In(
'FindStr / R^ Path =。*%PF% (设置FP = %% B
调用:Profs%% FP:/ = \ %%)
转到:EOF

:教授
Set / Ai + = 1
如果存在%PF %%〜1 \(设置%UN%%[%i%] =%PF %%〜1)否则设置%UN%%[%i%] =%〜1

脚本没有在多用户系统上测试,也没有在多个配置文件的用户上测试,我只是把它写成一个概念而已。

Background:

I am trying to migrate the Firefox profiles from the local machines to the roaming profile directory of the user. Some users already have the profile located in their home drive, the majority does not.

I already tried to do this with the CCK Tool from Mike Kaply but unfortunately it's not migrating the Firefox profile directory to the roaming profile of the user, but instead just points to the roaming profile directory where no Firefox profile is located.

So, to work around this, I decided to create a batch, which looks in every user directory under the directory "home" for the directory "FirefoxProfile". If this folder does not exist, it would create it (hidden folder). Furthermore the script would loop through every local profile, search in every Firefox profile folder for the latest places.sqlite file (the bookmarks file of Firefox), copy it to the users FirefoxProfile folder under home and lastly copy the file profiles.ini to the users Firefox folder under AppData\Roaming\Mozilla.

If the folder FirefoxProfile under the user’s home directory does exist, the script would do the same steps as if the folder does not exist, but not create the folder FirefoxProfile.

The script I wrote does not run as expected. I tried to rewrite it (rearranged the quotes and brackets, reformat the code), but it always comes down to the following problems: The script deletes the parameter "/D" in the FOR loop. Also from there on the script doesn't recognize the "%%" variables correctly, instead it deletes the leading "%" character.

Furthermore something is wrong with the FOR /F loop. The output in the flie 3.txt is:

DIR /S /B /O:-D testuser1.V2\AppData\Roaming\Mozilla\Firefox\Profiles\places.sqlite home\testuser1\FireFoxProfile\Default

But it should give back something like this:

testuser1.V2\AppData\Roaming\Mozilla\Firefox\Profiles\lh15n9tq.default\places.sqlite home\testuser1\FireFoxProfile\Default

I would highly appreciate any help. Thanks in advance.

The script:

setlocal enabledelayedexpansion
SET HomeProfile=""
SET WSProfile=""
SET FFProfile=""

pushd \\srv-xyz\users
cd home
FOR /D %%A in (*) DO (
SET "HomeProfile=%%A"
if not exist !HomeProfile!\FireFoxProfile (
echo does not exist >> C:\tmp\1.txt
echo !HomeProfile!\FireFoxProfile\Default >> C:\tmp\2.txt
cd profile_WS
FOR /D %%B in (!HomeProfile!.V2) DO (
SET "WSProfile=%%B"
FOR /F "usebackq delims=" %%C IN ('DIR !WSProfile!\AppData\Roaming\Mozilla\Firefox\Profiles\places.sqlite /S /B /O:-D') DO (
SET "FFProfile=%%C"
echo !FFProfile! home\!HomeProfile!\FireFoxProfile\Default >> C:\tmp\3.txt
echo C:\tmp\profiles.ini !WSProfile!\AppData\Roaming\Mozilla\Firefox >> C:\tmp\4.txt
))
) ELSE (
echo !HomeProfile!\FireFoxProfile\Default >> C:\tmp\5.txt
FOR /D %%B IN (!HomeProfile!.V2) DO (
SET "WSProfile=%%B"
FOR /F "usebackq delims=" %%C IN ('DIR !WSProfile!\AppData\Roaming\Mozilla\Firefox\Profiles\places.sqlite /S /B /O:-D') DO (
SET "FFProfile=%%C"
echo !FFProfile! home\!HomeProfile!\FireFoxProfile\Default >> C:\tmp\6.txt
echo C:\tmp\profiles.ini !WSProfile!\AppData\Roaming\Mozilla\Firefox >> C:\tmp\7.txt)
))
)
popd
PAUSE

The scripts state:

To test the script, I replaced the actual code and made the script write its output to text files.

The Problem:

For every user the script fails like follows

V:\home>(
SET "HomeProfile=testuser1"  
 if not exist !HomeProfile!\FireFoxProfile (
echo does not exist  1>>C:\tmp\1.txt  
 echo !HomeProfile!\FireFoxProfile\Default  1>>C:\tmp\2.txt  
 cd profile_WS  
 FOR / %B in (!HomeProfile!.V2) DO (
SET "WSProfile=%B"  
 FOR /F "usebackq delims" %C IN ('DIR /S /B /O:-D !WSProfile!\AppData\Roaming\Mozilla\Firefox\Profiles\places.sqlite') DO (
SET "FFProfile=%C"  
 echo !FFProfile! home\!HomeProfile!\FireFoxProfile\Default  1>>C:\tmp\3.txt  
 echo C:\tmp\profiles.ini !WSProfile!\AppData\Roaming\Mozilla\Firefox  1>>C:\tmp\4.txt 
) 
) 
)  ELSE (
echo !HomeProfile!\FireFoxProfile\Default  1>>C:\tmp\5.txt  
 FOR / %B IN (!HomeProfile!.V2) DO (
SET "WSProfile=%B"  
 FOR /F "usebackq delims" %C IN ('DIR /S /B /O:-D !WSProfile!\AppData\Roaming\Mozilla\Firefox\Profiles\places.sqlite') DO (
SET "FFProfile=%C"  
 echo !FFProfile! home\!HomeProfile!\FireFoxProfile\Default  1>>C:\tmp\6.txt  
 echo C:\tmp\profiles.ini !WSProfile!\AppData\Roaming\Mozilla\Firefox  1>>C:\tmp\7.txt 
) 
) 
) 
) 

V:\home>(
SET "WSProfile=testuser1.V2"  
 FOR /F "usebackq delims" %C IN ('DIR /S /B /O:-D !WSProfile!\AppData\Roaming\Mozilla\Firefox\Profiles\places.sqlite') DO (
SET "FFProfile=%C"  
 echo !FFProfile! home\!HomeProfile!\FireFoxProfile\Default  1>>C:\tmp\3.txt  
 echo C:\tmp\profiles.ini !WSProfile!\AppData\Roaming\Mozilla\Firefox  1>>C:\tmp\4.txt 
) 
) 

V:\home>(
SET "FFProfile=DIR"  
 echo !FFProfile! home\!HomeProfile!\FireFoxProfile\Default  1>>C:\tmp\3.txt  
 echo C:\tmp\profiles.ini !WSProfile!\AppData\Roaming\Mozilla\Firefox  1>>C:\tmp\4.txt 
) 

What caught my eye starts at these lines:

cd profile_WS  
 FOR / %B in (!HomeProfile!.V2) DO (
SET "WSProfile=%B" 

解决方案

If you take a look at the first of the following line:

FOR /D %%B in (!HomeProfile!.V2) DO (

That should resolve to %%A.V2 so you are effectively just setting WSProfile to \\srv-xyz\users\home\profile_WS\testuser1.V2 and could probably, since your current directory should at that point be \\srv-xyz\users\home\profile_WS, just use Set "WSProfile=%CD%\%%A.V2".

There appears to be a difference between the steps you are taking depending upon the existence of the FirefoxProfile folder under the user’s home directory. If it doesn't exist you are changing directory to profile_WS and never returning from there which will create issues in the rest of your script. If it does exist you are staying in home.

Edit

I have decided to post this despite it not being a solution to the OP's question as much of it may prove useful to them and all to many others.

This attempts to find all of the active Firefox profiles for all of the standard local users. It saves those profile paths to variables in the format %UsersName[i]% where i can be an increasing integer for each users profile found. (Those variables are also printed to the screen for information purposes).

@Echo Off
For /F "UseBackQ Tokens=1* Delims==" %%A In (
    `WMIc Path Win32_UserProfile Where "Special!='True'" Get LocalPath /Value`
) Do For /F "Delims=" %%C In ("%%B") Do Call :Users "%%C"
If Defined i If Not "%i%"=="0" (SetLocal EnableDelayedExpansion
    For /L %%A In (1,1,%i%) Do Echo( %%!UN![%%A]%% = !%UN%[%%A]!
    EndLocal
    Echo( Press any key to exit...
    Timeout -1 1>Nul)
Exit/B

:Users
Set "PF=%~1\AppData\Roaming\Mozilla\Firefox\"
If Not Exist "%PF%" GoTo :EOF
Set "UN=%~nx1"
Set "i=0"
For /F "Tokens=1* Delims==" %%A In (
    'FindStr/R "^Path=.*" "%PF%Profiles.ini"') Do (Set "FP=%%B"
    Call :Profs "%%FP:/=\%%")
GoTo :EOF

:Profs
Set/A "i+=1"
If Exist "%PF%%~1\" (Set "%UN%[%i%]=%PF%%~1") Else Set "%UN%[%i%]=%~1"

The script has not been tested on multi user systems nor on users with multiple profiles, I have written it as a concept only.

这篇关于Windows批处理 - 嵌套循环 - Firefox配置文件迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆