【问题标题】:Batch - Is there a way to set subdirectory attributes with a variable批处理 - 有没有办法用变量设置子目录属性
【发布时间】:2021-07-15 16:46:59
【问题描述】:

所以我在搞乱批处理,我想在我想要的任何子目录中创建一个垃圾文件夹, 我给你看代码你就明白了。

@echo off
set "var=%cd%"
if not EXIST trash goto create
if EXIST trash goto exists

:create
mkdir trash
echo [.ShellClassInfo] IconResource=C:\WINDOWS\System32\SHELL32.dll,63 [ViewState] Mode= Vid= FolderType=Generic > trash\desktop.ini
attrib +h +s trash\desktop.ini
cd ..
attrib +r +s %var%\trash
goto end

:exists
echo Would you like to delete the files inside of the trash? (y/n)
set /p YESNO=" "
if not %YESNO%==y goto end
if %YESNO%==y goto check

:check
echo Please state the correct password
set /p PASSWORD=" "
if %PASSWORD%==1243 del trash\*.* /s /f /q
goto end

:end

在创建区域中,我试图给它一个回收站徽标,你需要给 desktop.ini 提供隐藏和系统属性,这很有效,但是给垃圾文件夹本身提供只读和系统属性没有'不工作。我不能只使用任何旧的直接路径的原因是因为我希望你能够在任何路径中使用它。

【问题讨论】:

  • 你删除your last question有什么特殊原因吗?不仅有指向其他有用信息的链接,我还提供了答案,其中包括一些正确的语法,您未能在上面的新代码中复制这些语法。我还建议,问我一个问题,然后立即删除该帖子,是违反直觉的,因为我无法在已删除的问题中做出回应。您的代码设计得很糟糕,语法不安全,请重新查看我在您之前删除的问题中的回答,并查看其中的每个命令。
  • 好的,我删除了它,因为它说它就像其他问题一样,它告诉我删除它,我可以保留它吗?
  • 我取消了删除。
  • 关于在没有不需要的空格的情况下定义变量的部分是您的主要问题,因此作为副本正确关闭,但是,我的答案包含在内,为您提供一些有用的命令、语法和结构,以大大改善您前进的体验。如果您打开命令提示符窗口,键入命令后跟/?,它将显示其语法、选项和其他信息。
  • 从字面上看,这个问题后面的 4 个问题是另一个 question 询问如何创建文件夹图标。如果您在该网站上搜索[batch-file] folder icon。它会引导你到这个question

标签: batch-file


【解决方案1】:

抱歉,我对stackoverflow不熟悉,所以我无法理解,但我希望这已经足够了。

首先,您在处理变量时有很多错误,使用密码的想法非常好,但不切实际。批次不是为此而制作的。我教授软件安全和渗透测试课程,并且确实有一个类似的脚本供学生破解密码。更不用说删除和恢复文件会有很多问题,用户不需要通过你的程序来做你想做的事,除了使用普通垃圾非常实用。好主意,如果您愿意,我可以免费为您提供帮助,但我认为除了以优惠、随意和个性化的方式处理内容之外,它不会有太大用处。

好吧,脚本中的第一个错误是没有在 desktop.ini 代码之间留出空格,也没有使用 attrib 正确为其分配值,也可能没有使用正确的垃圾桶图标代码。

Windows 的 desktop.ini 文件存在一些错误并且延迟很大,但您需要执行以下操作:

@echo off
set "var=%cd%"
if not EXIST trash goto create
if EXIST trash goto exists

:create
mkdir trash
set katorn=%random%
md "%temp%\%katorn%"
:: As we can see bellow, SHELL32.dll,31 means empty trash ico, SHELL32.dll,32 means full trash ico.
(
echo [.ShellClassInfo]
echo IconResource=C:\Windows\system32\SHELL32.dll,31
echo [ViewState]
echo Mode=
echo Vid=
echo FolderType=Generic
)>"%temp%\%katorn%\desktop.ini"
attrib +S +H "%temp%\%katorn%\desktop.ini"
del /q /f /s "%var%\trash\desktop.in*"
move "%temp%\%katorn%\desktop.ini" "%var%\trash"
rd /s /q "%temp%\%katorn%"
attrib +r +s %var%\trash
goto end

:exists
echo Would you like to delete the files inside of the trash? (y/n)
set /p YESNO=" "
if not %YESNO%==y goto end
if %YESNO%==y goto check

:check
echo Please state the correct password
set /p PASSWORD=" "
if %PASSWORD%==1243 del trash\*.* /s /f /q
goto end

:end

您也可以插入一个 VBScript 来制作快捷方式,然后插入图标。

Set WshShell = CreateObject ("Wscript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
Set Shortcut = WshShell.CreateShortcut(strDesktop + "\ShortcutName.lnk")
Shortcut.WindowStyle = "4" 
Shortcut.IconLocation = "X:\ICoLOcation\icon.ico"
Shortcut.TargetPath = "ProgramToBeOpened\program.exe"
Shortcut.Save

您可以使用批处理来编写和执行脚本。

@echo off
:loop
set /p "schname=Shortcut name: "
set /p "schlocation=Shortcut Location: "
set /p "schdestination=Shortcut Destination: "
set /p "schicon=Icon Location: "
set "schname=%schname:"=%"
set "schlocation=%schlocation:"=%"
set "schdestination=%schdestination:"=%"
set "schicon=%schicon:"=%"
echo >>"%temp%\shortcut.vbs" Set WshShell = CreateObject ("Wscript.Shell")
echo >>"%temp%\shortcut.vbs" strDesktop = "%schdestination%"
echo >>"%temp%\shortcut.vbs" Set Shortcut = WshShell.CreateShortcut(strDesktop + "\%shcname%.lnk")
echo >>"%temp%\shortcut.vbs" Shortcut.WindowStyle = "4" 
echo >>"%temp%\shortcut.vbs" Shortcut.IconLocation = "%schicon%"
echo >>"%temp%\shortcut.vbs" Shortcut.TargetPath = "%schlocation%"
echo >>"%temp%\shortcut.vbs" Shortcut.Save
cscript "%temp%\shortcut.vbs"
del /q /f "%temp%\shortcut.vbs"
goto loop

希望这会有所帮助,
K.

【讨论】:

  • 大声笑,我这样做是为了练习,而不是为了任何实用的项目
  • 在单独的驱动器上可能会有所帮助,因为通常删除它们会立即永久删除它
  • 您可以在驱动器的垃圾箱配置中更改它。
  • 如果有帮助,请考虑将其标记为已接受的答案。
  • @Connor 答案对你有帮助吗?
猜你喜欢
  • 2021-10-07
  • 1970-01-01
  • 2020-01-19
  • 2011-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
相关资源
最近更新 更多