在 Windows 批处理脚本中更改目录

在 Windows 批处理脚本中更改目录

根据我找到的其他一些答案,我可以循环遍历可用的驱动器标签。但是,我似乎无法切换到该驱动器:

for /f "skip=1 delims=" %%x in ('wmic logicaldisk get caption') do (
%%x
dir
)

这给了我:

PS C:\> .\something.bat

C:\>for /F "skip=1 delims=" %x in ('wmic logicaldisk get caption') do (
%x
 dir
)

C:\>(
 :
 dir
)
The filename, directory name, or volume label syntax is incorrect.
 Volume in drive C has no label.
 Volume Serial Number is 9CE0-91A0

 Directory of C:\

07/14/2018  09:54 AM             1,586 compact-all.bat
06/25/2018  01:03 PM    <JUNCTION>     Documents and Settings [C:\Users]
07/14/2018  08:03 AM    <DIR>          Fraps
06/25/2018  01:07 PM    <DIR>          Intel
06/25/2018  02:18 PM    <DIR>          NVIDIA
04/11/2018  05:38 PM    <DIR>          PerfLogs
07/13/2018  08:32 AM    <DIR>          Program Files
07/13/2018  04:56 PM    <DIR>          Program Files (x86)
06/25/2018  02:12 PM    <DIR>          Python27
07/14/2018  12:26 PM             7,404 result.txt
07/14/2018  12:26 PM                83 something.bat
06/25/2018  01:08 PM    <DIR>          Users
06/30/2018  02:19 PM    <DIR>          Windows
               3 File(s)          9,073 bytes
              10 Dir(s)  84,364,111,872 bytes free

C:\>(
 :
 dir
)
The filename, directory name, or volume label syntax is incorrect.
 Volume in drive C has no label.
 Volume Serial Number is 9CE0-91A0

 Directory of C:\

07/14/2018  09:54 AM             1,586 compact-all.bat
06/25/2018  01:03 PM    <JUNCTION>     Documents and Settings [C:\Users]
07/14/2018  08:03 AM    <DIR>          Fraps
06/25/2018  01:07 PM    <DIR>          Intel
06/25/2018  02:18 PM    <DIR>          NVIDIA
04/11/2018  05:38 PM    <DIR>          PerfLogs
07/13/2018  08:32 AM    <DIR>          Program Files
07/13/2018  04:56 PM    <DIR>          Program Files (x86)
06/25/2018  02:12 PM    <DIR>          Python27
07/14/2018  12:26 PM             7,404 result.txt
07/14/2018  12:26 PM                83 something.bat
06/25/2018  01:08 PM    <DIR>          Users
06/30/2018  02:19 PM    <DIR>          Windows
               3 File(s)          9,073 bytes
              10 Dir(s)  84,364,111,872 bytes free

C:\>(
 :
 dir
)
The device is not ready.
 Volume in drive C has no label.
 Volume Serial Number is 9CE0-91A0

 Directory of C:\

07/14/2018  09:54 AM             1,586 compact-all.bat
06/25/2018  01:03 PM    <JUNCTION>     Documents and Settings [C:\Users]
07/14/2018  08:03 AM    <DIR>          Fraps
06/25/2018  01:07 PM    <DIR>          Intel
06/25/2018  02:18 PM    <DIR>          NVIDIA
04/11/2018  05:38 PM    <DIR>          PerfLogs
07/13/2018  08:32 AM    <DIR>          Program Files
07/13/2018  04:56 PM    <DIR>          Program Files (x86)
06/25/2018  02:12 PM    <DIR>          Python27
07/14/2018  12:26 PM             7,404 result.txt
07/14/2018  12:26 PM                83 something.bat
06/25/2018  01:08 PM    <DIR>          Users
06/30/2018  02:19 PM    <DIR>          Windows
               3 File(s)          9,073 bytes
              10 Dir(s)  84,364,111,872 bytes free

C:\>(

 dir
)
 Volume in drive C has no label.
 Volume Serial Number is 9CE0-91A0

 Directory of C:\

07/14/2018  09:54 AM             1,586 compact-all.bat
06/25/2018  01:03 PM    <JUNCTION>     Documents and Settings [C:\Users]
07/14/2018  08:03 AM    <DIR>          Fraps
06/25/2018  01:07 PM    <DIR>          Intel
06/25/2018  02:18 PM    <DIR>          NVIDIA
04/11/2018  05:38 PM    <DIR>          PerfLogs
07/13/2018  08:32 AM    <DIR>          Program Files
07/13/2018  04:56 PM    <DIR>          Program Files (x86)
06/25/2018  02:12 PM    <DIR>          Python27
07/14/2018  12:26 PM             7,404 result.txt
07/14/2018  12:26 PM                83 something.bat
06/25/2018  01:08 PM    <DIR>          Users
06/30/2018  02:19 PM    <DIR>          Windows
               3 File(s)          9,073 bytes
              10 Dir(s)  84,364,111,872 bytes free
PS C:\>

但我想要的是它转到驱动器 c:、d: 等并运行命令。

我知道通常我可以做类似的事情

dir %%x

但是我正在编写一个脚本来压缩(压缩)每个驱动器的内容,但它似乎没有正确获取参数,因为当我这样做时它没有遍历所有文件:

compact d:

仅影响 d 的根:

相对

d:
compact

这会影响 d 盘上的每个文件和目录:

答案1

更改驱动器号

当命令提示符目录使用的驱动器号与您需要更改的驱动器号不同时,要更改为特定的驱动器号,只需使用/D带有光盘命令(例如CD /D <DriveLetter>:)在运行继续执行的命令之前更改为不同的驱动器号。


德利姆斯

您应该省略delims="使用为了循环,因为它告诉它空格不是分隔符,而这并不是你想要防止出现的cr/cr/lf问题LotPings评论。

  • FOR /?
        delims=xxx      - specifies a delimiter set.  This replaces the
                          default delimiter set of space and tab.
    

由于它指出空格和制表符的默认分隔符集当您将delims="这样的内容放在等号后面加上结尾的双引号时=",就表示现在没有分隔符了。


袖珍的

自从袖珍的针对目录中的文件运行而不在参数中指定路径,如果适用,您可以先CD /D <Letter>:运行Compact /C以压缩目录中的所有文件,或者Compact /C /S在目录中递归压缩所有文件和文件夹。


批处理脚本

for /f "skip=1" %%x in ('wmic logicaldisk get caption') do (
    CD /D %%x
    Compact /C /S
    <Next or other command>
    )

笔记:<letter>:驱动器号以以下格式列出例如H:。这样CD /D H:就很好了。显然,你会将添加\到迭代的末尾%%x IE%%x\如果需要附加路径,请使用完整路径 ( %%x\folder\path) 作为命令参数例如dir %%x\folder\path

在此处输入图片描述


更多资源

  • 光盘

     Key
       /D : change the current DRIVE in addition to changing folder.
    
  • 袖珍的

    Key    
       /C        Compress the specified files.  Directories will be marked
                 so that files added afterward will be compressed.
    
       /S        Perform the specified operation on files in the given
                 directory and all subdirectories.  
                 Default "dir" is the current directory.
    
  • 我需要一个 compact.exe 的 .bat 并忽略文件类型?

答案2

但是我似乎无法切换到该驱动器

使用以下批处理文件(test.cmd):

@echo off
setlocal enabledelayedexpansion
rem skip=1 to remove the header
rem findstr to remove blank lines
for /f "skip=1" %%d in ('wmic logicaldisk get caption ^| findstr /r /v "^$"') do (
  cd /d %%d\
  dir
  )
endlocal

笔记:

  • 替换dir为您想要在每个驱动器的根目录上运行的命令。

进一步阅读

相关内容