重命名具有不同名称的文件夹还是将其删除? [英] Renaming folders with different names or removing them?

查看:65
本文介绍了重命名具有不同名称的文件夹还是将其删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到一种使用批处理文件的方法,以查找并重命名文件夹.有问题的文件夹位于不同的用户配置文件中,因此我很难确定可以使用哪些通配符或变量.

I need to find a way using a batch file, to find and rename a folder. The folder(s) in question are located in different user profiles, so I'm having trouble figuring out what wildcard or variable I can use.

在更详细的方法上,我有一个偶然被部署到大量计算机上的文件夹.该文件夹已部署到每个用户Start Menu,不包含任何内容.因此,我的服务台正在接到有关此新的测试"文件夹的呼叫,该文件夹在一夜之间出现,里面没有任何内容或空白文本文件.虽然有些用户被告知要删除它,但其他用户只是希望我们对此做些事情.由于Start Menu位于每个用户配置文件中,因此我需要一种在所有用户配置文件Start Menu位置中进行递归搜索的方法,然后查找并重命名或删除该文件夹.

On a more detailed approach, I have a folder that was accidentally deployed to a large number of machines. The folder was deployed to each users Start Menu, and contains nothing. So my help desk is getting calls about this new "test" folder that showed up over night, that has nothing or a blank text file inside it. While some users have been told to delete it, others just want us to do something about it. Because the Start Menu is located in each users profile, I need a way to search recursively in all user profiles Start Menu location, then find and rename or remove that folder.

无论位置如何,问题中的所有文件夹均以相同的5个字符开头,但在()中以用户名结尾.我尝试使用*,但是我的命令用引号引起来,所以它用字面意义代替了寻找xxxxx *的原因,在xxxxx *中,*随配置文件的不同而不同.

Regardless of the location, all of the folders in questions start with the same 5 characters, but they end with the user name in (). I tried using an *, but my command was in quotes, so it took that literally instead of looking for xxxxx *, where the * differs from profile to profile.

这是可能的,还是我甚至混淆了最好的?

Is this possible, or have I confused even the best?

这是我要做的工作的副本.前两行确定它是XP还是Win7,并根据IF EXIST的结果告诉它从哪里开始.然后,它试图在每个用户配置文件Start Menu中找到任何名为TEST的文件夹/目录.

Here is a copy of what I am trying to make work. The first two lines determine if it is XP or Win7 and tells it where to start based on the outcome of the IF EXIST. Then it is trying to find any folder/directory named TEST, located in each users profile Start Menu.

IF EXIST "C:\Documents and Settings\" PUSHD "C:\Documents and Settings\"
IF EXIST "C:\Users\" PUSHD "C:\Users\"
FOR /F "tokens=*" %%G in ('dir /a:d-s-h /b') do (
    IF EXIST "%%G\Start Menu\Programs\TEST" rmdir /q /s "%%G\Start Menu\Programs\TEST"
    IF EXIST "%%G\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\TEST" rmdir /q /s "%%G\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\TEST"
)
POPD

问题是完整的文件夹名称是TEST (user name).我尝试放入Test *,但是它不起作用,因为我认为它采用引号引起来的名称,而不是使用*作为通配符.因此,每个配置文件中的文件夹名称都不同,我只需要代码键入单词Test,因为其他任何应用程序通常都不会输入该名称,并且不必担心括号内的任何名称.然后,它只需使用 RMDIR 删除该文件夹即可.

The problem is that the full folder name is TEST (user name). I have tried putting in Test *, but it does not work because I believe it is taking the quoted name verbose instead of using * as a wildcard. So the folder name is different in each profile and I need the code to key in only on the word Test because that would not normally be there for any other application, and not be concerned with any of the name within the parentheses. Then it needs to simply delete that folder using RMDIR.

没有VB脚本,此处未批准.批处理文件将使用与推送文件夹相同的方法从服务器推送,但是我们希望它检查所有用户配置文件,因为许多受影响的计算机都没有一个特定的用户.对于任何在登录时登录的用户,该推送都会将该文件夹放置到Start Menu中,并且推送会重复进行30分钟,直到我们注意到有人致电我们的服务台询问有关新文件夹的情况时在开始菜单上显示名为TEST的文件.该修补程序将在一夜之间运行,因此也没有用户登录.

No VB script, not approved here. The batch file would be pushed from a server using the same methods the folder was pushed, but we want it to check all user profiles as a lot of the machines affected do not have just one specific user. The push would have placed the folder into the Start Menu for any user who was logged in at the time of the push, and the push repeated itself for 30 minutes until we noticed what was happening when someone called our Help Desk about a new folder called TEST showing up on the Start menu. The fix would be run overnight, so no users would be logged in either.

推荐答案

以下是此任务的批处理批处理代码:

Here is a commented batch code for this task:

@echo off
rem Make the parent directory of the profile directory of current user
rem the current directory if this directory exists on current machine.
if exist "%USERPROFILE%\..\" pushd "%USERPROFILE%\..\" & goto RemoveDirectories

rem Make the standard user profiles directory on English Windows XP
rem the current directory if this directory exists on current machine.
if exist "C:\Documents and Settings" pushd "C:\Documents and Settings" & goto RemoveDirectories

rem Make the standard user profiles directory on Windows Vista or later
rem Windows in any language if this directory exists on current machine.
if exist "C:\Users" pushd "C:\Users" & goto RemoveDirectories

goto :EOF

:RemoveDirectories
rem Find in all subdirectories a directory starting with "Test",
rem having next a space character, then an opening parenthesis,
rem any other string (user name) and last a closing parenthesis.

for /F "tokens=*" %%G in ('dir /A:D /B /S "Test (*)" 2^>nul') do rmdir /Q /S "%%~G" 2>nul

rem Restore the previous current directory.
popd

与您的方法相比,重要的区别是命令 DIR 中的选项/S可从C:\Documents and SettingsC:\Users所有子目录中获取感兴趣的目录

In comparison to your approach the important difference is option /S on command DIR to get the directories of interest from all subdirectories of C:\Documents and Settings or C:\Users.

如果目录树中除开始菜单之外的其他任何地方都存在目录Test (*),则这可能很关键.您可以在删除目录之前评估要删除的目录是否包含在路径Start Menu中,只是要确保删除正确的目录.但这仅在所有计算机都运行英语Windows的情况下有效.

This could be critical if there is a directory Test (*) anywhere else in the directory tree than the start menu. You could evaluate if directory to remove contains in path Start Menu before deleting the directory, just to make sure to delete the right directory. But that works only if all machines are running an English Windows.

@echo off
rem Make the parent directory of the profile directory of current user
rem the current directory if this directory exists on current machine.
if exist "%USERPROFILE%\..\" pushd "%USERPROFILE%\..\" & goto RemoveDirectories

rem Make the standard user profiles directory on English Windows XP
rem the current directory if this directory exists on current machine.
if exist "C:\Documents and Settings" pushd "C:\Documents and Settings" & goto RemoveDirectories

rem Make the standard user profiles directory on Windows Vista or later
rem Windows in any language if this directory exists on current machine.
if exist "C:\Users" pushd "C:\Users" & goto RemoveDirectories

goto :EOF

:RemoveDirectories
rem Find in all subdirectories a directory starting with "Test",
rem having next a space character, then an opening parenthesis,
rem any other string (user name) and last a closing parenthesis.
rem It is tested for safety if the directory to delete is really
rem in the programs directory of the start menu directory.

setlocal EnableDelayedExpansion
for /F "tokens=*" %%G in ('dir /A:D /B /S "Test (*)" 2^>nul') do (
    set "TestDirectory=%%~G"
    if not "!TestDirectory:\Start Menu\Programs\Test (=!" == "!TestDirectory!" rmdir /Q /S "%%~G" 2>nul
)
endlocal

rem Restore the previous current directory.
popd

两个批处理文件都使用 stderr 重定向到设备 nul ,以抑制根本找不到目录Test (*)引起的错误消息(命令2^>nul dir )或要删除的目录无法删除(命令 rmdir 2>nul),例如,当它是正在运行的任何其他进程的当前目录时.

Both batch files use redirections of stderr to device nul to suppress error messages caused by no directory Test (*) found at all (2^>nul for command dir) or the directory to remove could not be deleted (2>nul for command rmdir), for example when it is the current directory of any other process running.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • popd /?
  • pushd /?
  • rem /?
  • rmdir /?
  • set /?
  • setlocal /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • popd /?
  • pushd /?
  • rem /?
  • rmdir /?
  • set /?
  • setlocal /?

这篇关于重命名具有不同名称的文件夹还是将其删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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