AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 10938
In Process
lajos
lajos
Asked: 2009-05-22 11:05:08 +0800 CST2009-05-22 11:05:08 +0800 CST 2009-05-22 11:05:08 +0800 CST

在 .bat 脚本中查找用户的文档文件夹

  • 772

从批处理脚本中找到用户在 XP 和 Vista 上的 Documents 文件夹的最佳方法是什么?假设它是 %USERPROFILE%\Documents 是否安全?

windows-vista windows-xp batch-file
  • 10 10 个回答
  • 15335 Views

10 个回答

  • Voted
  1. user55644
    2010-09-30T07:39:25+08:002010-09-30T07:39:25+08:00

    所以我的最终版本是这样的:

    FOR /F "tokens=3 delims= " %%G IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') DO (SET docsdir=%%G)

    where the character between delims= and the following " is a single tab. Make sure your editor emits a tab and not spaces.

    EDIT: On Windows 7 (and maybe all windows) you shouldn't specify delims= at all as it defaults to which is the whitespace used inbetween the tokens and not just a tab.

    • 8
  2. WerkkreW
    2009-05-22T11:21:01+08:002009-05-22T11:21:01+08:00

    可以在此处找到环境变量的完整参考,在microsoft 站点上,也可以在注册表项中找到它。

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
    String value: Personal
    

    如果“我的文档”文件夹不在标准位置,则从注册表项中提取信息可能是最可靠的方法。

    • 7
  3. fsp
    2009-09-02T06:55:34+08:002009-09-02T06:55:34+08:00

    它只是英文窗口上的“我的文档”等。如果您使用的是另一种语言,则路径名是“翻译的”(Vista 除外)

    • 1
  4. JonathanDavidArndt
    2015-06-26T07:52:14+08:002015-06-26T07:52:14+08:00

    The best way to determine the location of My Documents is from the Windows Registry.

    Several other answers and comments on this page have made reference to using "reg query". Below is the correct implementation that takes into account spaces in the path, as well as different versions of Windows:

    for /f "tokens=1,2*" %%A in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal" 2^>nul') do (
       set RNAME=%%A
       set RTYPE=%%B
       set RDATA=%%C
    )
    

    Here is the one-liner for script writers:

    for /f "tokens=1,2*" %%A in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal" 2^>nul') do set MY_DOCS_ROOT=%%C
    

    This does not take into account localization or internationalization. This has not been tested on non-English versions of Windows. Comments on that topic are welcome.

    This does work for Windows XP, Windows Vista, Windows 7, and Windows 8.


    Note: Using the asterisk in the tokens= option is important for Windows XP, which usually contains spaces in the path for My Documents.

    Note: If using implicit variables like %%B and %%C seems a little strange, you may have a look at this article:

    http://ss64.com/nt/for_f.html

    tokens=3* will process the third token and the 4th + all subsequent items, this can also be written as tokens=3,*

    Each token specified will cause a corresponding parameter letter to be allocated. The letters used for tokens are case sensitive.

    If the last character in the tokens= string is an asterisk, then additional parameters are allocated for all the remaining text on the line.

    The first variable is declared in the FOR statement and subsequent variables will be implicitly declared via the tokens= option.

    The linked article gives the exact ordering of the variables that will be declared implicitly, but it is essentially alphabetic.

    (With three tokens, by declaring %%A in the FOR statement, %%B and %%C will be declared implicitly. In the same way, by declaring %%X in the FOR statement, %%Y and %%Z will be declared implicitly.)

    • 1
  5. squillman
    2009-05-22T11:10:00+08:002009-05-22T11:10:00+08:00

    那将是系统知道的地方(尽管它是 XP 中的“我的文档”),但是您必须自己判断这对于您的环境是否是一个安全的假设。但是,如果您使用“我的文档”重定向,则该变量应设置为您重定向到的任何内容,默认设置为 XP 中的 c:\documents 和 settings[username] 或 Vista 中的 c:\users[username]。

    • 0
  6. Alexander Taran
    2009-05-22T11:11:54+08:002009-05-22T11:11:54+08:00

    对于 xp 来说不安全,这是我的文档并且可以本地化。

    • 0
  7. Jack B Nimble
    2009-05-22T11:13:48+08:002009-05-22T11:13:48+08:00

    Windows XP 将其命名为“我的文档”,Vista 将其命名为“文档”。您可以输入一个脚本来确定您正在运行的操作系统。

    @echo off
    IF EXIST "%USERPROFILE%\My Documents" (
        echo Windows XP
    ) ELSE (
        echo Vista
    )
    
    • 0
  8. JFV
    2009-05-22T11:14:09+08:002009-05-22T11:14:09+08:00

    在 Windows Vista 中,它是

    %USERPROFILE%\Documents
    

    但在 Windows XP 中,它是

    %USERPROFILE%\My Documents
    

    这是两个不同操作系统之间的唯一区别。

    并且不要忘记在 Windows Vista 中Videos,Music和Photos目录都在C:\Users\[%USERNAME]

    • 0
  9. dod destroyer
    2012-03-20T23:37:03+08:002012-03-20T23:37:03+08:00
    :getuserdoc folder from registry
    set idkey="HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
    call:fetchvalue errorlevelvariable %idkey% Personal
    goto:eof
    errorlevel is set if fail
    :fetchvalue
     set /a %1=0
     REG query %2 /v %3
     if ERRORLEVEL 1 (set /a %1=1&goto :eof)
     FOR /F "tokens=3* skip=2 delims=   " %%A IN ('REG QUERY %2 /v %3') DO SET %3="%%A"
    goto:eof
    

    Use Button GETSource as this wenpage cant display answer characters are removed!

    • 0
  10. aeglasin
    2015-07-30T00:40:21+08:002015-07-30T00:40:21+08:00

    For all who, like me, stumble upon this post while searching for a way to simply get the users documents folder, having next to zero experience about batch files, but want to use the great solution by Werkkrew or (probably?) the derived version by user55644, that's how I made it working on my windows 7 Pc:

    echo off
    
    setlocal enableextensions enabledelayedexpansion
    
    
    FOR /F "tokens=3" %%G IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') DO (SET targetdir=%%G)
    echo on
    
    echo %targetdir%
    
    • 0

相关问题

  • Windows Admin 无法更改时间

  • 如何重新安装 Windows Movie Maker (XP)

  • 设置新的 Windows Vista 机器时需要调整的最关键设置是什么?[关闭]

  • 对于 SME 规模的安装,您在 Vista 中禁用了哪些功能(如果有)?

  • 如何诊断在关机时挂起的 Windows PC?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    Windows 中执行反向 DNS 查找的命令行实用程序是什么?

    • 14 个回答
  • Marko Smith

    如何检查 Windows 机器上的端口是否被阻塞?

    • 4 个回答
  • Marko Smith

    我应该打开哪个端口以允许远程桌面?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    kch 如何更改我的私钥密码? 2009-08-06 21:37:57 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve