批处理的嵌套IF语句 [英] Nested IF Statements for batch
问题描述
我目前正在尝试从旧计算机上的配置文件中获取保存的项目列表,然后发送到新计算机和配置文件,因为该配置文件是相同的。
在Windows 7上,位置%AppData%\ Microsoft \ Sticky Notes
存储 .snt
便利贴。该位置本身存在于Windows 10 1511中。
在Windows 10 1607上,它已移至%LocalAppData%\Packages \ Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState并更改为
plum.sqlite`。
如果您从早于Windows 10 1607的任何内容进行升级,它将具有在其中创建了 Legacy
文件夹,其中包含 ThresholdNotes.snt
,并将转换为 plum .sqlite
。
我正在写一个嵌套的 .bat
,它将:
如果新计算机上的旧便签位置存在
检查已拉 .snt
文件,然后复制
ELSE
检查是否已拉 .snt文件
如果是新的遗留位置不存在
创建然后复制并重命名 .snt
转换
ELSE
copy,rename
如果旧的便签位置不存在
那么che ck for new .sqlite
file
ELSE
将新文件复制到新位置
否则说没有检测到。
但似乎我写错了什么或因为我在 .bat中暂停了一下
但它只是在运行时立即关闭。
这是当前的Pull部分,它检索 .snt
或 .sqlite
。首先是变量,然后是实际的动作部分。
REM保存用户粘滞便笺
设置StickyNotes =%userprofile%\\ \\ AppData\Roaming\Microsoft \Sticky Notes \ StickyNotes.snt
设置FlashStickyNotes =%~dp0%USERNAME%\ StickyNotes
REM保存用户粘滞便笺来自赢10 1607+
Set StickyNotesWin10 =%userprofile%\ AppData \ Local \Packages \ Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe \ LocalState \ plum.sqlite
Set FlashStickyNotesWin10 =%~dp0% USERNAME%\ StickyNotesWin10
标题拉动StickyNotes
如果存在%StickyNotes%(xcopy%StickyNotes %% FlashStickyNotes%/ f / y)ELSE如果存在%StickyNotesWin10%(
xcopy%StickyNotesWin10 %% FlashStickyNotesWin10%/ f / y)否则回声未检测到粘滞便笺
^^这部分似乎工作正常,没有任何问题
这是Push部分,这是我似乎有的地方麻烦,但也许它的格式?首先是变量,然后是实际的动作部分。
REM保存用户粘滞便笺
设置StickyNotes =%userprofile%\\ \\ AppData \Roaming\Microsoft \Sticky Notes \
设置FlashStickyNotes =%~dp0%USERNAME%\ StickyNotes\StickyNotes.snt
REM保存用户粘性来自Win的注释10 1607+
Set StickyNotesWin10 =%userprofile%\ AppData \ Local \Packages \ Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe \ LocalState \
设置FlashStickyNotesWin10 =%~dp0%USERNAME %\ StickyNotesWin10 \plum.sqlite
标题推送StickyNotes
REM如果新计算机上存在旧便签位置,则检查拉出.snt文件,然后复制ELSE
REM如果存在新的便签位置,请检查已拉出的.snt文件,如果新旧位置不存在,则创建然后复制并重命名.snt进行转换,否则复制,重命名
REM如果旧便签位置不存在,然后检查新的.sqlite fi le,如果存在,则将新文件复制到新位置ELSE
REM否则说没有检测到
如果存在%userprofile%\ AppData \Roaming\Microsoft \Sticky Notes \(如果存在%FlashStickyNotes%(xcopy%FlashStickyNotes %% StickyNotes%/ F / Y)
如果存在%userprofile%\ AppData \ Local \Packages \ Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe \ LocalState \ (
如果存在%FlashStickyNotes%(
如果不存在%userprofile%\ AppData \ Local \ Packack\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe \ LocalState \ Legacy(
) mkdir%userprofile%\ AppData \ Local \ Packack\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe \ LocalState \ Legacy
xcopy%FlashStickyNotes%%userprofile%\ AppData \ Local \ Packack\Microsoft .MicrosoftStickyNotes_8wekyb3d8bbwe \ LocalState \ Legacy \ThresholdNotes.snt/ F / Y
)否则如果exi st%userprofile%\ AppData \ Local \ Packack\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe \ LocalState \ Legacy((
xcopy%FlashStickyNotes%%userprofile%\ AppData \ Local \ Packages \\ \\ Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\Legacy\ThresholdNotes.snt/ F / Y
)
)
)否则如果不存在%StickyNotes%(
IF存在%FlashStickyNotesWin10%(
复制%FlashStickyNotesWin10 %% StickyNotesWin10%/ Y
)
)
)ELSE Echo未检测到粘滞便笺
Vista之前的REM OS没有LocalAppData变量
如果未定义LocalAppData设置LocalAppData =%AppData%\..\ Local
REM保存用户粘滞便笺
设置StickyNotes =%AppData%\ Microsoft \ Sticky Notes \ StickyNotes.snt
设置FlashStickyNotes =%~dp0%USERNAME%\ StickyNotes
REM保存用户Stic ky Notes from Win 10 1607+
Set StickyNotesWin10 =%LocalAppData%\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\plum.sqlite
Set FlashStickyNotesWin10 =%~dp0%USERNAME%\ StickyNotesWin10
标题拉动StickyNotes
如果存在%StickyNotes%(
xcopy%StickyNotes%%FlashStickyNotes%/ f / y
)否则,如果存在%StickyNotesWin10%(
xcopy%StickyNotesWin10%%FlashStickyNotesWin10%/ f / y
)否则回声没有检测到粘滞便笺
这是拉动部分。只需使用引号更改路径并使用
的std变量。添加%LocalAppData%
未定义检查是否需要
并在Vista之前在操作系统上使用替代。
Vista之前的REM OS没有LocalAppData变量
如果没有定义LocalAppData设置LocalAppData =%AppData%\..\ Local
REM保存用户粘滞便笺
设置StickyNotes =%AppData%\ Microsoft \ Sticky Notes
设置FlashStickyNotes =%~dp0%USERNAME%\ StickyNotes\StickyNotes.snt
REM从Win 10 1607+
保存用户粘滞便笺设置StickyNotesWin10 =%LocalAppData%\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe \ LocalState
设置FlashStickyNotesWin10 =%~dp0%USERNAME%\ StickyNotesWin10 \plum。 sqlite
标题推送StickyNotes
REM如果新计算机上的旧便签位置存在,则检查拉出.snt文件,然后复制ELSE
REM如果新粘滞便笺位置存在,检查拉动的.snt文件,如果新的遗留位置不存在,则创建然后创建py和重命名.snt转换,否则复制,重命名
REM如果旧的粘滞便笺位置不存在,则检查新的.sqlite文件,如果存在则将新文件复制到新位置ELSE
REM否则说没有检测到
如果存在%StickyNotes%(
如果存在%FlashStickyNotes%(
xcopy%FlashStickyNotes%%StickyNotes%/ F / Y
)
)否则如果存在%StickyNotesWin10%\(
如果存在%FlashStickyNotes%(
IF不存在%StickyNotesWin10%\ Legacy(
) mkdir%StickyNotesWin10%\ Legacy
xcopy%FlashStickyNotes%%StickyNotesWin10%\ Legacy \ThresholdNotes.snt/ F / Y
)else(
xcopy% FlashStickyNotes%%StickyNotesWin10%\ Legacy \ThresholdNotes.snt/ F / Y
)
)
)else(
如果存在%FlashStickyNotesWin10%(
复制%FlashStickyNotesWin10%%StickyNotesWin10%/ Y
)其他(
回声没有检测到粘滞便笺
)
)
替换为文字路径适当的变量。缩进使得
一致,因此打开和关闭括号可以更容易配对。如果仅用 else
检查
,则替换过时的 else。
引用稍后完成的路径,允许在需要时扩展路径。
如果 Echo未检测到粘滞便笺,则不确定是否退出脚本
。
如果你想退出,可以使用:
>& 2 Echo没有检测到粘滞便笺
退出/ b 1
所以回显到stderr并退出errorlevel 1。 / p>
代码未经过测试,请检查我的更改。
I am currently trying to get a list of items saved from a profile on an old machine and then sent to the new machine and profile, being that the profile is the same.
On Windows 7 the location %AppData%\Microsoft\Sticky Notes
stores the .snt
for sticky notes. That location exists natively all the way to Windows 10 1511.
On Windows 10 1607 it has been moved to %LocalAppData%\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState" and changed to
plum.sqlite`.
If you upgraded from anything earlier than Windows 10 1607 it will have made a Legacy
folder with ThresholdNotes.snt
in it and that will convert over to the plum.sqlite
.
I am writing a nested .bat
which will:
IF old sticky notes location on the new computer exists
check for pulled .snt
file, then copy over
ELSE
check for pulled .snt file
IF new legacy location not exist
create then copy and rename .snt
to convert
ELSE
copy,rename
IF old sticky notes location not exist
then check for new .sqlite
file
ELSE
copy new file to new location
Otherwise say there are none detected.
But it seems I may be writing it wrong or something because I have placed a pause in the .bat
but it just closes immediately when ran.
Here is the current Pull part where it retrieves the .snt
or .sqlite
. Variables first and then the actual action part.
REM Saves Users Sticky Notes
Set StickyNotes="%userprofile%\AppData\Roaming\Microsoft\Sticky Notes\StickyNotes.snt"
Set FlashStickyNotes="%~dp0%USERNAME%\StickyNotes"
REM Saves Users Sticky Notes From Win 10 1607+
Set StickyNotesWin10="%userprofile%\AppData\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\plum.sqlite"
Set FlashStickyNotesWin10="%~dp0%USERNAME%\StickyNotesWin10"
Title Pulling StickyNotes
if exist %StickyNotes% ( xcopy %StickyNotes% %FlashStickyNotes% /f /y ) ELSE if exist %StickyNotesWin10% (
xcopy %StickyNotesWin10% %FlashStickyNotesWin10% /f /y ) else Echo "No Sticky Notes Detected"
^^This part seems to work just fine and not have any problems
Here's the Push part and this is where I seem to have trouble but maybe its formatting? Variables first and then the actual action part.
REM Saves Users Sticky Notes
Set StickyNotes="%userprofile%\AppData\Roaming\Microsoft\Sticky Notes\"
Set FlashStickyNotes="%~dp0%USERNAME%\StickyNotes\StickyNotes.snt"
REM Saves Users Sticky Notes From Win 10 1607+
Set StickyNotesWin10="%userprofile%\AppData\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\"
Set FlashStickyNotesWin10="%~dp0%USERNAME%\StickyNotesWin10\plum.sqlite"
Title Pushing StickyNotes
REM if old sticky notes location on the new computer exists, then check for pulled .snt file, then copy over ELSE
REM if new sticky notes location exists, check for pulled .snt file, if new legacy location not exist, then create and then copy and rename .snt to convert, else copy,rename
REM if old sticky notes location not exist, then check for new .sqlite file, if exist then copy new file to new location ELSE
REM otherwise say there are none detected
IF exist "%userprofile%\AppData\Roaming\Microsoft\Sticky Notes\"( IF exist "%FlashStickyNotes%"( xcopy %FlashStickyNotes% %StickyNotes% /F /Y )
IF exist "%userprofile%\AppData\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\"(
IF exist "%FlashStickyNotes%"(
IF not exist "%userprofile%\AppData\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\Legacy"(
mkdir "%userprofile%\AppData\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\Legacy"
xcopy %FlashStickyNotes% "%userprofile%\AppData\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\Legacy\ThresholdNotes.snt" /F /Y
) else IF exist "%userprofile%\AppData\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\Legacy"((
xcopy %FlashStickyNotes% "%userprofile%\AppData\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\Legacy\ThresholdNotes.snt" /F /Y
)
)
) else IF not exist "%StickyNotes%" (
IF exist %FlashStickyNotesWin10% (
copy %FlashStickyNotesWin10% %StickyNotesWin10% /Y
)
)
) ELSE Echo "No Sticky Notes Detected"
REM OS before Vista have no LocalAppData variable
if not defined LocalAppData set "LocalAppData=%AppData%\..\Local"
REM Saves Users Sticky Notes
Set StickyNotes=%AppData%\Microsoft\Sticky Notes\StickyNotes.snt
Set FlashStickyNotes=%~dp0%USERNAME%\StickyNotes
REM Saves Users Sticky Notes From Win 10 1607+
Set StickyNotesWin10=%LocalAppData%\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\plum.sqlite
Set FlashStickyNotesWin10=%~dp0%USERNAME%\StickyNotesWin10
Title Pulling StickyNotes
if exist "%StickyNotes%" (
xcopy "%StickyNotes%" "%FlashStickyNotes%" /f /y
) else if exist "%StickyNotesWin10%" (
xcopy "%StickyNotesWin10%" "%FlashStickyNotesWin10%" /f /y
) else Echo "No Sticky Notes Detected"
That is the Pull part. Just changed paths with quoting and use
of std variables. Add %LocalAppData%
not defined check if needed
and use a substitute on OS before Vista.
REM OS before Vista have no LocalAppData variable
if not defined LocalAppData set "LocalAppData=%AppData%\..\Local"
REM Saves Users Sticky Notes
Set StickyNotes=%AppData%\Microsoft\Sticky Notes
Set FlashStickyNotes=%~dp0%USERNAME%\StickyNotes\StickyNotes.snt
REM Saves Users Sticky Notes From Win 10 1607+
Set StickyNotesWin10=%LocalAppData%\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState
Set FlashStickyNotesWin10=%~dp0%USERNAME%\StickyNotesWin10\plum.sqlite
Title Pushing StickyNotes
REM if old sticky notes location on the new computer exists, then check for pulled .snt file, then copy over ELSE
REM if new sticky notes location exists, check for pulled .snt file, if new legacy location not exist, then create and then copy and rename .snt to convert, else copy,rename
REM if old sticky notes location not exist, then check for new .sqlite file, if exist then copy new file to new location ELSE
REM otherwise say there are none detected
IF exist "%StickyNotes%" (
IF exist "%FlashStickyNotes%" (
xcopy "%FlashStickyNotes%" "%StickyNotes%" /F /Y
)
) else IF exist "%StickyNotesWin10%\" (
IF exist "%FlashStickyNotes%" (
IF not exist "%StickyNotesWin10%\Legacy" (
mkdir "%StickyNotesWin10%\Legacy"
xcopy "%FlashStickyNotes%" "%StickyNotesWin10%\Legacy\ThresholdNotes.snt" /F /Y
) else (
xcopy "%FlashStickyNotes%" "%StickyNotesWin10%\Legacy\ThresholdNotes.snt" /F /Y
)
)
) else (
IF exist "%FlashStickyNotesWin10%" (
copy "%FlashStickyNotesWin10%" "%StickyNotesWin10%" /Y
) else (
Echo "No Sticky Notes Detected"
)
)
Literal paths replaced with variables where suitable. Indenting made consistent
so opening and closing braces can be paired easier. Replaced obsolete else if
checks
with just else
.
Quoting paths done later as allows extending paths if needed.
Unsure if you exit from the script if Echo "No Sticky Notes Detected"
.
If you do want to exit then can use:
>&2 Echo "No Sticky Notes Detected"
exit /b 1
so the echo goes to stderr and exit with errorlevel 1.
Code is not tested so please check my changes.
这篇关于批处理的嵌套IF语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!