Syntax Problem - moving files in subdirs with white space in filename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kindly_confused
Posts: 3
Joined: 14 Mar 2019 13:02

Syntax Problem - moving files in subdirs with white space in filename

#1 Post by kindly_confused » 14 Mar 2019 13:37

Hi,

I want to sort my .jpg files by time of day - I found that the filename contains the hour of the day at which it was taken - time corresponds to a digit preceded by a white space - only white space in the filename.

This command works to list the specific files in question:

Code: Select all

dir /s E:\photos\"* 19*".jpg
The white space is before the 19.

Trying to get this to work in conjunction with move or copy does not work.

Code: Select all


for /F %i in ('command to get files list') do command %i

for /f %i in ('dir E:\photos\"* 19*".jpg') do move E:\ %i

Any suggestions would be great.

I want to put this code into a batch file so I can sort my photos by time of day.

aGerman
Expert
Posts: 3670
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Syntax Problem - moving files in subdirs with white space in filename

#2 Post by aGerman » 14 Mar 2019 14:37

untested

Code: Select all

for /f "delims=" %%i in ('dir "E:\photos\* 19*.jpg"') do move "%%i"  "E:\your destination folder\"
In a batch script percent signs of FOR variables have to be doubled. The space is one of the default delimiters in a FOR loop which means that you have to override this behavior by defining "delims=". The file names in the FOR variable still contain the space and thus, the variable has to be quoted,

But ... why don't you just use wildcards in the move command?

Code: Select all

move "E:\photos\* 19*.jpg" "E:\your destination folder\"
Steffen

kindly_confused
Posts: 3
Joined: 14 Mar 2019 13:02

Re: Syntax Problem - moving files in subdirs with white space in filename

#3 Post by kindly_confused » 14 Mar 2019 17:25

Thanks Steffen

Second option works.

First option has a syntax problem - I had issues myself dealing with delim...

Any other suggestions for using a for loop?

I have implemented the move command in my batch file - works very well.

Squashman
Expert
Posts: 4107
Joined: 23 Dec 2011 13:59

Re: Syntax Problem - moving files in subdirs with white space in filename

#4 Post by Squashman » 14 Mar 2019 18:11

Code: Select all

for %%i in ("E:\photos\* 19*.jpg") do move "%%i"  "E:\your destination folder\"

kindly_confused
Posts: 3
Joined: 14 Mar 2019 13:02

Re: Syntax Problem - moving files in subdirs with white space in filename

#5 Post by kindly_confused » 14 Mar 2019 20:03

Problem, I celebrated too soon.

I wasn't clear enough - I need to scan sub-folders.

For loops are giving me a headache - already took two Tylenols this afternoon

As I stated:

This command works to list the specific files in question - IT SCANS SUB-FOLDERS:

[SCANNING SUB-FOLDERS WORKS!]

Code: Select all

dir /s E:\photos\"* 19*".jpg
The white space is before the 19.

Trying to get this to work in conjunction with move or copy does not work.

[DOESN'T WORK AND DOESN'T SCAN SUB-FOLDERS]

Code: Select all


for /F %i in ('command to get files list') do command %i

for /f %i in ('dir E:\photos\"* 19*".jpg') do move E:\ %i

I need a means of scanning sub-folders for a string containing a white space and then moving (or copying) the files to another folder.

I have several network drives mapped to ntfs folders.

Big thanks.

Squashman
Expert
Posts: 4107
Joined: 23 Dec 2011 13:59

Re: Syntax Problem - moving files in subdirs with white space in filename

#6 Post by Squashman » 14 Mar 2019 21:14

Code: Select all

for /R "E:\photos" %%i in ("* 19*.jpg") do move "%%i"  "E:\your destination folder\"

Post Reply