Page 1 of 1

Moving Empty Folders

Posted: 16 Oct 2021 05:54
by drgt
I want to move empty folders specified in List.txt using the FOR command.

What am I doing wrong?

Screen Dump
C:\Documents and Settings\user\Desktop\New Folder (5)>for %x in ("C:\Docume
nts and Settings\user\Desktop\New Folder (5)\List.txt") DO move %x "C:\Docum
ents and Settings\user\Desktop\New Folder (4)\empty"
This moved the List.txt to the destination instead of the entries in List.txt

Contents of List.txt
"C:\Documents and Settings\user\Desktop\New Folder (5)\1"
"C:\Documents and Settings\user\Desktop\New Folder (5)\2"

Re: Moving Empty Folders

Posted: 16 Oct 2021 06:04
by aGerman
If you want to read the file you have to use a FOR /F loop like

Code: Select all

for /f "usebackq delims=" %%x in ("C:\wherever\file.txt") do ...
Steffen

Re: Moving Empty Folders

Posted: 16 Oct 2021 06:23
by drgt
Thank you Steffen!

Re: Moving Empty Folders

Posted: 16 Oct 2021 14:14
by Squashman
You were shown how to read a file in several of your previous batch files.
viewtopic.php?f=3&t=7045&p=45954#p45954

Moving Empty Folders

Posted: 16 Oct 2021 22:52
by drgt
Thank you for pointing that out!

Alzheimer is setting in!

Although I do not really understand the difference between "delims=" and "usebackq delims="...

Re: Moving Empty Folders

Posted: 17 Oct 2021 04:06
by aGerman
Without USEBACKQ, a string enclosed in double quotes is treated as string stream of characters. Since file names/paths can and often do contain spaces and other special characters requiring quotes, you have to change the semantics of the supported quotation marks (", ', and`). With USEBACKQ, a string enclosed in double quotes is treated as file path and is opened as file stream for reading.

Steffen

Moving Empty Folders

Posted: 17 Oct 2021 08:58
by drgt
Like I said,... Alzheimer & brain damage!
Perhaps examples to use one or the other will help.
Are there instances where one will work but not the other (and vice versa)?

Re: Moving Empty Folders

Posted: 17 Oct 2021 09:08
by aGerman
Since it is best practice to always quote paths (regardless whether they actually contain spaces or special characters like ampersands), you have to use them along with USEBACKQ in a FOR /F loop.
Read the help message of FOR in order to understand how the semantics of quotes are changed by USEBACKQ. I'm not willing to copy/paste those things into forum posts.

Steffen

Moving Empty Folders

Posted: 17 Oct 2021 09:14
by drgt
OK.
By help message I suppose you mean the one shown when I type for /? I on the command prompt?

Re: Moving Empty Folders

Posted: 18 Oct 2021 05:46
by ShadowThief
Exactly