How to? rename files with spaces

Apr 2, 2011
1,441
10
54
North Carolina, USA
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?
 

rps

Jul 6, 2008
436
6
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.
 
Apr 2, 2011
1,441
10
54
North Carolina, USA
@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 !
 

samintz

Scott Mintz
May 20, 2008
1,503
16
Solon, OH, USA
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
 
May 20, 2008
11,424
99
Syracuse, NY, USA
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

Jul 6, 2008
436
6
@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.
 
Apr 2, 2011
1,441
10
54
North Carolina, USA
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.
 
May 20, 2008
11,424
99
Syracuse, NY, USA
@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.
 
May 20, 2008
11,424
99
Syracuse, NY, USA
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
Thread starter Title Forum Replies Date
Phileosophos WAD Can't rename files in subfolders Support 1
P Rename files with ampersand in the name Support 1
C How to rename files with "(" Support 14
A Rename Files in Chronological Order? Support 5
Joe Caverly Rename file that begins with Emoji Support 7
C Documentation Help Nit. REN / RENAME Support 0
R How to? Replace in Rename Support 9
M How to? File rename problem... Support 11
D Folders pane display problem after rename Support 0
fpefpe How to? Rename runs tree Support 10
JohnQSmith Slow network directory rename Support 8
JohnQSmith Folder pane right-click rename Support 2
samintz Regex Rename Support 2
nikbackm Rename TC tab from command line Support 0
MickeyF TCC crashing when copying multiple files (now resolved) Support 6
fpefpe How to? shortcuts and batch files Support 4
C v28 key files Support 2
J ffind does not find files Support 4
C duplicate files in multiple plugin sites Support 9
Dmitry L. Kobyakov Fixed The SETARRAY /R command works incorrectly with cyrillic files Support 2
S How to? Deinstalling TC/reinstating execution of bat files by cmd in Win7 Support 31
vefatica FFIND, temp files? ... not deleted? Support 0
R FUNCTION to count NUMBER of files matching a pattern and specific length/not length Support 4
vefatica Tmp files from in-process pipes not deleted Support 6
vefatica Finding files with alternate data streams? Support 4
rps Multi-column DIR /v not displaying all files. Support 5
D How to skip files in a fileset? Support 6
vefatica PIPEVIEW leaves TMP files Support 18
K "copy /z /w" command not deleting read-only files in destination Support 10
Jesse Heines "Taken" date for JPG and PNG files Support 4
O I cannot use links to .btm files Support 5
R How to? Append files in multiple subfolders using copy? Support 8
Joe Caverly "Functions" in cmd.exe batch files Support 6
S Batch files Calling Batch Files Support 2
vefatica Find Files/Text dialog: does it work? Support 0
Joe Caverly UNZIPping older .ZIP files Support 3
C Documentation default location for LOG files Support 21
P exit /b in batch files Support 7
P Labels in batch files: TCC vs CMD.exe Support 9
M ANSI is used to show files instead of OEM in version 17 to 19 ! Support 5
C 7Z files on forum? Support 0
x13 Problem listing repository files using DIR http(s)://... Support 8
B Can copy command concatenate multiple mp3 files Support 2
redwdc Copying files with spaces, commas, etc. Support 5
J New lexer for TCC batch files. Support 0
M How to? want to download a file from a website enumarate the links and download the files Support 2
keithg1964 TCC RT Install Files Support 1
P Fixed Rexx .cmd files no longer work in TCC 20 Support 3
thorntonpg Handling files with embedded spaces Support 1
WavSlave Fixed Unable to sort files by description in TCC v19 Support 2

Similar threads