Based on some other answers I found, I can loop through available drive labels. However, I can't seem to change to that drive:
for /f "skip=1 delims=" %%x in ('wmic logicaldisk get caption') do (
%%x
dir
)Which gives me:
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:\>But what I want is it to go to drive c:, d:, etc. and run a command.
I know normally I could do something like
dir %%xbut I'm making a script to compress (compact) the contents of every drive, but it doesn't seem to take the parameter correctly as it doesn't go through all the files when I do:
compact d:which only affect the root of d:
versus
d:
compactwhich affects every file and directory on d:
22 Answers
Change to Drive Letter
To change to a specific drive letter while the command prompt directory is using a different drive letter than the one you need to change to, simply use the /D parameter with the CD command (e.g. CD /D <DriveLetter>:) to change to a different drive letter before running proceeding commands.
Delims
You should omit using the delims=" in the FOR loop as that tells it that space is not a delimiter which is not what you want to prevent that cr/cr/lf issue as per the LotPings comment.
FOR /?delims=xxx - specifies a delimiter set. This replaces the default delimiter set of space and tab.
Since it states the default delimiter set of space and tab when you put delims=" just like that with the ending double quote after the equal sign =", that tells it there are no delimiters now.
Compact
Since Compact runs against files in the directory without specifying the path in an argument, if applicable, you can first CD /D <Letter>: and then run Compact /C to compact all files in a directory, or Compact /C /S to compact all files and folders recursively in the directory.
Batch Script
for /f "skip=1" %%x in ('wmic logicaldisk get caption') do ( CD /D %%x Compact /C /S <Next or other command> )Note: The drive letter is listed in a
<letter>:format e.g.H:. SoCD /D H:works just fine. You will obviously add the\to the end of the iterated%%xi.e.%%x\if you need to append a path to use a full path (%%x\folder\path) as a command argument e.g.dir %%x\folder\path.
Further Resources
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.
However, I can't seem to change to that drive
Use the following batch file (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 )
endlocalNotes:
- Replace
dirwith the command you want to run on the root of each drive.
Further Reading
- An A-Z Index of the Windows CMD command line | SS64.com
- Windows CMD Commands (categorized) - Windows CMD - SS64.com
- CD Change Directory - Windows CMD - SS64.com
- Command Redirection, Pipes - Windows CMD - SS64.com
- Findstr - Search for strings - Windows CMD - SS64.com
- For - Loop through command output - Windows CMD - SS64.com
- Quotes, Escape Characters, Delimiters - Windows CMD - SS64.com
- WMIC - Windows Management - Windows CMD - SS64.com