-1

我目前正在尝试获取从旧机器上的配置文件中保存的项目列表,然后发送到新机器和配置文件,因为配置文件是相同的。

在 Windows 7 上,该位置%AppData%\Microsoft\Sticky Notes存储.snt便签。该位置一直存在到 Windows 10 1511。

在 Windows 10 1607 上,它已移至%LocalAppData%\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState" and changed toplum.sqlite`。

如果您从 Windows 10 1607 之前的任何版本升级,它将在其中创建一个Legacy文件夹ThresholdNotes.snt,并将转换为plum.sqlite.

我正在写一个嵌套的.bat,它将:

如果新计算机上的旧便签位置存在
    检查拉取.snt文件,然后复制
ELSE
    检查拉取.snt file

如果不存在新的旧位置,
    则创建然后复制并重命名.snt以转换
ELSE
    副本,重命名

如果旧的便签位置不存在,
    则检查新.sqlite文件
否则
    将新文件复制到新位置

否则说没有检测到。

但似乎我可能写错了或其他什么,因为我已经暂停,.bat但它只是在运行时立即关闭。

这是当前的 Pull 部分,它在其中检索.sntor .sqlite。首先是变量,然后是实际操作部分。

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"

^^这部分似乎工作得很好,没有任何问题

这是推送部分,这似乎是我遇到问题的地方,但可能是它的格式?首先是变量,然后是实际操作部分。

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" 
4

2 回答 2

1

除了您遇到的语法问题(SPACE条件表达式和左括号之间缺少if exist "%somepath%" (; 甚至两个左括号一次,if exist "%somepath%"(()之外,我相信您的问题是您使用的缩进和括号的逻辑和误导方式。

让我们假设以下代码片段:

@echo off
set "X=" & set "Y="
set /P X="X="
set /P Y="Y="

if defined X if defined Y (
    echo X=%X%, Y=%Y%
) else echo X and/or Y is empty

X如果变量为空,您希望返回什么?

让我们看一下函数中相同但编写方式不同的代码(这次跳过变量定义/提示):

if defined X (
    if defined Y (
        echo X=%X%, Y=%Y%
    ) else (
        echo X and/or Y is empty
    )
)

您现在可能会注意到,如果变量为空,则不会返回任何内容X

固定代码可能如下所示:

if defined X (
    if defined Y (
        echo X=%X%, Y=%Y%
    ) else (
        echo Y is empty
    )
) else (
    echo X is empty, Y not checked
)
于 2018-04-27T16:30:19.133 回答
0
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"

那就是拉动部分。只是通过引用和使用 std 变量来更改路径。如果需要,添加%LocalAppData%未定义的检查并在 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"
    )
)

在合适的地方用变量替换文字路径。缩进保持一致,因此可以更轻松地配对打开和关闭大括号。用 just替换过时else if的检查else

如果需要,引用稍后完成的路径允许扩展路径。

如果Echo "No Sticky Notes Detected". 如果您确实想退出,则可以使用:

>&2 Echo "No Sticky Notes Detected"
exit /b 1

所以回显转到标准错误并以错误级别 1 退出。

代码未经测试,因此请检查我的更改。

于 2018-04-27T16:55:55.263 回答