Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to? rename files with spaces

Apr
1,793
15
assume .\temp\ already exists.

c:> set %fn=Screenshot_20181028-191416_Hangouts (2).jpg

c:> for %fn in (*) move /n /r "%fn" "temp\%@replace[:,_,%@replace[-,_,%@filedate[`%fn`,c,4]_%@filetime[`%fn`,c,s]]].%@ext[`%fn`]"

how would I get this to work - since @file functions require quoting in case the %fn has spaces?
 
assume .\temp\ already exists.

c:> set %fn=Screenshot_20181028-191416_Hangouts (2).jpg

c:> for %fn in (*) move /n /r "%fn" "temp\%@replace[:,_,%@replace[-,_,%@filedate[`%fn`,c,4]_%@filetime[`%fn`,c,s]]].%@ext[`%fn`]"

how would I get this to work - since @file functions require quoting in case the %fn has spaces?
I not sure what you expect the new file name to be.

I think the 1st thing is to use set fn="Screenshot_20181028-191416_Hangouts (2).jpg " with the quotes and no %
This fn is independant of the one used by FOR.

When you use the for %fn in (*) move /n /r "%fn" .... statement try:

Code:
for %fn in (*) move /n /r "%fn" %@replace[:,_,%@replace[-,_,%@filedate["%fn",c,4]_%@filetime["%fn",c,s]]].%@ext["%fn"]

I don't have the same directory structure as your so I ran a test case with your very long filename and several of my test files:
Code:
v24.00.19_$dir /nhsv /h /a:-d-h /t:c *.txt;*.jpg
11/11/2017   4:00             270  DummFNames.txt
12/05/2018   1:49              32  Screenshot_20181028-191416_Hangouts (2).jpg
 2/20/2017   1:13             851  TEST123.TXT
 2/20/2017   1:13              15  testdatename-07-15.tx

v24.00.19_$set fn="Screenshot_20181028-191416_Hangouts (2).jpg"

v24.00.19_$for %fn in (*.txt *.jpg) move /n /r "%fn" %@replace[:,_,%@replace[-,_,%@filedate["%fn",c,4]_%@filetime["%fn",c,s]]].%@ext["%fn"]
D:\BAT\test01\DummFNames.txt -> D:\BAT\test01\2017_11_11_04_00_39.txt
     1 file would be moved
D:\BAT\test01\TEST123.TXT -> D:\BAT\test01\2017_02_20_01_13_47.TXT
     1 file would be moved
D:\BAT\test01\testdatename-07-15.txt -> D:\BAT\test01\2017_02_20_01_13_47.txt
     1 file would be moved
D:\BAT\test01\Screenshot_20181028-191416_Hangouts (2).jpg -> D:\BAT\test01\2018_12_05_01_49_27.jpg
     1 file would be moved

v24.00.19_$echo %fn
"Screenshot_20181028-191416_Hangouts (2).jpg"
Those are really strange names, just having dates and times. Oh well, each to his own.
Looks like these changes will do what you requested.
 
@rps I actually had it in a

for %fn in (*) move /n /r "%fn" "temp\%@replace[:,_,%@replace[-,_,%@filedate[`%fn`,c,4]_%@filetime[`%fn`,c,s]]].%@ext[`%fn`]"

No set actually hardcoded. would it be better to use this?

for %fn in (*) move /n /r "%fn" "temp\%@replace[:,_,%@replace[-,_,%@filedate[%@quote[%fn],c,4]_%@filetime[%@quote[%fn],c,s]]].%@ext[%@quote[%fn]]"

?? thank you for your help !
 
if the directory the file is being moved to is on the same drive you could use REN and regular expressions. Your initial example shows you assigning a string indirectly to a variable. I.e. you have 'set %fn=' instead of 'set fn='. I'm not sure what you were trying to accomplish.

As far as the move/rename, what are you trying to accomplish? It looks like you want to replace the filename with a date/time string but keep the extension.
-Scott
 
Back to the beginning ... what's the matter with quoting the variable containing the filename normally? I don't feel like mucking around with FOR and wildcards, but this works.

Code:
v:\> set fn=a b.txt & ren "%fn" "a b-%@filedate["%fn"]-%@replace[:,-,%@filetime["%fn"]].%@ext["%fn"]"
V:\a b.txt -> V:\a b-2018-12-05-12-37.txt
     1 file renamed
 
@rps
...

for %fn in (*) move /n /r "%fn" "temp\%@replace[:,_,%@replace[-,_,%@filedate[%@quote[%fn],c,4]_%@filetime[%@quote[%fn],c,s]]].%@ext[%@quote[%fn]]"
Does that actually work? If so, then you have what you want. :confused:

I don't have any plugins loaded.

When I ran that in my test directory, I receive the error
Code:
TCC: (Sys) The system cannot find the file specified.
 "D:\BAT\test01\Screenshot_20181028-191416_Hangouts"

Which means the multiple "%fn" syntax is needed to act on any file names with spaces.

The outer most double quotes don't seem to do anything useful.
 
if the directory the file is being moved to is on the same drive you could use REN and regular expressions. Your initial example shows you assigning a string indirectly to a variable. I.e. you have 'set %fn=' instead of 'set fn='. I'm not sure what you were trying to accomplish.

As far as the move/rename, what are you trying to accomplish? It looks like you want to replace the filename with a date/time string but keep the extension.
-Scott

@samintz Guess in my OP I should have said I was doing the MOVEing in a for loop, sorry. I was moving them so that the files were not possibly going to be renamed more then once, then i would just MOVE /D /R /S temp\* . to move them back. And yes you are correct on my intentions.

Back to the beginning ... what's the matter with quoting the variable containing the filename normally? I don't feel like mucking around with FOR and wildcards, but this works.

Code:
v:\> set fn=a b.txt & ren "%fn" "a b-%@filedate["%fn"]-%@replace[:,-,%@filetime["%fn"]].%@ext["%fn"]"
V:\a b.txt -> V:\a b-2018-12-05-12-37.txt
     1 file renamed

@vefatica First I didn't want any part of the original filename to be part of the new name. Just to a date/time and original extension for the new file name.
 
@vefatica First I didn't want any part of the original filename to be part of the new name. Just to a date/time and original extension for the new file name.
That was just an experiment. In your original post, why didn't you just quote %fn normally when it's inside a @FILE* function? That seems to work.
 
I mean, what was the problem in the first place? Below I used COPY instead of MOVE (and didn't nest @replace). The files with spaces in their names (the first three) were handled just fine.

Code:
v:\> for %fn in ( *.txt ) copy "%fn" "temp\%@replace[-,_,%@filedate["%fn",c,4]]_%@replace[:,_,%@filetime["%fn",c,s]].%@ext["%fn"]"
V:\a b-2018-12-05-12-37.txt => V:\temp\2018_12_05_12_37_51.txt
     1 file copied
V:\a b-2018-12-05-12-40.txt => V:\temp\2018_12_05_12_40_23.txt
     1 file copied
V:\a b.txt => V:\temp\2018_12_05_16_33_16.txt
     1 file copied
V:\abc2017.txt => V:\temp\2018_10_22_00_24_12.txt
     1 file copied
 
  • Like
Reactions: rps

Similar threads

Back
Top