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.
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:
. So CD /D H:
works just fine. You will obviously add the \
to the end of the iterated %%x
i.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
.
![enter image description here](https://cdn.statically.io/img/i.sstatic.net/v4XK9.png)
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.