I want to batch change 50+ files in a folder in Windows10. the original name style: 'how to install the software - softwareA user manual p1.pdf', the new file name will be 'softwareA user manual p1 - how to install the software.pdf'. I am using a .bat file to do the job and exported a list of all the file names into a .txt file. I want to change each line from 'filename' to 'move filename new_filename'. I can add 'move ' in front of all the lines, but don't know how to generate new filename (swap 2 parts before and after - in the old filename) and append the new filename at the end of each line.
1 Answer
This should do it:
:%s/^\(.*\) - \(.*\)\.pdf$/move "&" "\2 - \1.pdf"
If you have a file with lines with only file names:
how to install the software - softwareA user manual p1.pdf
how to test the software - softwareA user manual p1.pdf
how to use the software - softwareB user manual p2.pdf
That substitute command will turn them into:
move "how to install the software - softwareA user manual p1.pdf" "softwareA user manual p1 - how to install the software.pdf"
move "how to test the software - softwareA user manual p1.pdf" "softwareA user manual p1 - how to test the software.pdf"
move "how to use the software - softwareB user manual p2.pdf" "softwareB user manual p2 - how to use the software.pdf"
Which should actually do everything you're trying to accomplish, including adding the move
command and quoting the file names, considering they contain spaces.
Trying to disect that regex a bit:
\(.*\)
matches a group and saves it in a "group" such as\1
,\2
, etc.- the
-
will match a dash surrounded by spaces, in the middle of the name. - the
\.pdf
will match a literal ".pdf" extension. - the
^
and$
anchors make sure this only matches full lines.
And the expansion:
&
expands to the entire text matched, which is the whole filename.\2
and\1
will expand to the matched groups, but in reverse order.- so
\2 - \1.pdf
will expand to the new filename you want. - adding literal quotes with the
"
s. - and finally including a
move
command.
Finally, the %
(right after :
) acts on the whole file (equivalent to a 1,$
range) and s
is the substitute command.
Save the resulting file as a .bat
and run it to batch rename all your files. Good luck!